From 5a397e48917a6307ed328a66887366115f1a5855 Mon Sep 17 00:00:00 2001 From: krzysiej Date: Sun, 16 Jul 2017 22:50:37 +0200 Subject: [PATCH] Adjusted source to meat PEP8 --- play.py | 201 +++++++++++++++++++++++++---------------------------- youtube.py | 27 ++++--- 2 files changed, 106 insertions(+), 122 deletions(-) diff --git a/play.py b/play.py index 9863361..db76051 100644 --- a/play.py +++ b/play.py @@ -1,21 +1,24 @@ #!/usr/bin/python3 -#import cProfile -#import re -import requests +# import cProfile +# import re +import requests from datetime import datetime -#import calendar +# import calendar import os from html import unescape import urllib.parse -#import time +# import time +from subprocess import Popen, PIPE, DEVNULL import curses import sqlite3 as lite import youtube -#import os.path -#import math +# import os.path +# import math from curses.textpad import Textbox, rectangle -#527 biale z podkreśleniem 520 szare z podkresleniem -#https://www.googleapis.com/youtube/v3/search?key=AIzaSyBwecAq2aCjRxStpOclQZNSRpJEI2HtvX0&part=snippet&type=channel&q=etho&maxResults=10 + + +# 527 biale z podkreśleniem 520 szare z podkresleniem +# https://www.googleapis.com/youtube/v3/search?key=AIzaSyBwecAq2aCjRxStpOclQZNSRpJEI2HtvX0&part=snippet&type=channel&q=etho&maxResults=10 def get_video_id(url): url_data = urllib.parse.urlparse(url) @@ -26,9 +29,9 @@ def get_video_id(url): def get_valid_filename(string): return "".join([c for c in string if c.isalpha() or c.isdigit() or c == ' ']).rstrip().replace(' ', '_') -def file_exists(video_title): - return os.path.isfile("/public/youtube/" + get_valid_filename(video_title)+".mp4") +def file_exists(video_title): + return os.path.isfile("/public/youtube/" + get_valid_filename(video_title) + ".mp4") class Mc(object): @@ -44,24 +47,27 @@ class Mc(object): channel_id_filter = 0 total_videos = 0 search = '' - + subprocess = False def __init__(self): self.read_data() self.total_videos = self.get_total_videos() - + def read_data(self): with self.connection: cur = self.connection.cursor() cur.execute('SELECT id, nick, uploadsid FROM channel') self.youtube_channels = cur.fetchall() if self.channel_id_filter > 0: - cur.execute("SELECT nick, title, videoid, channel_id, seen FROM video JOIN channel ON video.channel_id = channel.id WHERE deleted IS NULL AND channel.id = ? AND title LIKE ? ORDER BY publishtime DESC LIMIT ?, 30", (self.channel_id_filter, '%'+self.search+'%', self.offset)) + cur.execute( + "SELECT nick, title, videoid, channel_id, seen FROM video JOIN channel ON video.channel_id = channel.id WHERE deleted IS NULL AND channel.id = ? AND title LIKE ? ORDER BY publishtime DESC LIMIT ?, 30", + (self.channel_id_filter, '%' + self.search + '%', self.offset)) else: - cur.execute("SELECT nick, title, videoid, channel_id, seen FROM video JOIN channel ON video.channel_id = channel.id WHERE deleted IS NULL AND title LIKE ? ORDER BY publishtime DESC LIMIT ?, 30", ('%'+self.search+'%', self.offset)) - + cur.execute( + "SELECT nick, title, videoid, channel_id, seen FROM video JOIN channel ON video.channel_id = channel.id WHERE deleted IS NULL AND title LIKE ? ORDER BY publishtime DESC LIMIT ?, 30", + ('%' + self.search + '%', self.offset)) + self.videos = cur.fetchall() - def lite(self, channels): with self.connection: @@ -70,7 +76,7 @@ class Mc(object): cur.execute("SELECT * FROM channel WHERE nick = :nick", {"nick": channel['nick']}) self.connection.commit() row = cur.fetchone() - if row == None: + if row is None: cur.execute( "INSERT INTO channel('playlist', 'nick', 'channel', 'userid', 'uploadsid') VALUES (?, ?, ?, ?, ?)", ('1', channel['nick'], channel['channel'], channel['userid'], channel['uploadsid'])) @@ -80,44 +86,50 @@ class Mc(object): return self.youtube_info_url + video_id def print_videos(self, stdscr): - iterator = 0 - for video in self.videos: - is_file = "X" if file_exists(video[1]) else " " + iterator = 0 + for video in self.videos: + is_file = "X" if file_exists(video[1]) else " " if iterator == self.selected_video: - stdscr.addstr(iterator, 0, "[%s] %s %-60s" % (video[0].ljust(14, ' '), is_file, video[1]), curses.A_STANDOUT) + stdscr.addstr(iterator, 0, "[%s] %s %-60s" % (video[0].ljust(14, ' '), is_file, video[1]), + curses.A_STANDOUT) elif video[4] == 1: - stdscr.addstr(iterator, 0, "[%s] %s %-60s" % (video[0].ljust(14, ' '), is_file, video[1]), curses.color_pair(236)) - #stdscr.addstr(iterator, 0, "[%s] %s %s %-60s" % (video[0].ljust(14, ' '), is_file, video[1], iterator+self.offset), curses.color_pair(iterator+self.offset)) + stdscr.addstr(iterator, 0, "[%s] %s %-60s" % (video[0].ljust(14, ' '), is_file, video[1]), + curses.color_pair(236)) + # stdscr.addstr(iterator, 0, "[%s] %s %s %-60s" % (video[0].ljust(14, ' '), is_file, video[1], iterator+self.offset), curses.color_pair(iterator+self.offset)) else: - #stdscr.addstr(iterator, 0, "[%s] %s %s %-60s" % (video[0].ljust(14, ' '), is_file, video[1], iterator+self.offset), curses.color_pair(iterator+self.offset)) - stdscr.addstr(iterator, 0, "[%s] %s %-60s" % (video[0].ljust(14, ' '), is_file, video[1]), curses.color_pair(255)) + # stdscr.addstr(iterator, 0, "[%s] %s %s %-60s" % (video[0].ljust(14, ' '), is_file, video[1], iterator+self.offset), curses.color_pair(iterator+self.offset)) + stdscr.addstr(iterator, 0, "[%s] %s %-60s" % (video[0].ljust(14, ' '), is_file, video[1]), + curses.color_pair(255)) iterator += 1 def print_botom_info(self, stdscr): pages = int(self.total_videos) // 30 - stdscr.addstr(len(self.videos), 0, "Play [Enter] | Download [D] | Refresh [R] | Exit [Q] |", curses.A_BOLD ) + stdscr.addstr(len(self.videos), 0, "Play [Enter] | Download [D] | Refresh [R] | Exit [Q] |", curses.A_BOLD) if_search = curses.color_pair(90) if len(self.search) > 0 else curses.A_BOLD if_filter = curses.color_pair(90) if self.channel_id_filter > 0 else curses.A_BOLD stdscr.addstr(len(self.videos), 55, "Search [S]", if_search) stdscr.addstr(len(self.videos), 65, " | ", curses.A_BOLD) - stdscr.addstr(len(self.videos), 68, "Filter [F]" , if_filter) - stdscr.addstr(len(self.videos), 78, " | " , curses.A_BOLD) - stdscr.addstr(len(self.videos), 81, "[<-/->] | [%d / %d] " % (self.offset /30+1, pages), curses.A_BOLD) - + stdscr.addstr(len(self.videos), 68, "Filter [F]", if_filter) + stdscr.addstr(len(self.videos), 78, " | ", curses.A_BOLD) + stdscr.addstr(len(self.videos), 81, "[<-/->] | [%d / %d] " % (self.offset / 30 + 1, pages), curses.A_BOLD) + def get_total_videos(self): with self.connection: cur = self.connection.cursor() if self.channel_id_filter > 0: - cur.execute("SELECT count(video.id) FROM video JOIN channel ON video.channel_id = channel.id AND channel.id = ? WHERE title like ?", (self.channel_id_filter, '%'+self.search+'%')) + cur.execute( + "SELECT count(video.id) FROM video JOIN channel ON video.channel_id = channel.id AND channel.id = ? WHERE title like ?", + (self.channel_id_filter, '%' + self.search + '%')) else: - cur.execute("SELECT count(video.id) FROM video JOIN channel ON video.channel_id = channel.id WHERE title LIKE ?", ('%'+self.search+'%',)) + cur.execute( + "SELECT count(video.id) FROM video JOIN channel ON video.channel_id = channel.id WHERE title LIKE ?", + ('%' + self.search + '%',)) return int(cur.fetchone()[0]) def get_video_direct_urls(self, video_id): streams = [] data = requests.get(self.get_video_info_url(video_id)) - unescaped = unescape(data.text) decoded = urllib.parse.parse_qs(data.text) for stream in decoded['url_encoded_fmt_stream_map'][0].split(','): @@ -127,27 +139,23 @@ class Mc(object): def get_all_videos(self): for id, channelName, uploadListId in self.youtube_channels: - + videos = youtube.playlistItems(uploadListId, self.youtube_api_key, True) for row in videos: self.save_video(id, row) - def get_recent_videos_silent(self, allVideos=False): - for id, channelName, uploadListId in self.youtube_channels: + for id, channelName, uploadListId in self.youtube_channels: videos = youtube.playlistItems(uploadListId, self.youtube_api_key, allVideos) for row in videos: self.save_video(id, row) - - - def get_recent_videos(self, stdscr): channel_iterator = 1 for id, channelName, uploadListId in self.youtube_channels: - stdscr.addstr(channel_iterator, 0, '%s %d/%d' % (channelName,0,50)) - channel_iterator+=1 + stdscr.addstr(channel_iterator, 0, '%s %d/%d' % (channelName, 0, 50)) + channel_iterator += 1 channel_iterator = 1 for id, channelName, uploadListId in self.youtube_channels: @@ -155,43 +163,31 @@ class Mc(object): videos_iterator = 1 for row in videos: self.save_video(id, row) - stdscr.addstr(channel_iterator, 0, '%s %d/%d' % (channelName,videos_iterator, len(videos))) + stdscr.addstr(channel_iterator, 0, '%s %d/%d' % (channelName, videos_iterator, len(videos))) stdscr.refresh() - videos_iterator+=1 - channel_iterator+=1 - - + videos_iterator += 1 + channel_iterator += 1 def save_video(self, channel_id, video_row): with self.connection: cur = self.connection.cursor() - cur.execute( - "INSERT OR IGNORE INTO video('channel_id', 'title', 'views', 'duration', 'videoid', 'publishtime') VALUES (?, ?, ?, ?, ?, ?)", - (channel_id, video_row['title'], 0, 0, video_row['videoid'], video_row['timestamp'])) + "INSERT OR IGNORE INTO video('channel_id', 'title', 'views', 'duration', 'videoid', 'publishtime') VALUES (?, ?, ?, ?, ?, ?)", + (channel_id, video_row['title'], 0, 0, video_row['videoid'], video_row['timestamp'])) self.connection.commit() - def main_loop(self, stdscr): stdscr.keypad(True) stdscr.clear() - - curses.use_default_colors() - for i in range(0, curses.COLORS): curses.init_pair(i, i, -1); - self.print_videos(stdscr) mc.print_botom_info(stdscr) curses.curs_set(0) - - while 1: stdscr.refresh() - key = stdscr.getch() - if key == curses.KEY_UP: stdscr.refresh() mc.selected_video -= 1 @@ -206,10 +202,10 @@ class Mc(object): stdscr.clear() self.offset += 30; self.read_data() - + elif key == curses.KEY_LEFT: stdscr.clear() - if(self.offset > 30): + if self.offset > 30: self.offset -= 30 else: self.offset = 0 @@ -227,28 +223,23 @@ class Mc(object): elif key in [ord('A'), ord('a')]: stdscr.clear() editwin = curses.newwin(1, 49, 2, 1) - stdscr.addstr(0,0,'Search channel:') - stdscr.addstr(1, 0, '+-------------------------------------------------+') - stdscr.addstr(2, 0, '| |') - stdscr.addstr(3, 0, '+-------------------------------------------------+') + stdscr.addstr(0, 0, 'Search channel:') + stdscr.addstr(1, 0, '+-------------------------------------------------+') + stdscr.addstr(2, 0, '| |') + stdscr.addstr(3, 0, '+-------------------------------------------------+') stdscr.refresh() box = Textbox(editwin) box.edit() - self.searchChannel = box.gather() - - - - + elif key in [ord('S'), ord('s')]: stdscr.clear() - editwin = curses.newwin(1,49, 2,1) - stdscr.addstr(0, 0, 'SEARCH BY TITLE:') - stdscr.addstr(1, 0, '+-------------------------------------------------+') - stdscr.addstr(2, 0, '| |') - stdscr.addstr(3, 0, '+-------------------------------------------------+') - + editwin = curses.newwin(1, 49, 2, 1) + stdscr.addstr(0, 0, 'SEARCH BY TITLE:') + stdscr.addstr(1, 0, '+-------------------------------------------------+') + stdscr.addstr(2, 0, '| |') + stdscr.addstr(3, 0, '+-------------------------------------------------+') stdscr.refresh() box = Textbox(editwin) @@ -260,7 +251,6 @@ class Mc(object): stdscr.clear() - elif key in [ord('R'), ord('r')]: stdscr.clear() @@ -269,7 +259,6 @@ class Mc(object): self.read_data() self.total_videos = self.get_total_videos() - elif key in [ord('D'), ord('d')]: video_id = mc.videos[mc.selected_video][2] streams = mc.get_video_direct_urls(video_id) @@ -279,38 +268,37 @@ class Mc(object): os.system('wget -nv --show-progress -O "/public/youtube/%s.mp4" "%s" >> /dev/null &' % ( get_valid_filename(mc.videos[mc.selected_video][1]), stream['url'][0])) - elif key in [ord('Q'), ord('q')]: # esc + elif key in [ord('Q'), ord('q')]: # esc curses.endwin() os._exit(0) - elif key == 10 or key == curses.KEY_ENTER: + elif key == 10 or key == curses.KEY_ENTER: video_id = mc.videos[mc.selected_video][2] - #stdscr.clear() - #stdscr.addstr(0, 0, '/public/youtube/' + get_valid_filename(mc.videos[mc.selected_video][1])+".mp4") + # stdscr.clear() + # stdscr.addstr(0, 0, '/public/youtube/' + get_valid_filename(mc.videos[mc.selected_video][1])+".mp4") - # stdscr.refresh() - # time.sleep(4) + # stdscr.refresh() + # time.sleep(4) with self.connection: cur = self.connection.cursor() - cur.execute('UPDATE video SET seen = 1 WHERE videoid = ?', (video_id, )) + cur.execute('UPDATE video SET seen = 1 WHERE videoid = ?', (video_id,)) self.youtube_channels = cur.fetchall() self.connection.commit() - - - if file_exists(mc.videos[mc.selected_video][1]): - - # stdscr.addstr(1, 1, '/public/youtube/' + get_valid_filename(mc.videos[mc.selected_video][1])+".mp4") - # stdscr.refresh() - os.system('omxplayer -b "%s"' % ("/public/youtube/" + get_valid_filename(mc.videos[mc.selected_video][1])+".mp4" )) + #stdscr.addstr(1, 1, '/public/youtube/' + get_valid_filename(mc.videos[mc.selected_video][1])+".mp4") + # stdscr.refresh() + os.system('omxplayer -b "%s"' % ( + "/public/youtube/" + get_valid_filename(mc.videos[mc.selected_video][1]) + ".mp4")) else: streams = mc.get_video_direct_urls(video_id) - + for stream in streams: # print (stream['itag']) if stream['itag'][0] == "22": - os.system('omxplayer -b "%s"' % stream['url'][0]) + # os.system('omxplayer -b "%s"' % stream['url'][0]) + self.subprocess = Popen(['omxplayer', '-b', stream['url'][0]], stdin=PIPE, + stdout=DEVNULL, close_fds=True, bufsize=0) stdscr.clear() mc.print_videos(stdscr) @@ -320,27 +308,26 @@ class Mc(object): if __name__ == "__main__": mc = Mc() -#test('test') + # test('test') -#print (youtube.search('generikb', 'channel', "AIzaSyBwecAq2aCjRxStpOclQZNSRpJEI2HtvX0")) + # print (youtube.search('generikb', 'channel', "AIzaSyBwecAq2aCjRxStpOclQZNSRpJEI2HtvX0")) -#videos = youtube.playlistItems("UU-_VTaWqRsZ1nzZLHQIGwQA", "AIzaSyBwecAq2aCjRxStpOclQZNSRpJEI2HtvX0") -#print(videos) -#mc.get_all_videos() + # videos = youtube.playlistItems("UU-_VTaWqRsZ1nzZLHQIGwQA", "AIzaSyBwecAq2aCjRxStpOclQZNSRpJEI2HtvX0") + # print(videos) + # mc.get_all_videos() -#mc.lite(channels) + # mc.lite(channels) -# print (mc.get_video_info_url('https://www.youtube.com/watch?v=vw61gCe2oqI')) + # print (mc.get_video_info_url('https://www.youtube.com/watch?v=vw61gCe2oqI')) -# mc.get_videos('UUFKDEp9si4RmHFWJW1vYsMA') + # mc.get_videos('UUFKDEp9si4RmHFWJW1vYsMA') -# mc.get_videos('UUJTWU5K7kl9EE109HBeoldA') -# os.system('clear') -# + # mc.get_videos('UUJTWU5K7kl9EE109HBeoldA') + # os.system('clear') + # try: curses.wrapper(mc.main_loop) - #cProfile.run('re.compile("foo|bar")') + # cProfile.run('re.compile("foo|bar")') except KeyboardInterrupt: curses.endwin() os._exit(0) - diff --git a/youtube.py b/youtube.py index 43bff4b..1d033cf 100644 --- a/youtube.py +++ b/youtube.py @@ -2,34 +2,35 @@ import requests from datetime import datetime import calendar + def search(what, type, api_key): url = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=%s&type=%s&key=%s" % (what, type, api_key) r = requests.get(url) return r.json() -def playlistItems(playlistId, api_key, all=False, nextToken=None): + +def playlistItems(playlistid, api_key, allmovies=False, nexttoken=None): videos = [] info_url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=%s&key=%s" % ( - playlistId, + playlistid, api_key ) - if nextToken is not None: - info_url += "&pageToken=%s" % (nextToken) + if nexttoken is not None: + info_url += "&pageToken=%s" % (nexttoken) request_data = requests.get(info_url) request_data_json = request_data.json() - videos += parseRequestData(request_data_json) - if all == True and 'nextPageToken' in request_data_json: - videos += playlistItems(playlistId, api_key, request_data_json['nextPageToken']) + videos += parse_request_data(request_data_json) + if allmovies is True and 'nextPageToken' in request_data_json: + videos += playlistItems(playlistid, api_key, request_data_json['nextPageToken']) return videos - - -def parseRequestData(request_data_json): + +def parse_request_data(request_data_json): parsed_videos = [] for element in request_data_json['items']: video = {} @@ -41,8 +42,4 @@ def parseRequestData(request_data_json): video['channel_id'] = element['snippet']['channelId'] video['nick'] = element['snippet']['channelTitle'] parsed_videos.append(video) - - return parsed_videos - - - \ No newline at end of file + return parsed_videos