@@ -682,7 +682,7 @@ def create_filter(self, user_id, filter_params):
682682 filter_params )
683683
684684 def _send (self , method , path , content = None , query_params = None , headers = None ,
685- api_path = MATRIX_V2_API_PATH ):
685+ api_path = MATRIX_V2_API_PATH , return_json = True ):
686686 if query_params is None :
687687 query_params = {}
688688 if headers is None :
@@ -741,8 +741,10 @@ def _send(self, method, path, content=None, query_params=None, headers=None,
741741 raise MatrixRequestError (
742742 code = response .status_code , content = response .text
743743 )
744-
745- return response .json ()
744+ if return_json :
745+ return response .json ()
746+ else :
747+ return response
746748
747749 def media_upload (self , content , content_type , filename = None ):
748750 query_params = {}
@@ -779,8 +781,87 @@ def get_download_url(self, mxcurl):
779781 else :
780782 raise ValueError ("MXC URL did not begin with 'mxc://'" )
781783
784+ def media_download (self , mxcurl , allow_remote = True ):
785+ """Download raw media from provided mxc URL.
786+
787+ Args:
788+ mxcurl (str): mxc media URL.
789+ allow_remote (bool): indicates to the server that it should not
790+ attempt to fetch the media if it is deemed remote. Defaults
791+ to true if not provided.
792+ """
793+ query_params = {}
794+ if not allow_remote :
795+ query_params ["allow_remote" ] = False
796+ if mxcurl .startswith ('mxc://' ):
797+ return self ._send (
798+ "GET" , mxcurl [6 :],
799+ api_path = "/_matrix/media/r0/download/" ,
800+ query_params = query_params ,
801+ return_json = False
802+ )
803+ else :
804+ raise ValueError (
805+ "MXC URL '%s' did not begin with 'mxc://'" % mxcurl
806+ )
807+
808+ def get_thumbnail (self , mxcurl , width , height , method = 'scale' , allow_remote = True ):
809+ """Download raw media thumbnail from provided mxc URL.
810+
811+ Args:
812+ mxcurl (str): mxc media URL
813+ width (int): desired thumbnail width
814+ height (int): desired thumbnail height
815+ method (str): thumb creation method. Must be
816+ in ['scale', 'crop']. Default 'scale'.
817+ allow_remote (bool): indicates to the server that it should not
818+ attempt to fetch the media if it is deemed remote. Defaults
819+ to true if not provided.
820+ """
821+ if method not in ['scale' , 'crop' ]:
822+ raise ValueError (
823+ "Unsupported thumb method '%s'" % method
824+ )
825+ query_params = {
826+ "width" : width ,
827+ "height" : height ,
828+ "method" : method
829+ }
830+ if not allow_remote :
831+ query_params ["allow_remote" ] = False
832+ if mxcurl .startswith ('mxc://' ):
833+ return self ._send (
834+ "GET" , mxcurl [6 :],
835+ query_params = query_params ,
836+ api_path = "/_matrix/media/r0/thumbnail/" ,
837+ return_json = False
838+ )
839+ else :
840+ raise ValueError (
841+ "MXC URL '%s' did not begin with 'mxc://'" % mxcurl
842+ )
843+
844+ def get_url_preview (self , url , ts = None ):
845+ """Get preview for URL.
846+
847+ Args:
848+ url (str): URL to get a preview
849+ ts (double): The preferred point in time to return
850+ a preview for. The server may return a newer
851+ version if it does not have the requested
852+ version available.
853+ """
854+ params = {'url' : url }
855+ if ts :
856+ params ['ts' ] = ts
857+ return self ._send (
858+ "GET" , "" ,
859+ query_params = params ,
860+ api_path = "/_matrix/media/r0/preview_url"
861+ )
862+
782863 def get_room_id (self , room_alias ):
783- """Get room id from its alias
864+ """Get room id from its alias.
784865
785866 Args:
786867 room_alias (str): The room alias name.
0 commit comments