def api_get_series_nfo(): type = 'seriesnfo' type = encode32(txt=type) vod_url = '{dut_epg_url}/{type}.zip'.format(dut_epg_url=CONST_DUT_EPG, type=type) file = os.path.join("cache", "{type}.json".format(type=type)) tmp = os.path.join(ADDON_PROFILE, 'tmp', "{type}.zip".format(type=type)) if not is_file_older_than_x_days(file=os.path.join(ADDON_PROFILE, file), days=0.45): data = load_file(file=file, isJSON=True) else: resp = Session().get(vod_url, stream=True) if resp.status_code != 200: resp.close() return None with open(tmp, 'wb') as f: for chunk in resp.iter_content(chunk_size=SESSION_CHUNKSIZE): f.write(chunk) resp.close() extract_zip(file=tmp, dest=os.path.join(ADDON_PROFILE, "cache", ""))
def api_download(url, type, headers=None, data=None, json_data=True, return_json=True, allow_redirects=True, auth=None): session = Session(cookies_key='cookies') if headers: session.headers = headers if type == "post" and data: if json_data: resp = session.post(url, json=data, allow_redirects=allow_redirects, auth=auth) else: resp = session.post(url, data=data, allow_redirects=allow_redirects, auth=auth) else: resp = getattr(session, type)(url, allow_redirects=allow_redirects, auth=auth) if return_json: try: returned_data = json.loads(resp.json().decode('utf-8'), object_pairs_hook=OrderedDict) except: try: returned_data = resp.json(object_pairs_hook=OrderedDict) except: returned_data = resp.text else: returned_data = resp.text session.close() return { 'code': resp.status_code, 'data': returned_data, 'headers': resp.headers, 'url': resp.url }
def api_get_vod_by_type(type, character, genre, subscription_filter): if not os.path.isdir(ADDON_PROFILE + 'tmp'): os.makedirs(ADDON_PROFILE + 'tmp') encodedBytes = base64.b32encode(type.encode("utf-8")) type = str(encodedBytes, "utf-8") vod_url = '{dut_epg_url}/{type}.zip'.format(dut_epg_url=CONST_DUT_EPG, type=type) file = "cache" + os.sep + "{type}.json".format(type=type) tmp = ADDON_PROFILE + 'tmp' + os.sep + "{type}.zip".format(type=type) if not is_file_older_than_x_days(file=ADDON_PROFILE + file, days=0.5): data = load_file(file=file, isJSON=True) else: resp = Session().get(vod_url, stream=True) if resp.status_code != 200: resp.close() return None with open(tmp, 'wb') as f: for chunk in resp.iter_content(chunk_size=SESSION_CHUNKSIZE): f.write(chunk) resp.close() if os.path.isfile(tmp): from zipfile import ZipFile try: with ZipFile(tmp, 'r') as zipObj: zipObj.extractall(ADDON_PROFILE + "cache" + os.sep) except: try: fixBadZipfile(tmp) with ZipFile(tmp, 'r') as zipObj: zipObj.extractall(ADDON_PROFILE + "cache" + os.sep) except: try: from resources.lib.base.l1.zipfile import ZipFile as ZipFile2 with ZipFile2(tmp, 'r') as zipObj: zipObj.extractall(ADDON_PROFILE + "cache" + os.sep) except: return None if os.path.isfile(ADDON_PROFILE + file): data = load_file(file=file, isJSON=True) else: return None else: return None data2 = OrderedDict() for currow in data: row = data[currow] id = row['id'] if genre and row['category']: if not genre in row['category']: continue if character: if not row['first'] == character: continue if subscription_filter and not int(id) in subscription_filter: continue data2[currow] = row return data2
def api_get_list_by_first(first, start, end, channels): if not os.path.isdir(ADDON_PROFILE + 'tmp'): os.makedirs(ADDON_PROFILE + 'tmp') list_url = '{dut_epg_url}/list.zip'.format(dut_epg_url=CONST_DUT_EPG) tmp = ADDON_PROFILE + 'tmp' + os.sep + 'list.zip' file = "cache" + os.sep + "list.json" if not is_file_older_than_x_days(file=ADDON_PROFILE + file, days=0.5): data = load_file(file=file, isJSON=True) else: resp = Session().get(list_url, stream=True) if resp.status_code != 200: resp.close() return None with open(tmp, 'wb') as f: for chunk in resp.iter_content(chunk_size=SESSION_CHUNKSIZE): f.write(chunk) resp.close() if os.path.isfile(tmp): from zipfile import ZipFile try: with ZipFile(tmp, 'r') as zipObj: zipObj.extractall(ADDON_PROFILE + "cache" + os.sep) except: try: fixBadZipfile(tmp) with ZipFile(tmp, 'r') as zipObj: zipObj.extractall(ADDON_PROFILE + "cache" + os.sep) except: try: from resources.lib.base.l1.zipfile import ZipFile as ZipFile2 with ZipFile2(tmp, 'r') as zipObj: zipObj.extractall(ADDON_PROFILE + "cache" + os.sep) except: return None if os.path.isfile(ADDON_PROFILE + file): data = load_file(file=file, isJSON=True) else: return None else: return None data2 = OrderedDict() data = data[str(first)] for currow in data: row = data[currow] try: if not int(row['startl']) < start or not int(row['starth']) > end: continue except: pass try: found = False for station in row['channels']: if station in channels: found = True break if found == False: continue except: pass data2[currow] = row return data2
def _download(url, dst, dst_path, arch, md5=None): filename = url.split('/')[-1] tmp = ADDON_PROFILE + "tmp" + os.sep + "widevine" downloaded = 0 if os.path.exists(dst_path): if md5 and md5sum(dst_path) == md5: #log.debug('MD5 of local file {} same. Skipping download'.format(filename)) return True elif not gui.yes_no(_.NEW_IA_VERSION): return False else: if os.path.exists(dst_path): os.remove(dst_path) with gui.progress(_(_.IA_DOWNLOADING_FILE, url=filename), heading=_.IA_WIDEVINE_DRM) as progress: resp = Session().get(url, stream=True) if resp.status_code != 200: raise InputStreamError( _(_.ERROR_DOWNLOADING_FILE, filename=filename)) total_length = float(resp.headers.get('content-length', 1)) with open(tmp, 'wb') as f: for chunk in resp.iter_content(chunk_size=SESSION_CHUNKSIZE): f.write(chunk) downloaded += len(chunk) downloadperc = int(downloaded) * 100 downloadperc2 = int(downloadperc) // int(total_length) percent = int(downloadperc2) if progress.iscanceled(): progress.close() resp.close() progress.update(percent) if os.path.isfile(tmp): if 'arm' in arch: with open(tmp, "rb") as encoded_file: import base64 decoded_string = base64.b64decode(encoded_file.read()) with open(dst_path, "wb") as decoded_file: decoded_file.write(decoded_string) else: from zipfile import ZipFile with ZipFile(tmp, 'r') as zipObj: zipObj.extractall(ADDON_PROFILE + "tmp" + os.sep) if os.path.isfile(ADDON_PROFILE + "tmp" + os.sep + dst): shutil.copy(ADDON_PROFILE + "tmp" + os.sep + dst, dst_path) for file in glob.glob(ADDON_PROFILE + "tmp" + os.sep + "*"): os.remove(file) if progress.iscanceled(): return False checksum = md5sum(dst_path) if checksum != md5: if os.path.exists(dst_path): os.remove(dst_path) raise InputStreamError( _(_.MD5_MISMATCH, filename=filename, local_md5=checksum, remote_md5=md5)) return True
def api_get_vod_by_type(type, character, genre, subscription_filter, menu=0): menu = int(menu) if not os.path.isdir(os.path.join(ADDON_PROFILE, 'tmp')): os.makedirs(os.path.join(ADDON_PROFILE, 'tmp')) if check_key(CONST_MOD_CACHE, str(type)): days = CONST_MOD_CACHE[str(type)] else: days = 0.5 type = encode32(txt=type) vod_url = '{dut_epg_url}/{type}.zip'.format(dut_epg_url=CONST_DUT_EPG, type=type) file = os.path.join("cache", "{type}.json".format(type=type)) tmp = os.path.join(ADDON_PROFILE, 'tmp', "{type}.zip".format(type=type)) if not is_file_older_than_x_days(file=os.path.join(ADDON_PROFILE, file), days=days): data = load_file(file=file, isJSON=True) else: resp = Session().get(vod_url, stream=True) if resp.status_code != 200: resp.close() return None with open(tmp, 'wb') as f: for chunk in resp.iter_content(chunk_size=SESSION_CHUNKSIZE): f.write(chunk) resp.close() if not extract_zip(file=tmp, dest=os.path.join(ADDON_PROFILE, "cache", "")): return None else: data = load_file(file=file, isJSON=True) if menu == 1: return data data2 = OrderedDict() for currow in data: row = data[currow] id = row['id'] if genre and genre.startswith('C') and genre[1:].isnumeric(): if not row['vidcollection'] or not genre in row['vidcollection']: continue elif genre: if not row['category'] or not genre in row['category']: continue if character: if not row['first'] == character: continue if subscription_filter and not int(id) in subscription_filter: continue data2[currow] = row return data2
def api_get_list_by_first(first, start, end, channels, movies=False): if not os.path.isdir(os.path.join(ADDON_PROFILE, 'tmp')): os.makedirs(os.path.join(ADDON_PROFILE, 'tmp')) list_url = '{dut_epg_url}/list.zip'.format(dut_epg_url=CONST_DUT_EPG) tmp = os.path.join(ADDON_PROFILE, 'tmp', 'list.zip') if movies == True: file = os.path.join("cache", "list_movies.json") else: file = os.path.join("cache", "list.json") if check_key(CONST_MOD_CACHE, 'list'): days = CONST_MOD_CACHE['list'] else: days = 0.5 if not is_file_older_than_x_days(file=os.path.join(ADDON_PROFILE, file), days=days): data = load_file(file=file, isJSON=True) else: resp = Session().get(list_url, stream=True) if resp.status_code != 200: resp.close() return None with open(tmp, 'wb') as f: for chunk in resp.iter_content(chunk_size=SESSION_CHUNKSIZE): f.write(chunk) resp.close() if not extract_zip(file=tmp, dest=os.path.join(ADDON_PROFILE, "cache", "")): return None else: data = load_file(file=file, isJSON=True) data2 = OrderedDict() try: data = data[str(first)] except: data = [] for currow in data: row = data[currow] try: if not int(row['startl']) < start or not int(row['starth']) > end: continue except: pass try: found = False for station in row['channels']: if station in channels: found = True break if found == False: continue except: pass data2[currow] = row return data2