Adjusted source to meat PEP8
This commit is contained in:
149
play.py
149
play.py
@@ -1,21 +1,24 @@
|
||||
#!/usr/bin/python3
|
||||
#import cProfile
|
||||
#import re
|
||||
# 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,7 +47,7 @@ class Mc(object):
|
||||
channel_id_filter = 0
|
||||
total_videos = 0
|
||||
search = ''
|
||||
|
||||
subprocess = False
|
||||
|
||||
def __init__(self):
|
||||
self.read_data()
|
||||
@@ -56,13 +59,16 @@ class Mc(object):
|
||||
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:
|
||||
cur = self.connection.cursor()
|
||||
@@ -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']))
|
||||
@@ -84,40 +90,46 @@ class Mc(object):
|
||||
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(','):
|
||||
@@ -132,22 +144,18 @@ class Mc(object):
|
||||
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:
|
||||
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']))
|
||||
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
|
||||
@@ -209,7 +205,7 @@ class Mc(object):
|
||||
|
||||
elif key == curses.KEY_LEFT:
|
||||
stdscr.clear()
|
||||
if(self.offset > 30):
|
||||
if self.offset > 30:
|
||||
self.offset -= 30
|
||||
else:
|
||||
self.offset = 0
|
||||
@@ -227,7 +223,7 @@ 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(0, 0, 'Search channel:')
|
||||
stdscr.addstr(1, 0, '+-------------------------------------------------+')
|
||||
stdscr.addstr(2, 0, '| |')
|
||||
stdscr.addstr(3, 0, '+-------------------------------------------------+')
|
||||
@@ -235,21 +231,16 @@ class Mc(object):
|
||||
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)
|
||||
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)
|
||||
box.edit()
|
||||
@@ -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)
|
||||
@@ -284,33 +273,32 @@ class Mc(object):
|
||||
os._exit(0)
|
||||
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)
|
||||
|
||||
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.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" ))
|
||||
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)
|
||||
|
||||
|
||||
23
youtube.py
23
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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user