def _call(self, url, headers=None): if headers is None: headers = {} headers["Upgrade-Insecure-Requests"] = "1" headers["User-Agent"] = common.mozilla_agent() response = common.urlcall(url, headers=headers) return self._parse(response)
def doanload_updates(self): release = self.check_updates() if release is not None: url = release["url"] file = url[url.rfind("/") + 1:len(url)] if not os.path.exists(self.UPDATE): os.makedirs(self.UPDATE) file = os.path.join(self.UPDATE, file) try: self.debug("Downloading new release: %s" % url) data = common.urlcall(url, output='binary') handler = open(file, 'wb') handler.write(data) handler.close() self.trace('Successfully wrote data to file: %s' % file) return True except IOError as e: common.error('Unable to write data to [%s] file: %s' % (file, str(e))) return False except Exception as e: common.error( 'Unknown error while downloading/writing data to [%s] file: %s' % (file, str(e))) return False else: return False
def _setSkinArtistImages(self, artist): common.debug("Collecting images for artist: %s" % str(artist)) images = [] params = {} kontor = 0 params['lang'] = self.__BIOLANGUAGE params['artist'] = artist params['infodir'] = self.dir_cache params['exclusionsfile'] = os.path.join(self.dir_cache, "_exclusions.nfo") for key in self.PROVIDERS.keys(): if self._isPlaybackChanged(): common.debug( "Cancel collecting images due to the change of player content" ) return common.debug('Identifying images by provider: [%s]' % key) params['getall'] = common.setting(key + "_all") params['clientapikey'] = common.setting(key + "_apikey") content = self.PROVIDERS[key].getImageList(params) if content is not None and len(content) > 0: images.extend(content) if self._reload: self._reload = False common.debug( "Cancel collecting images due to the addon configuration update" ) break common.trace("Downloading images for artist [%s]" % str(artist)) _, cachefiles = xbmcvfs.listdir(self.dir_cache) for url in images: if self._isPlaybackChanged(): common.debug( "Cancel downloading images due to the change of player content" ) break common.trace('Checking image URL: %s' % url) cachepath = utilities.ItemHashWithPath( url, self.dir_cache) + utilities.ImageType(url) if os.path.split( cachepath )[1] not in cachefiles and not xbmc.abortRequested and not self._isPlaybackChanged( ): common.trace('Downloading image file: %s' % cachepath) urldata = common.urlcall(url, output='binary', certver=self.__SSLCHECK) success = utilities.WriteFile(urldata, cachepath) if urldata else False if success and xbmcvfs.Stat(cachepath).st_size() < 999: utilities.DeleteFile(cachepath) elif success: kontor += 1 if (kontor % 5 == 0) or (kontor == 1 and len([ f for f in cachefiles if os.path.splitext(f)[1] != ".nfo" ]) == 0): self._setSkinSlideshow(None, self.dir_cache) common.trace("Images setup is done")
def validate(self): response = self._parse( common.urlcall( "https://api.openweathermap.org/data/2.5/weather?id=2172797&appid=%s" % self.apikey)) if response is not None and response["cod"] != 200: raise RuntimeError(response["message"]) elif response is None: raise RuntimeError("No content provided")
def validate(self): response = common.urlcall( "https://api.darksky.net/forecast/%s/37.8267,-122.4233?exclude=minutely,hourly,daily,alerts,flags" % self.apikey) if response is not None: response = self._parse(response) if "error" in response: raise RuntimeError(response["error"]) elif response is None: raise RuntimeError("No content provided due to an HTTP error")
def publiclversion(self): try: latest = common.urlcall(self.URLBASE_LATEST, output='json') sysinfo = common.sysinfo() if sysinfo is not None and sysinfo != {}: device = sysinfo["DEVICE"] else: device = None if latest is not None and latest != {} and device is not None: return latest["devices"][device]["version"] else: return None except BaseException as be: self.error( "Error reading latest release details published in repository: %s" % str(be)) return None
def check_updates(self): device = None system = None latest = None try: latest = common.urlcall(self.URLBASE_LATEST, output='json') sysinfo = common.sysinfo() if sysinfo is not None and sysinfo != {}: device = sysinfo["DEVICE"] system = sysinfo["VERSION_ID"] if latest is not None and latest != {} and device is not None: latest = latest["devices"][device] except BaseException as be: self.error( "Error checking latest release vs current system version: %s" % str(be)) if system is not None and latest is not None and latest[ "version"] > system: self.debug("New version found: %s" % str(latest["version"])) return latest else: return None
def getImageList(self, params): common.trace("Starting to search images using parameters: %s" %str(params), "duckgo") images = [] if params.get('mbid', '') == '': common.warn("No artist identified over MusicBrainz, search stopped") return images if "fullname" in params and not common.isempty(params['fullname']): keywords = params['fullname'] + " AND (singer OR band)" elif "alias" in params and not common.isempty(params['alias']): keywords = params['alias'] + " AND (singer OR band)" elif "artist" in params and not common.isempty(params['artist']): keywords = params['artist'] + " AND (singer OR band)" else: keywords = None if keywords is not None and "location" in params and not common.isempty(params['location']): keywords = keywords + " AND " + params['location'] elif keywords is not None and "lang" in params and not common.isempty(params['lang']): keywords = keywords + " AND " + params['lang'] if keywords is not None: payload = {'q': keywords} common.trace("Hitting DuckDuckGo for token", "duckgo") data = common.urlcall(self.DDG_SEARCH_URL, "POST", payload=payload) searchObj = re.search(r'vqd=([\d-]+)\&', data, re.M | re.I) if not searchObj: common.error("Token parsing failed!", "duckgo") return images else: common.debug("Obtained token: %s" % searchObj.group(1), "duckgo") headers = { 'authority': 'duckduckgo.com', 'accept': 'application/json, text/javascript, */*; q=0.01', 'sec-fetch-dest': 'empty', 'x-requested-with': 'XMLHttpRequest', 'user-agent': common.agent(), 'sec-fetch-site': 'same-origin', 'sec-fetch-mode': 'cors', 'referer': 'https://duckduckgo.com/' } payload = { "q": keywords, "vqd": searchObj.group(1), "v7exp": "a", "o": "json", "l": "wt-wt", "f": ",,,", "p": '1' } data = None while True: try: data = common.urlcall(self.DDG_SEARCH_URL + "i.js", headers=headers, payload=payload, output='json') break except ValueError as e: common.trace("Calling url failure; sleep and retry", "duckgo") common.sleep(500) continue index = 0 max = common.any2int(params['limit'], 0) for obj in data["results"]: contextual = str(obj["title"].encode('utf-8')).lower().find(params['artist'].lower() + " ") >= 0 dimension = int(obj["width"]) >= 876 if common.any2bool(params.get('getall', 'false')) else int(obj["width"]) >= 1920 if contextual and dimension: index += 1 images.append(obj["image"]) if max > 0 and index >= max: break if not images: return [] else: return self._delExclusions(images, params.get('exclusionsfile'))
# -*- coding: utf-8 -*-