Adjusted source to meat PEP8
This commit is contained in:
73
play.py
73
play.py
@@ -8,12 +8,15 @@ import os
|
||||
from html import unescape
|
||||
import urllib.parse
|
||||
# import time
|
||||
from subprocess import Popen, PIPE, DEVNULL
|
||||
import curses
|
||||
import sqlite3 as lite
|
||||
import youtube
|
||||
# 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
|
||||
|
||||
@@ -26,11 +29,11 @@ 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")
|
||||
|
||||
|
||||
|
||||
class Mc(object):
|
||||
youtube_info_url = 'http://youtube.com/get_video_info?video_id='
|
||||
youtube_channels = []
|
||||
@@ -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,14 +90,17 @@ 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 %-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 %-60s" % (video[0].ljust(14, ' '), is_file, video[1]),
|
||||
curses.color_pair(255))
|
||||
iterator += 1
|
||||
|
||||
def print_botom_info(self, stdscr):
|
||||
@@ -109,15 +118,18 @@ class Mc(object):
|
||||
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,16 +144,12 @@ 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
|
||||
|
||||
@@ -160,38 +168,26 @@ class Mc(object):
|
||||
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
|
||||
@@ -235,12 +231,8 @@ 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)
|
||||
@@ -249,7 +241,6 @@ class Mc(object):
|
||||
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)
|
||||
@@ -296,21 +285,20 @@ class Mc(object):
|
||||
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" ))
|
||||
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)
|
||||
@@ -343,4 +331,3 @@ if __name__ == "__main__":
|
||||
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