def getVideos(self, name, url, img, page, category, pamode): printD('Youtube.getVideos name[%s]' % (name)) #category='channel' print 'category', category if "channel" == category or 'browse_ajax' in url: if -1 == url.find('browse_ajax'): if url.endswith('/videos'): url = url + '?flow=list&view=0&sort=dd' else: url = url + '/videos?flow=list&view=0&sort=dd' category = 'channel' self.currList = self.getVideosFromChannelList( url, category, page, {}) print 'self.currList-1', self.currList[-1] elif "playlist" == category: self.currList = self.getVideosFromPlaylist(url, category, page, {}) elif "traylist" == category: self.currList = self.getVideosFromTraylist(url, category, page, cItem) else: printD('YTlist.getVideos Error unknown category[%s]' % category) self.displayList(self.currList, category, page, pamode)
def submenu(self, name, chid, page=''): url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&type=playlist&channelId=' + chid + '&maxResults=11' + default_params + '&key=' + KEYV3 printD('urlvv', url) pcount = str(self.gettotalresults(url)) url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&channelId=' + chid + '&maxResults=11' + default_params + '&key=' + KEYV3 vcount = str(self.gettotalresults(url)) if int(pcount) > 0 and int(vcount) == 0: self.addDir('\\c0000??00 ' + 'Playlists(' + pcount + ')', url, 0, 'img/playlist.png', name, '') url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&type=playlist&channelId=' + chid + '&maxResults=11' + default_params + '&key=' + KEYV3 getviedoList(name, url, page) if int(pcount) == 0 and int(vcount) > 0: self.addDir('\\c0??????? Videos(' + vcount + ')', url, 0, 'img/video.png', name, '') url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&channelId=' + chid + '&maxResults=11' + default_params + '&key=' + KEYV3 getviedoList(name, url, page) if int(pcount) > 0 and int(vcount) > 0: url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&type=playlist&channelId=' + chid + '&maxResults=11' + default_params + '&key=' + KEYV3 self.addDir('\\c0000??00 ' + 'Playlists(' + pcount + ')', url, 1002, 'img/playlist.png', name, '') url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&channelId=' + chid + '&maxResults=11' + default_params + '&key=' + KEYV3 self.addDir('\\c0??????? Videos(' + vcount + ')', url, 1002, 'img/video.png', name, '') if int(pcount) == 0 and int(vcount) == 0: self.addDir('Error:No videos or playlists in channel', url, 0, 'img/error.png', name, '')
def getuserlist(self): files = [] fullpath = [] user_iptv_file = iptv_folder + 'iptvlist' print "user_iptv_file", user_iptv_file if os.path.exists(user_iptv_file): f = open(user_iptv_file, 'r') lines = f.readlines() print "linesxx", lines for line in lines: line = line.strip() if line.startswith('m3u'): parts = line.split(';') itemtype = parts[0] itempath = parts[2] itemname = parts[1] try: if True: success = downloadfile( itempath, iptv_folder + itemname + '.m3u') printD("success", success) if not os.path.exists(folder + itemname + '.lnk'): cfile = open(folder + itemname + '.lnk', "w") cfile.write(itempath) cfile.close() except: printE() pass if line.startswith('iptv'): parts = line.split(';') itemtype = parts[0] itempath = parts[2] itemname = parts[1] self.addDir(itemname, itempath, 6, 'img/iptv.png', '', 1) if os.path.exists(folder): emptyfolder = True for item in os.listdir(folder): itempath = folder + item if os.path.isfile(itempath): if item.endswith('.m3u'): itemname = item.replace('.m3u', '') icon = self.geticon(itemname) self.addDir(itemname, itempath, 1, icon, '', 1) emptyfolder = False if emptyfolder == True: self.addDir('No m3u file in ' + iptv_folder, "http://", 1, '', '', 1)
def getSearchResult(self, pattern, searchType, page, nextPageCategory, sortBy=''): printDBG( 'YouTubeParser.getSearchResult pattern[%s], searchType[%s], page[%s]' % (pattern, searchType, page)) currList = [] try: url = 'http://www.youtube.com/results?search_query=%s&filters=%s&search_sort=%s&page=%s' % ( pattern, searchType, sortBy, page) printD(url) sts, data = self.cm.getPage(url, {'host': self.HOST}) if sts: nextPage = self.cm.ph.getDataBeetwenMarkers( data, 'page-box', '</div>', False)[1] if nextPage.find('>%d<' % (int(page) + 1)) > -1: nextPage = True else: nextPage = False sp = '<li><div class="yt-lockup' if searchType == 'playlist': m2 = '<div class="branded-page-box' else: m2 = '</ol>' data = CParsingHelper.getDataBeetwenMarkers( data, sp, m2, False)[1] data = data.split(sp) currList = self.parseListBase(data, searchType) print "nextPage", nextPage if len(currList) and nextPage: item = { 'name': 'history', 'type': 'category', 'category': nextPageCategory, 'pattern': pattern, 'search_type': searchType, 'title': _("Next page"), 'page': str(int(page) + 1) } currList.append(item) except Exception as error: printE() printD(str(error)) return [] return currList
def reqData(url, headers = {}, params = None): import requests ses = requests.Session() if params: try: print 'heades', headers res = ses.post(url, headers=headers, params=params, verify=False, timeout=5) print 'res.status_code', res.status_code if res.status_code == 200: return res.content e = res.raise_for_status() print 'error', e printD('Download error', e) return '' except requests.exceptions.RequestException as e: printD('Download error', str(e)) printE() return '' else: try: res = ses.get(url, headers=headers, verify=False, timeout=5) if res.status_code == 200: return res.content e = res.raise_for_status() print 'error', e printD('Download error', e) return '' except requests.exceptions.RequestException as e: printD('Download error', str(e)) printE() return ''
def listM3u(self, name, m3ufile, page): baseUrl = '' data = '' data = open(m3ufile).read() from m3uparser import ParseM3u data = ParseM3u(data) printD("data", data) groups = [] groups_list = [] links = [] i = 0 self.addDir("Search", m3ufile, 7, "img/search.png", "", 1) for item in data: i = i + 1 group_title = item.get('group-title', '') url = item['uri'] icon = self._getM3uIcon(item, group_title) if item['f_type'] == 'inf': if group_title != "": if group_title not in groups_list: groupIcon = item.get('group-logo', '') if groupIcon == '': groupIcon = item.get('group-art', '') if groupIcon == '': groupIcon = icon groups_list.append(group_title) groups.append((group_title, m3ufile, groupIcon, i)) else: title = item.get('title', '') url = item['uri'] icon = self._getM3uIcon(item, title) links.append((title, url, icon, i)) if len(groups) != 0: for group in groups: if "info" in group[0].lower() or "label" in group[0].lower(): continue self.addDir(group[0], group[1], 2, group[2], "", group[3]) return groups else: for link in links: self.addDir(link[0], link[1], 0, link[2], "", link[3])
def get_recommended(self, cParams): main_title = cParams.get('name', '') url = 'https://www.youtube.com/' data = self.getPage(url) printD('data', data) if data == '': return self.youtube_error('Download error') blocks = data.split('class="yt-lockup-title') i = 0 list1 = [] print 'blaocks', len(blocks) printD('blaocks', len(blocks)) for block in blocks: i = i + 1 if i == 1: continue print 'blcokd', block regx = 'href="(.*?)"' try: href = re.findall(regx, block)[0] except: continue href = href.split('v=')[1] image = 'http://img.youtube.com/vi/' + href + '/default.jpg' href = 'plugin://plugin.video.youtube/?action=play_video&videoid=' + href regx = 'title="(.*?)"' try: name = re.findall(regx, block, re.M | re.I)[0] except: continue try: name = name.encode('utf-8') except: name = str(name) self.addDir(name, href, 0, image, main_title, '', desc='', extra={})
def getMyChannel(self, cParams): list1 = [] access_token, refresh_token = self.getaccesstoken() url = 'https://www.googleapis.com/youtube/v3/channels?part=contentDetails&mine=true&access_token=' + self.access_token.strip() printD('url', url) cParams.update({'url': url}) self.getMyChannelData(cParams) for item in self.videoList: print 'item', item['mode'] name = item.get('name', '') url = item.get('url', '') mode = item.get('mode', '') image = item.get('image', '') category = item.get('category', '') page = item.get('page', '') desc = item.get('desc', '') extra = item.get('extra', {}) self.addDir(name, url, mode, image, desc=desc, page=page, link=False, category=category, maintitle=True, extra=extra)
def runm3u(self, name, url, page): PLUGIN_PATH = '/usr/lib/enigma2/python/Plugins/Extensions/TSmedia' dlocation_file = PLUGIN_PATH + "/lib/defaults/download_location" try: iptv_folder = open(dlocation_file).read() except: iptv_folder = "/media/hdd" iptv_folder = iptv_folder + "/TSmedia/iptv/" if not os.path.exists(iptv_folder): try: os.makedirs(iptv_folder) except: pass folder = iptv_folder module_path = iptv_folder success = downloadfile(url, iptv_folder + "tmp.m3u") printD("url", url) printD("success", success) if success: from m3u import listM3u listM3u(name, iptv_folder + "tmp.m3u", page, self.addDir)
def get_video_url(url = None): error = None if os.path.exists('/tmp/TSmedia/TSmedia_log'): try: os.remove('/tmp/TSmedia/TSmedia_log') except: pass video_id = get_youtube_video_id(url) ql = '2' print 'video_idxxx',video_id video_url = None try: ytdl = YouTubeVideoUrl() if ql == '0': quality = '17' elif ql == '1': quality = '5' elif ql == '2': quality = '18' elif ql == '3': quality = '22' elif ql == '4': quality = '22' elif ql == '5': quality = '37' elif ql == '6': quality = '38' video_url = ytdl.extract(video_id, quality) print 'video_urlxxx',video_url printD('error,video_url', str(video_url)) if video_url.startswith('Error'): return video_url if video_url is None: return ('Error :', ' video_url ' + str(video_url)) if video_url is not None and video_url.startswith('http') and not video_url.startswith('Error') and not video_url.strip() == '': return video_url return 'Error' except Exception as e: printE() printD('url','https://www.youtube.com/watch?v='+video_id) #addDir("Message:"+ str(e),'https://www.youtube.com/watch?v='+video_id,10,"","",1) printD('Error:' + str(e)) return 'https://www.youtube.com/watch?v='+video_id return
def getMyChannelData(self, cParams): list1 = [] main_url = cParams.get('url', '') main_title = cParams.get('name', '') page = cParams.get('page', '') main_url = main_url + uParams access_token, refresh_token = self.getaccesstoken() url_page = main_url printD('url_page', url_page) content = self.getPage(url_page) try: data = json.loads(content) totalResults = data.get('pageInfo')['totalResults'] except: printE() self.youtube_error('Please refresh login') title = 'Refresh login' url = '' mode = 20 image = 'img/signin.png' page = '' desc = '' extra = {} self.dInfo(title, url, mode, image, 'my channel', page, desc=desc, extra=extra) title = 'New login' url = '' mode = 20 image = 'img/signin.png' page = '' desc = '' self.dInfo(title, url, mode, image, 'my channel', page, desc=desc, extra=extra) return a = 0 l = len(data) if int(totalResults) < 1: addDir('No contents / results found!', '', 1, '', '') self.dInfo('No contents / results found!', '', -1, '', 'my channel', '', desc='', extra='') return for item in data.get('items', {}): kind = item['kind'] if not kind == 'youtube#channel': continue channelid = item['id'] contentDetails = item['contentDetails'] if contentDetails: favorites_id = item['contentDetails']['relatedPlaylists']['favorites'] uploads_id = item['contentDetails']['relatedPlaylists']['uploads'] watchHistory_id = item['contentDetails']['relatedPlaylists']['watchHistory'] watchLater_id = item['contentDetails']['relatedPlaylists']['watchLater'] likes_id = item['contentDetails']['relatedPlaylists']['likes'] url = 'https://www.googleapis.com/youtube/v3/playlists?part=snippet&mine=true&' + '&access_token=' + self.access_token self.dInfo('playlists', url, 100, 'img/playlists.png', main_title, page, desc='', extra={}) url = 'https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&mine=true&' + '&access_token=' + self.access_token self.dInfo('Subscriptions', url, 100, 'img/subscriptions.png', main_title, page, desc='', extra={}) url = 'https://www.googleapis.com/youtube/v3/activities?part=snippet,id,contentDetails&home=true&forMine=true' + '&access_token=' + self.access_token self.dInfo('Activities', url, 100, 'img/activities.png', main_title, page, desc='', extra={}) url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails,status&playlistId=' + favorites_id + '&mine=true' + '&access_token=' + self.access_token self.dInfo('Favorites', url, 100, 'img/favorites.png', main_title, page, desc='', extra={}) url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails,status&playlistId=' + uploads_id + '&mine=true' + '&access_token=' + self.access_token self.dInfo('My uploads', url, 100, 'img/myuploads.png', main_title, page, desc='', extra={}) url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails,status&playlistId=' + likes_id + '&mine=true' + '&access_token=' + self.access_token self.dInfo('likes', url, 108, 'img/likes.png', main_title, page, desc='', extra={}) url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails,status&playlistId=' + watchLater_id + '&mine=true' + '&access_token=' + self.access_token self.dInfo('WatchLater', url, 100, 'img/watch_later.png', main_title, page, desc='', extra={}) url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails,status&playlistId=' + watchLater_id + '&mine=true' + '&access_token=' + self.access_token self.dInfo('Sign out', url, 106, 'img/signout.png', main_title, page, desc='', extra={})
def getviedoList(self, cParams): default_params = getSettings() uParams = '&maxResults=45' + default_params + '&key=' + KEYV3 uParams2 = uParams.replace('order=viewCount', 'order=relevance') main_title = cParams.get('title', '') main_url = cParams.get('url', '') if 'subscriptions' in main_url: main_url = main_url + uParams2 elif 'key=' not in main_url: main_url = main_url + uParams printD('mainu url', main_url) page = cParams.get('page', '') main_mode = cParams.get('mode', 0) #main_url = main_url + uParams if page == '': url_page = main_url else: url_page = main_url + '&pageToken=' + str(page).strip() print 'url_page', url_page content = reqData(url_page, self.HEADER) if content.strip() == '': return self.youtube_error('download error') try: data = json.loads(content, strict=False) except: printE() c4_browse_ajax = str(data.get('nextPageToken', '')) page = c4_browse_ajax a = 0 try: TotalResults = int(data.get('pageInfo', 0)['totalResults']) except: TotalResults = 0 try: resultsPerPage = int(data.get('pageInfo', 0)['resultsPerPage']) except: resultsPerPage = 0 etag = data.get('etag', 'none') extra = {'TotalResults': TotalResults, 'resultsperpage': resultsPerPage, 'etag': etag} if TotalResults < 1: return self.youtube_error('download error-No contents / results found!') list_item = 'ItemList' in data['kind'] for item in data.get('items', {}): try: if not list_item: try: kind = item['id']['kind'] except: kind = item['kind'] else: kind = item['kind'] if kind: title = item['snippet']['title'].encode('utf-8') channelId = item['snippet'].get('channelId', '') channelTitle = item['snippet'].get('channelTitle', '') extra.update({'channelId': channelId, 'channelTitle': channelTitle}) desc = item['snippet']['description'].encode('utf-8', 'ignore').replace('&', '_').replace(';', '') if kind.endswith('#video'): try: url = str(item['id']['videoId']) img = str(item['snippet']['thumbnails']['default']['url']) print 'url', url stream_link = 'plugin://plugin.video.youtube/?action=play_video&videoid=%s' % url if enigmaos == 'oe2.0': title = '\\c0??????? ' + title self.dInfo(title, stream_link, 0, img, main_title, page, desc=desc, extra=extra) except: continue elif kind.endswith('#playlistItem'): try: url = str(item['snippet']['resourceId']['videoId']) img = str(item['snippet']['thumbnails']['default']['url']) url = 'plugin://plugin.video.youtube/?action=play_video&videoid=%s' % url if enigmaos == 'oe2.0': title = '\\c0??????? ' + title self.dInfo(title, url, 0, img, main_title, page, desc=desc, extra=extra) except: pass elif kind.endswith('#channel'): url = str(item['id']['channelId']) try: img = str(item['snippet']['thumbnails']['default']['url']) except: img = '' if enigmaos == 'oe2.0': title = '\\c0000???? ' + title self.dInfo(title, url, 12, img, main_title, page, desc=desc, extra=extra) elif kind.endswith('#playlist'): try: url = str(item['id']['playlistId']) except: url = str(item['id']) try: img = str(item['snippet']['thumbnails']['default']['url']) except: img = '' if enigmaos == 'oe2.0': title = '\\c0000??00 ' + title url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=' + url self.dInfo(title, url, 100, img, main_title, page, desc=desc, extra=extra) elif kind.endswith('#activity'): try: url = url = str(item['snippet']['channelId']) except: url = str(item['id']) try: img = str(item['snippet']['thumbnails']['default']['url']) except: img = '' if enigmaos == 'oe2.0': title = '\\c0000???? ' + title self.dInfo(title, url, 12, img, main_title, page, desc=desc, extra=extra) except: printE() if not c4_browse_ajax == '': self.dInfo('More', main_url, 100, 'img/next.png', main_title, page, desc='', extra={}, dialog='nextpage')
def run(self, cParams = None): if not cParams: params = self.get_params() else: params = cParams url = params.get('url', '') name = params.get('name', '') try: mode = int(params.get('mode', None)) except: mode = None page = str(params.get('page', '')) section = params.get('section', '') extra = params.get('extra', {}) try: extra = ast.literal_eval(extra) except: extra = {} show = params.get('show', '') image = params.get('image', '') print 'Mode: ' + str(mode) print 'URL: ' + str(url) print 'Name: ' + str(name) print 'page: ' + str(page) print 'section: ' + str(section) print 'extra: ' + str(extra) print 'show: ' + str(show) print 'image: ' + str(image) cParams = params printD('input Parameters', cParams) if mode == None: print '' self.showmenu() elif mode == 103: sterm = cParams.get('strem', '') printD('sterm', sterm) self.search(cParams) elif mode == 11: print '' + url self.getMainCats(cParams) elif mode == 12: print '' + url self.getChannelsMenu(cParams) elif mode == 13: print '' + url self.getMyChannel(cParams) elif mode == 14: print '' + url self.getbestyoutube(cParams) elif mode == 15: print '' + url self.getrecommended(cParams) elif mode == 16: print '' + url self.getUserlists(cParams) elif mode == 17: self.getRecommeded(cParams) elif mode == 18: self.getUserPlaylists(cParams) elif mode == 19: self.getUserChannels(cParams) elif mode == 20: self.getUserVideos(cParams) elif mode == 100: print '' + url self.getVideosList(cParams) return self.endDir()