def __configure_from_json(self, json): self.resource_id = json['id'] self.name = json['name'] self.tag = json['tag'] self.last_update = json['lastUpdate'] self.author_id = json['author']['id'] self.author_name = get_author_details(self.author_id)['username'] # TODO find way to get author object inside resource self.download = json['download'] self.link = json['link'] self.version = json['version'] self.versions = [] # Iterate through all the versions available and append it to the objects available versinos for version in json['versions']: self.versions.append(version) self.category_id = json['category']['id'] self.external = json['external'] self.file_size = "" self.file_type = "" if not self.external: self.file_size = json['file']['size'] self.file_type = json['file']['type'] else: self.file_type = os.path.splitext(os.path.basename(urlsplit(self.download).path))[1]
def __configure_from_json(self, json): self.resource_id = json['id'] self.name = json['name'] self.tag = json['tag'] self.last_update = json['lastUpdate'] self.author_id = json['author']['id'] self.author_name = get_author_details(self.author_id)['username'] # TODO find way to get author object inside resource self.download = json['download'] self.link = json['link'] self.version = json['version'] self.versions = [] # Iterate through all the versions available and append it to the objects available versinos for version in json['versions']: self.versions.append(version) self.category_id = json['category']['id'] self.external = json['external'] self.file_size = "" self.file_type = "" if not self.external: self.file_size = json['file']['size'] self.file_type = json['file']['type'] else: self.file_type = os.path.splitext( os.path.basename(urlsplit(self.download).path))[1]
def iri2uri(uri): """Convert an IRI to a URI. Note that IRIs must be passed in a unicode strings. That is, do not utf-8 encode the IRI before passing it into the function.""" if isinstance(uri ,str): (scheme, authority, path, query, fragment) = urllib.urlsplit(uri) authority = authority.encode('idna') # For each character in 'ucschar' or 'iprivate' # 1. encode as utf-8 # 2. then %-encode each octet of that utf-8 uri = urllib.urlunsplit((scheme, authority, path, query, fragment)) uri = "".join([encode(c) for c in uri]) return uri
def __parse_path(self, url): if sys.version_info < (3, 0): path = urlparse.urlsplit(url).path else: path = urllib.urlsplit(url).path path = path if path else '/' pattens = urllib.unquote(path).split('/') tmp_paths = [] for v in pattens: tmp_paths.append(self.__urlencode(v)) urlpath = '/'.join(tmp_paths) if urlpath[-1] != '/': urlpath = urlpath + '/' return urlpath
def __parse_query_string(self, url): if sys.version_info < (3, 0): query = urlparse.parse_qs(urlparse.urlsplit(url).query) else: query = urllib.parse_qs(urllib.urlsplit(url).query) sorted_query = sorted(query.items(), key=lambda item: item[0]) sorted_query_string = '' for (k, v) in sorted_query: if type(v) is list: v.sort() for item in v: sorted_query_string += '&' + self.__urlencode( k) + '=' + self.__urlencode(item) else: sorted_query_string += '&' + self.__urlencode( k) + '=' + self.__urlencode(v) return sorted_query_string[1:]
def post_url(url, fields, files=[]): urlparts = urllib.urlsplit(url) return post_multipart(urlparts[1], urlparts[2], fields, files)
def __filename_from_url(url): "%s%s" % os.path.splitext(os.path.basename(urlsplit(url).path))