Skip to content

Commit

Permalink
made videotop more stable
Browse files Browse the repository at this point in the history
  • Loading branch information
intnull committed Aug 25, 2011
1 parent 03d4966 commit 0101232
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 25 deletions.
81 changes: 64 additions & 17 deletions videotop.py
Expand Up @@ -7,6 +7,10 @@
import youtube_client


import socket # socket.gaierror
import subprocess # subprocess.CalledProcessError


class VideoButton(urwid.FlowWidget):
clicked_buttons = []

Expand Down Expand Up @@ -53,20 +57,33 @@ def keypress(self, size, key):
if key == 'enter':
self.display_widget.set_attr_map({None: 'downloaded'})
status_bar.set_text(' Downloading: "' + self.video.title + '"')
self.video.download()
VideoButton.clicked_buttons.append(self)
try:
self.video.download()
VideoButton.clicked_buttons.append(self)
except AttributeError:
status_bar.set_text(' Local videos do not support redownloading yet')
elif key == 'o':
self.display_widget.set_attr_map({None: 'opened'})
status_bar.set_text(' Opening in browser: "' + self.video.title + '"')
self.video.open()
try:
self.video.open()
except AttributeError:
status_bar.set_text(' Local videos do not support opening their YouTube site yet')
elif key == 'p':
status_bar.set_text(' Playing: "' + self.video.title + '"')
self.video.play()
if not self.video.play():
status_bar.set_text(' You must download "' + self.video.title + '"' + ' first before you can play it')
elif key == 's':
self.display_widget.set_attr_map({None: 'streamed'})
status_bar.set_text(' Streaming: "' + self.video.title + '"')
loop.draw_screen()
self.video.stream()
try:
self.video.stream()
except AttributeError:
status_bar.set_text(' Local videos do not support streaming yet')
except subprocess.CalledProcessError:
status_bar.set_text(' You probably lost your internet connection')
main_frame.set_focus('body')
elif key == 'a':
status_bar.set_text(' ' + self.video.abort())
else:
Expand Down Expand Up @@ -117,6 +134,9 @@ def keypress(self, size, key):
status_bar.set_text(' Please also enter what to search for')
loop.draw_screen()
main_frame.set_focus('body')
except socket.gaierror:
status_bar.set_text(' You probably lost your internet connection')
main_frame.set_focus('body')
elif command[0] in ('videos', 'v'):
self.clear()
status_bar.set_text(' Listing downloaded videos')
Expand Down Expand Up @@ -189,6 +209,8 @@ def search(self, pattern):
self.set_focus(first_result)
except:
status_bar.set_text(' Error, could not find pattern "' + pattern + '"')
self.latest_search = None
self.latest_search_position = None
main_frame.set_focus('body')

def keypress(self, size, key):
Expand All @@ -203,36 +225,61 @@ def keypress(self, size, key):
elif key == 'k':
self.listbox.keypress(size, 'up')
elif key == 'g':
self.listbox.change_focus(size, 0)
try:
self.listbox.change_focus(size, 0)
except AttributeError:
pass
elif key == 'G':
self.listbox.change_focus(size, len(self.body) - 1)
try:
self.listbox.change_focus(size, len(self.body) - 1)
except AttributeError:
pass
elif key == 'ctrl d':
position = self.listbox.get_focus()[1] + 5
self.listbox.set_focus(position, 'above')
try:
position = self.listbox.get_focus()[1] + 5
self.listbox.set_focus(position, 'above')
except TypeError:
pass
elif key == 'ctrl u':
position = self.listbox.get_focus()[1] - 5
if position < 0:
position = 0
self.listbox.set_focus(position, 'below')
try:
position = self.listbox.get_focus()[1] - 5
if position < 0:
position = 0
self.listbox.set_focus(position, 'below')
except TypeError:
pass
elif key == 'ctrl r':
self.clear()
elif key == 'ctrl n':
search = client.next_page()
self.append(search)
try:
status_bar.set_text(' Listing next videos please wait...')
loop.draw_screen()
search = client.next_page()
status_bar.set_text('')
self.append(search)
except TypeError:
status_bar.set_text(' You need to search for something before you do that!')
except socket.gaierror:
status_bar.set_text(' You probably lost your internet connection')
main_frame.set_focus('body')
elif key == 'n':
try:
self.latest_search_position += 1
self.listbox.set_focus(self.latest_search[self.latest_search_position])
except IndexError:
self.latest_search_position = 0
self.listbox.set_focus(self.latest_search[self.latest_search_position])
except TypeError:
pass
elif key == 'N':
try:
self.latest_search_position -= 1
self.listbox.set_focus(self.latest_search[self.latest_search_position])
except IndexError:
self.latest_search_position = len(self.latest_search) - 1
self.listbox.set_focus(self.latest_search[self.latest_search_position])
except TypeError:
pass
else:
return self.listbox.keypress(size, key)

Expand All @@ -250,7 +297,7 @@ def update(main_loop, user_data):


def main():
# create the download directory if it doesn't exist already
# create the download directory if it doesn't already exist
home_dir = os.environ['HOME']
download_dir = os.path.join(home_dir, '.videotop/videos')
try:
Expand All @@ -260,7 +307,7 @@ def main():
pass
else:
raise
# change to download directory
# change working directory to download directory
os.chdir(download_dir)

palette = [('focus', 'light red', 'black', 'standout'),
Expand Down
21 changes: 13 additions & 8 deletions youtube_client.py
@@ -1,12 +1,13 @@
import gdata.youtube
import gdata.youtube.service
import webbrowser
import tempfile
import subprocess
import locale
import download_thread
import os

locale.setlocale(locale.LC_ALL, 'en_US')

locale.setlocale(locale.LC_ALL, '')


class YouTubeClient:
Expand Down Expand Up @@ -107,14 +108,18 @@ def get_formatted_views(self):
return self.views

def play(self):
temp = tempfile.TemporaryFile()
devnull = open(os.devnull)
extensions = ['.flv', '.mp4', '.webm']
for ext in extensions:
file = self.filename + ext
subprocess.Popen(['mplayer', '-msglevel', 'all=-1', '-use-filename-title', file], stdin=temp)
if os.path.exists(file):
subprocess.Popen(['mplayer', '-msglevel', 'all=-1', '-use-filename-title', file], stdin=devnull)
return True
return False

def stream(self):
title = self.title.replace('"', '\'')
cmd = 'mplayer -title "%s" -prefer-ipv4 -cookies -cookies-file /tmp/videotop_cookie \
$(youtube-dl --get-url --max-quality=34 --cookies=/tmp/videotop_cookie "%s")' % (title, self.url)
subprocess.call(cmd, shell=True)
cookie = '/tmp/videotop_cookie'
c1 = ['youtube-dl', '--get-url', '--max-quality=34', '--cookies=' + cookie, self.url]
stream = subprocess.check_output(c1).strip()
c2 = ['mplayer', '-title', self.title, '-prefer-ipv4', '-cookies', '-cookies-file', cookie, stream]
subprocess.call(c2)

0 comments on commit 0101232

Please sign in to comment.