Skip to content

Commit

Permalink
Fixed downloads in guicavane. Supports the new api
Browse files Browse the repository at this point in the history
TODO: the download filename must contain the quality or the resume
feature will produce corrupted files
  • Loading branch information
j0hn committed Dec 1, 2011
1 parent 4daddfa commit 012d57b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 46 deletions.
4 changes: 2 additions & 2 deletions guicavane/Hosts/Cuevana/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, id, name, number, season, show, url):
self.__hosts = {}

def get_subtitle_url(self, lang="ES", quality=None):
if quality:
if quality and quality != "360":
return urls.sub_show_quality % (self.id, lang, quality)

return urls.sub_show % (self.id, lang)
Expand Down Expand Up @@ -172,7 +172,7 @@ def __init__(self, id, name, url):
self.__hosts = {}

def get_subtitle_url(self, lang="ES", quality=None):
if quality:
if quality and quality != "360":
return urls.sub_movie_quality % (self.id, lang, quality)

return urls.sub_movie % (self.id, lang)
Expand Down
6 changes: 3 additions & 3 deletions guicavane/Hosts/Freevana/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
url_open = UrlOpen()


class Episode(object):
class Episode(BaseEpisode):
_query_sources = "SELECT source, url FROM series_episode_sources " \
"WHERE definition = '360' AND series_episode_id = '%s'"

Expand Down Expand Up @@ -67,7 +67,7 @@ def file_hosts(self):
return self.__hosts

def get_subtitle_url(self, lang="ES", quality=None):
if quality:
if quality and quality != "360":
return sub_show_quality % (self.id, lang, quality)

return sub_show % (self.id, lang)
Expand Down Expand Up @@ -135,7 +135,7 @@ def __init__(self, id, name):
self.__hosts = {}

def get_subtitle_url(self, lang="ES", quality=None):
if quality:
if quality and quality != "360":
return sub_movie_quality % (self.id, lang, quality)

return sub_movie % (self.id, lang)
Expand Down
108 changes: 67 additions & 41 deletions guicavane/Player.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import subprocess

import Downloaders
import Hosts
from Constants import HOSTS_GUI_FILE, HOSTS_VIEW_COLUMN_OBJECT
from Hosts.Base import BaseMovie, BaseEpisode
from guicavane.Utils.UrlOpen import UrlOpen
from Constants import HOSTS_GUI_FILE, HOSTS_VIEW_COLUMN_OBJECT, \
HOSTS_VIEW_COLUMN_TEXT


