def quote_paths(url): """ This function will quote paths **only** in a given url :param url: string or unicode :return: joined url string """ try: url = py2_enc(url) if url.startswith('http'): parsed = urlparse(url) processed_path = '/'.join( [quote(i) for i in parsed.path.split('/')]) url = urlunparse(parsed._replace(path=processed_path)) return url else: path = '/'.join([quote(i) for i in url.split('/')]) return path except Exception: return url
def call(): plugin_call = 'plugin://plugin.video.youtube/api/update/?enable=true' route = '{0}&client_id={1}&client_secret={2}&api_key={3}'.format( plugin_call, quote(credentials[0]), quote(credentials[2]), quote(credentials[1])) control.execute('RunPlugin({0})'.format(route))
def mini_picker(hl, sl, params): try: image = params.get('image').encode('latin-1') title = params.get('title').encode('latin-1') except (UnicodeEncodeError, UnicodeDecodeError, AttributeError): image = params.get('image') title = params.get('title') if len(hl) == 1: stream = cache.get(gm_debris, 12, sl[0]) if control.setting('action_type') == '2': if control.setting('auto_play') == 'true': play_url = sysaddon + play_action + quote( stream) + '&image=' + quote(image) + '&title=' + quote( title) control.execute('PlayMedia("{0}")'.format(play_url)) else: m3u_file = playlist_maker(hl, sl, title, image) control.playlist().load(m3u_file) control.openPlaylist() else: control.infoDialog(hl[0]) return stream elif control.setting('action_type') == '2': m3u_file = playlist_maker(hl, sl, title, image) control.playlist().load(m3u_file) if control.setting('auto_play') == 'true': control.execute('Action(Play)') else: control.openPlaylist() return else: choice = control.selectDialog(heading=control.lang(30064), list=hl) if choice <= len(sl) and not choice == -1: popped = sl[choice] return cache.get(gm_debris, 12, popped) else: return
def set_controls(self): # Image object self.image = pyxbmct.Image( 'http://chart.apis.google.com/chart?cht=qr&chl={0}&chs=256'.format( quote(authorization_link())), aspectRatio=2) self.placeControl(self.image, 0, 0, 6) # Text box self.text_box = pyxbmct.TextBox() self.placeControl(self.text_box, 0, 1, 2) self.text_box.setText(control.lang(30064).format(get_ip())) self.text_box.autoScroll(1000, 1000, 1000) # Button capable of changing the local ip address self.address_button = pyxbmct.Button(control.lang(30056)) self.placeControl(self.address_button, 2, 1) self.connect(self.address_button, lambda: ip_address_set()) # Button capable of starting the server self.server_button = pyxbmct.Button(control.lang(30147)) self.placeControl(self.server_button, 3, 1) self.connect(self.server_button, lambda: reddit_server()) # Button displaying address for auth, and capable of opening it self.link_button = pyxbmct.Button(self.link) self.placeControl(self.link_button, 4, 1) self.connect(self.link_button, lambda: control.open_web_browser(self.link)) # Close button self.close_button = pyxbmct.Button(control.lang(30062)) self.placeControl(self.close_button, 5, 1) self.connect(self.close_button, self.close)
def category_list(url): if BASE_API_LINK in url: _json = client.request(url, output='json') list_of_lists = _json['sectionContents'] codename = parse_qs(split(url)[1])['pageCodename'][0] page = _json['pagination']['page'] total_pages = _json['pagination']['totalPages'] else: html = client.request(url) script = [ i for i in client.parseDOM(html, 'script') if 'INITIAL_STATE' in i ][0] script = re.sub(r'var _*?\w+_*? = ', '', script).partition(';</script>')[0] if script.endswith(';'): script = script[:-1] _json = json.loads(script) pages = _json['pages'] list_of_lists = [ i for i in list(pages['sectionsByCodename'].values()) if 'adman' not in i['sectionContentCodename'] ] codename = list(pages.keys())[-1] page = 1 total_pages = pages[codename]['totalPages'] next_url = GET_PAGE_CONTENT.format(page + 1, codename) self_list = [] for list_ in list_of_lists: title = list_['portalName'] section_codename = list_['sectionContentCodename'] if not list_['tilesIds']: continue url = LIST_OF_LISTS_LINK.format( title=quote(section_codename), pagecodename=codename, backurl=codename, sectioncodename=list_['sectionContentCodename']) data = {'title': title, 'url': url} if page < total_pages: data.update({ 'nextaction': 'categories', 'nextlabel': 30500, 'nexticon': control.addonmedia('next.jpg'), 'next': next_url }) self_list.append(data) return self_list
def search(self): input_str = control.inputDialog() try: input_str = cleantitle.strip_accents(input_str.decode('utf-8')) except (UnicodeEncodeError, UnicodeDecodeError, AttributeError): input_str = cleantitle.strip_accents(input_str) input_str = quote(input_str.encode('utf-8')) directory.run_builtin(action='listing', url=self.search_link.format(input_str))
def wrapper(self, str_input, category): post = 'searchcategory={0}&searchtext={1}'.format( category, quote(str_input.encode('utf-8'))) if category == 'person': self.list = gm.Indexer().persons_index(gm.SEARCH, post=post) else: self.list = gm.Indexer().listing(gm.SEARCH, post=post, get_listing=True) return self.list
def window_activate(url=None, query=None, jump=False, action='listing'): if not jump: if query == 'video': window = 'videos' elif query == 'audio': window = 'music' elif query == 'image': window = 'pictures' else: window = 'programs' executable = 'ActivateWindow({0},"{1}?content_type={2}&action=root",return)'.format( window, sysaddon[:-1], query) control.execute(executable) elif url == 'home': control.execute('Container.Update({0}?action=root)'.format( sysaddon[:-1])) else: if query: param = 'query' else: param = 'url' try: obj = quote(query if param == 'query' else url) except KeyError: obj = quote(query if param == 'query' else url.encode('latin1')) executable = 'Container.Update({0}?action={1}&{2}={3},return)'.format( sysaddon[:-1], action, param, obj) control.execute(executable)
def playlist_maker(hl=None, sl=None, title=None, image=None, m3u_playlist=None): if not m3u_playlist: try: title = title.decode('utf-8') except (UnicodeEncodeError, UnicodeDecodeError, AttributeError): pass vids = [ sysaddon + play_action + cache.get(gm_debris, 12, i) + '&image=' + quote(image) + '&title=' + quote(title) for i in sl ] videos = zip(hl, vids) if control.setting('randomize_items') == 'true': random.shuffle(videos) play_list = [u'#EXTM3U\n'] + [ u'#EXTINF:0,{0}\n'.format(title + u' - ' + h) + v + u'\n' for h, v in videos ] m3u_playlist = u''.join(play_list) m3u_file = control.join(control.transPath('special://temp'), 'pl_action.m3u') with open(m3u_file, 'w') as f: try: f.write(m3u_playlist.encode('utf-8')) except Exception: f.write(m3u_playlist) return m3u_file
def download(self, path, url): try: url = re.findall(r'/(\d+)/', url + '/', re.I)[-1] url = ''.join([self.download_link, '/getp.php?id={0}'.format(url)]) url = client.request(url, output='geturl', timeout=control.setting('timeout')) req = Request(url) req.add_header('User-Agent', randomagent()) opener = urlopen(req) data = opener.read() zip_file = zipfile.ZipFile(BytesIO(data)) opener.close() files = zip_file.namelist() files = [i for i in files if i.startswith('subs/')] srt = [i for i in files if i.endswith(('.srt', '.sub'))] archive = [i for i in files if i.endswith(('.rar', '.zip'))] if len(srt) > 0: if len(srt) > 1: srt = multichoice(srt) else: srt = srt[0] result = zip_file.open(srt).read() subtitle = basename(srt) try: subtitle = control.join(path, subtitle.decode('utf-8')) except Exception: subtitle = control.join(path, subtitle) with open(subtitle, 'wb') as subFile: subFile.write(result) return subtitle elif len(archive) > 0: if len(archive) > 1: archive = multichoice(archive) else: archive = archive[0] result = zip_file.open(archive).read() f = control.join(path, os_split(url)[1]) with open(f, 'wb') as subFile: subFile.write(result) dirs, files = control.listDir(path) if len(files) == 0: return if zipfile.is_zipfile(f): zipped = zipfile.ZipFile(f) zipped.extractall(path) if not zipfile.is_zipfile(f): if control.infoLabel('System.Platform.Windows'): uri = "rar://{0}/".format(quote(f)) else: uri = "rar://{0}/".format(quote_plus(f)) dirs, files = control.listDir(uri) else: dirs, files = control.listDir(path) if dirs and not zipfile.is_zipfile(f): for dir in dirs: _dirs, _files = control.listDir(control.join(uri, dir)) [files.append(control.join(dir, i)) for i in _files] if _dirs: for _dir in _dirs: _dir = control.join(_dir, dir) __dirs, __files = control.listDir( control.join(uri, _dir)) [ files.append(control.join(_dir, i)) for i in __files ] filenames = [i for i in files if i.endswith(('.srt', '.sub'))] filename = multichoice(filenames) try: filename = filename.decode('utf-8') except Exception: pass if not control.exists(control.join( path, os_split(filename)[0])) and not zipfile.is_zipfile(f): control.makeFiles(control.join(path, os_split(filename)[0])) subtitle = control.join(path, filename) if not zipfile.is_zipfile(f): with closing(control.openFile(uri + filename)) as fn: try: output = bytes(fn.readBytes()) except Exception: output = bytes(fn.read()) content = output.decode('utf-16') with closing(control.openFile(subtitle, 'w')) as subFile: subFile.write(bytearray(content.encode('utf-8'))) fileparts = os_split(subtitle)[1].split('.') # noinspection PyTypeChecker result = control.join( os_split(subtitle)[0], 'subtitles.' + fileparts[len(fileparts) - 1]) control.rename(subtitle, result) return result except Exception as e: _, __, tb = sys.exc_info() print(traceback.print_tb(tb)) log_debug( 'Subtitles.gr subtitle download failed for the following reason: ' + str(e)) return
def _listing(self, url): if self.ajax_url in url: result = client.request(url.partition('?')[0], post=url.partition('?')[2]) else: result = client.request(url) try: header = parseDOM(result, 'h2')[0] except IndexError: header = None next_url = None override = False if self.base_link + '/?s=' in url or control.setting('pagination') == 'true': override = True threads_1 = [] threads_2 = [] # Nest the function to work on either of the two cases def _exec(_items, _next_url=None): if control.setting('threading') == 'true': for count, _item in list(enumerate(_items, start=1)): threads_2.append(workers.Thread(self.loop(_item, header, count, _next_url))) [i.start() for i in threads_2] [i.join() for i in threads_2] else: for count, _item in list(enumerate(_items, start=1)): self.loop(_item, header, count, _next_url) if 'enimerosi-24' not in url and self.ajax_url not in url: ajaxes = [i for i in parseDOM(result, 'script', attrs={'type': 'text/javascript'}) if 'ajaxurl' in i] ajax1 = json.loads(re.search(r'var loadmore_params = ({.+})', ajaxes[-1]).group(1)) ajax2 = json.loads(re.search(r'var cactus = ({.+})', ajaxes[0]).group(1)) ajax = self._ajax_merge(ajax1, ajax2) pages = int(ajax['max_page']) posts = ajax['posts'] try: posts = posts.encode('utf-8') except Exception: pass if control.setting('threading') == 'true' and not override: for i in range(0, pages + 1): threads_1.append( workers.Thread( self.thread(self.ajax_url, post=self.load_more.format(query=quote(posts), page=str(i))) ) ) [i.start() for i in threads_1] [i.join() for i in threads_1] else: for i in range(0, pages + 1): a = client.request(self.ajax_url, post=self.load_more.format(query=quote(posts), page=str(i))) self.data.append(a) if i == 0 and override: next_url = '?'.join([self.ajax_url, self.load_more.format(query=quote(posts), page='1')]) break html = '\n'.join(self.data) items = itertags_wrapper(html, 'div', attrs={'class': 'item item-\d+'}) if len(items) < 20: next_url = None _exec(items, next_url) elif self.ajax_url in url: items = itertags_wrapper(result, 'div', attrs={'class': 'item item-\d+'}) parsed = dict(parse_qsl(url.partition('?')[2])) next_page = int(parsed['page']) + 1 parsed['page'] = next_page if len(items) >= 20: next_url = '?'.join([url.partition('?')[0], urlencode(parsed)]) _exec(items, next_url) else: items = itertags_wrapper(result, 'div', attrs={'class': 'item item-\d+'}) for item in items: text = item.text img = item.attributes['style'] image = re.search(r'url\((.+)\)', img).group(1) title = client.replaceHTMLCodes(parseDOM(text, 'a')[0].strip()) url = parseDOM(text, 'a', ret='href')[0] self.list.append({'title': title, 'image': image, 'url': url}) return self.list
def download(path, url): try: result = client.request(url) f = control.join(path, os_split(url)[1]) with open(f, 'wb') as subFile: subFile.write(result) dirs, files = control.listDir(path) if len(files) == 0: return if not f.lower().endswith('.rar'): try: zipped = zipfile.ZipFile(f) zipped.extractall(path) except Exception: control.execute('Extract("{0}","{0}")'.format(f, path)) if f.lower().endswith('.rar'): if control.infoLabel('System.Platform.Windows'): uri = "rar://{0}/".format(quote(f)) else: uri = "rar://{0}/".format(quote_plus(f)) dirs, files = control.listDir(uri) else: dirs, files = control.listDir(path) if dirs and f.lower().endswith('.rar'): for dir in dirs: _dirs, _files = control.listDir(control.join(uri, dir)) [files.append(control.join(dir, i)) for i in _files] if _dirs: for _dir in _dirs: _dir = control.join(_dir, dir) __dirs, __files = control.listDir( control.join(uri, _dir)) [ files.append(control.join(_dir, i)) for i in __files ] filenames = [i for i in files if i.endswith(('.srt', '.sub'))] if len(filenames) == 1: filename = filenames[0] else: filename = multichoice(filenames) try: filename = filename.decode('utf-8') except Exception: pass if not control.exists(control.join( path, os_split(filename)[0])) and f.lower().endswith('.rar'): control.makeFiles(control.join(path, os_split(filename)[0])) subtitle = control.join(path, filename) if f.lower().endswith('.rar'): content = openFile(uri + filename).read() with open(subtitle, 'wb') as subFile: subFile.write(content) fileparts = os_split(subtitle)[1].split('.') result = control.join( os_split(subtitle)[0], 'subtitles.' + fileparts[len(fileparts) - 1]) rename(subtitle, result) return result except Exception as e: log.log( 'Subzxyz subtitle download failed for the following reason: ' + str(e)) return
def download(self, path, url): # try: url = re.findall('/(\d+)/', url + '/', re.I)[-1] url = 'http://www.greeksubtitles.info/getp.php?id={0}'.format(url) url = client.request(url, output='geturl') data = urlopen(url, timeout=20).read() zip_file = zipfile.ZipFile(StringIO(data)) files = zip_file.namelist() files = [i for i in files if i.startswith('subs/')] srt = [i for i in files if i.endswith(('.srt', '.sub'))] archive = [i for i in files if i.endswith(('.rar', '.zip'))] if len(srt) > 0: if len(srt) > 1: srt = multichoice(srt) else: srt = srt[0] result = zip_file.open(srt).read() subtitle = basename(srt) try: subtitle = control.join(path, subtitle.decode('utf-8')) except Exception: subtitle = control.join(path, subtitle) with open(subtitle, 'wb') as subFile: subFile.write(result) return subtitle elif len(archive) > 0: if len(archive) > 1: archive = multichoice(archive) else: archive = archive[0] result = zip_file.open(archive).read() f = control.join(path, os_split(url)[1]) with open(f, 'wb') as subFile: subFile.write(result) dirs, files = control.listDir(path) if len(files) == 0: return if zipfile.is_zipfile(f): # try: # zipped = zipfile.ZipFile(f) # zipped.extractall(path) # except Exception: control.execute('Extract("{0}","{0}")'.format(f, path)) if not zipfile.is_zipfile(f): if control.infoLabel('System.Platform.Windows'): uri = "rar://{0}/".format(quote(f)) else: uri = "rar://{0}/".format(quote_plus(f)) dirs, files = control.listDir(uri) else: dirs, files = control.listDir(path) if dirs and not zipfile.is_zipfile(f): for dir in dirs: _dirs, _files = control.listDir(control.join(uri, dir)) [files.append(control.join(dir, i)) for i in _files] if _dirs: for _dir in _dirs: _dir = control.join(_dir, dir) __dirs, __files = control.listDir( control.join(uri, _dir)) [ files.append(control.join(_dir, i)) for i in __files ] filenames = [i for i in files if i.endswith(('.srt', '.sub'))] filename = multichoice(filenames) try: filename = filename.decode('utf-8') except Exception: pass if not control.exists(control.join( path, os_split(filename)[0])) and not zipfile.is_zipfile(f): control.makeFiles(control.join(path, os_split(filename)[0])) subtitle = control.join(path, filename) if not zipfile.is_zipfile(f): content = openFile(uri + filename).read() with open(subtitle, 'wb') as subFile: subFile.write(content) fileparts = os_split(subtitle)[1].split('.') result = control.join( os_split(subtitle)[0], 'subtitles.' + fileparts[len(fileparts) - 1]) rename(subtitle, result) return result