class Player(object):
Expand All @@ -27,11 +29,12 @@ def __init__(self, gui_manager, file_object,
self.gui_manager = gui_manager
self.config = self.gui_manager.config

self.api = getattr(Hosts, self.config.get_key("site") + "_api")
self.file_object = file_object
self.download_only = download_only
self.choose_host = choose_host

self.selected_quality = None

self._speed_list = []
self._last_downloaded_size = 0
self.speed = 0
Expand All @@ -58,81 +61,89 @@ def __init__(self, gui_manager, file_object,
status_message="Fetching hosts...", unfreeze=False)

def get_hosts(self):
""" Returns a list with the avaliable downloaders for the file. """
""" Returns a dict with the avaliable downloaders for the file. """

result = []
result = {}
avaliable_downloaders = Downloaders.get_avaliable()

try:
hosts = self.file_object.file_hosts
except Exception, error:
print "Warning: no host found due to: '%s'" % error
raise Exception("No host found due to: '%s'" % error)
hosts = {}

hosts["dummy"] = ""

for host in hosts:
if host in avaliable_downloaders:
result.append(Downloaders.get(host, self.gui_manager,
hosts[host]))
result[host] = hosts[host]

return result

def display_hosts(self, (is_error, result)):
""" Shows up the hosts selecting window. """

if is_error:
self.gui_manager.report_error("Error displaying hosts: %s" % result)
self.gui_manager.report_error("Error fetching hosts: %s" % result)
gobject.idle_add(self.gui_manager.progress.hide)
return


gobject.idle_add(self.gui_manager.set_status_message, "")

if len(result) == 0:
if not result:
self.gui_manager.report_error("No host found")
self.gui_manager.unfreeze()
elif len(result) == 1 and not self.choose_host:
gobject.idle_add(self.gui_manager.set_status_message,
"Only one host found, starting download...")
self.downloader = result[0]
self.downloader.process_url(self.play, self.file_path)
else:
megaupload = [x for x in result if x.name == "Megaupload"]
if not self.choose_host and len(megaupload) != 0 and \
self.config.get_key("automatic_megaupload"):

gobject.idle_add(self.gui_manager.set_status_message,
"Automatically starting with megaupload")
self.downloader = megaupload[0]
self.downloader.process_url(self.play, self.file_path)
else:
for downloader in result:
icon = downloader.icon
name = downloader.name
self.hosts_icon_view_model.append([icon, name, downloader])

self.hosts_window.show_all()
# elif len(result) == 1 and not self.choose_host:
# gobject.idle_add(self.gui_manager.set_status_message,
# "Only one host found, starting download...")
# self.downloader = result[0]
# self.downloader.process_url(self.play, self.file_path)
# else:
# megaupload = [x for x in result if x.name == "Megaupload"]
# if not self.choose_host and len(megaupload) != 0 and \
# self.config.get_key("automatic_megaupload"):

# gobject.idle_add(self.gui_manager.set_status_message,
# "Automatically starting with megaupload")
# self.downloader = megaupload[0]
# self.downloader.process_url(self.play, self.file_path)
# else:
# for downloader in result:
# icon = downloader.icon
# name = downloader.name
# self.hosts_icon_view_model.append([icon, name, downloader])

# self.hosts_window.show_all()

for host in result:
for quality in result[host]:
downloader = Downloaders.get(host, self.gui_manager,
result[host][quality])
icon = downloader.icon
name = "%s (%s)" % (downloader.name, quality)

self.hosts_icon_view_model.append([icon, name, downloader])

self.hosts_window.show_all()

def play(self):
""" Starts the playing of the file on file_path. """

self.gui_manager.background_task(self.pre_download,
self.open_player, unfreeze=False)

self.gui_manager.background_task(self.download_subtitle,
self._on_download_subtitle_finish, unfreeze=False)

def pre_download(self):
""" Downloads some content to start safely the player. """

# Download the subtitle
gobject.idle_add(self.gui_manager.set_status_message,
"Downloading subtitles...")

try:
self.file_object.get_subtitle(
filename=self.file_path.replace(".mp4", ""))
except:
gobject.idle_add(self.gui_manager.set_status_message,
"Subtitle not found")

# Wait for the file to exists
gobject.idle_add(self.gui_manager.set_status_message, "Wait please...")
while not os.path.exists(self.file_path):
Expand Down Expand Up @@ -188,6 +199,7 @@ def update_speed(self):
list_offset = len(self._speed_list) - 30 if len(self._speed_list) > 30 else 0
self._speed_list = self._speed_list[list_offset:]
self.speed = sum(self._speed_list) / len(self._speed_list)
self._last_downloaded_size = downloaded_size

def update(self):
""" Updates the GUI with downloading data. """
Expand Down Expand Up @@ -236,8 +248,6 @@ def update(self):
if not self.download_only:
stop |= self.player_process.poll() != None

self._last_downloaded_size = downloaded_size

def _update_progress(self):
""" Updates the progress bar using the downloaded size and the
total file size if posible. """
Expand Down Expand Up @@ -270,11 +280,11 @@ def on_finish(self, (is_error, result)):
def get_filename(self):
""" Returns the file path of the file. """

if isinstance(self.file_object, self.api.Movie):
if isinstance(self.file_object, BaseMovie):
return self.file_object.name.replace(os.sep, "_") + ".mp4"

# If isn't a movie it must be an episode
assert isinstance(self.file_object, self.api.Episode)
assert isinstance(self.file_object, BaseEpisode)

result = self.config.get_key("filename_template")
result = result.replace("<show>", self.file_object.show.name)
Expand All @@ -289,6 +299,14 @@ def get_filename(self):

return result

def download_subtitle(self):
""" Downloads the subtitle for the selected episode. """

url = self.file_object.get_subtitle_url(quality=self.selected_quality)
url_open = UrlOpen()

url_open(url, filename=self.file_path.replace(".mp4", ".srt"))

# ================================
# = CALLBACKS =
# ================================
Expand All @@ -309,7 +327,15 @@ def _on_host_select(self, *args):
return

path = cursor[0]
selected_text = self.hosts_icon_view_model[path][HOSTS_VIEW_COLUMN_TEXT]
self.downloader = self.hosts_icon_view_model[path][HOSTS_VIEW_COLUMN_OBJECT]

self.hosts_window.hide()

self.selected_quality = selected_text.split("(")[1].split(")")[0]
self.downloader.process_url(self.play, self.file_path)

def _on_download_subtitle_finish(self, (is_error, result)):
if is_error:
gobject.idle_add(self.gui_manager.set_status_message,
"Download subtitle failed")

0 comments on commit 012d57b

Please sign in to comment.