Improve error handling

master
Sven Slootweg 12 years ago
parent d6dd91cd85
commit ed940b4ee4

@ -1,5 +1,5 @@
import re, time, urllib2 import re, time, urllib2
from resolv.shared import ResolverError, Task from resolv.shared import ResolverError, TechnicalError, Task
class FileboxTask(Task): class FileboxTask(Task):
result_type = "video" result_type = "video"
@ -21,7 +21,7 @@ class FileboxTask(Task):
contents = self.fetch_page("http://www.filebox.com/embed-%s-970x543.html" % video_id) contents = self.fetch_page("http://www.filebox.com/embed-%s-970x543.html" % video_id)
except urllib2.URLError, e: except urllib2.URLError, e:
self.state = "failed" self.state = "failed"
raise ResolverError("Could not retrieve the video page.") raise TechnicalError("Could not retrieve the video page.")
matches = re.search("url: '([^']+)',", contents) matches = re.search("url: '([^']+)',", contents)

@ -1,5 +1,5 @@
import re, urllib2 import re, urllib2
from resolv.shared import ResolverError, unescape, Task from resolv.shared import ResolverError, TechnicalError, unescape, Task
class MediafireTask(Task): class MediafireTask(Task):
result_type = "file" result_type = "file"
@ -13,7 +13,7 @@ class MediafireTask(Task):
contents = self.fetch_page(self.url) contents = self.fetch_page(self.url)
except urllib2.URLError, e: except urllib2.URLError, e:
self.state = "failed" self.state = "failed"
raise ResolverError("Could not retrieve the specified URL.") raise TechnicalError("Could not retrieve the specified URL.")
if '<form name="form_password"' in contents: if '<form name="form_password"' in contents:
# The file is password-protected # The file is password-protected
@ -45,7 +45,7 @@ class MediafireTask(Task):
file_title = unescape(re.search('<title>([^<]+)<\/title>', contents).group(1)) file_title = unescape(re.search('<title>([^<]+)<\/title>', contents).group(1))
except: except:
self.state = "failed" self.state = "failed"
raise ResolverError("Could not find the download title.") raise TechnicalError("Could not find the download title.")
file_dict = { file_dict = {
'url' : file_url, 'url' : file_url,

@ -1,5 +1,5 @@
import re, base64 import re, base64
from resolv.shared import ResolverError, Task from resolv.shared import ResolverError, TechnicalError, Task
class OneChannelTask(Task): class OneChannelTask(Task):
result_type = "url" result_type = "url"
@ -19,7 +19,7 @@ class OneChannelTask(Task):
real_url = base64.b64decode(matches.group(2)).strip() real_url = base64.b64decode(matches.group(2)).strip()
except TypeError: except TypeError:
self.state = "failed" self.state = "failed"
raise ResolverError("The provided URL is malformed.") raise TechnicalError("The provided URL is malformed.")
self.results = { 'url': real_url } self.results = { 'url': real_url }
self.state = "finished" self.state = "finished"

@ -1,5 +1,5 @@
import re import re
from resolv.shared import ResolverError, unescape, Task from resolv.shared import ResolverError, TechnicalError, unescape, Task
class PutlockerTask(Task): class PutlockerTask(Task):
result_type = "video" result_type = "video"
@ -13,7 +13,7 @@ class PutlockerTask(Task):
import mechanize import mechanize
except ImportError: except ImportError:
self.state = "failed" self.state = "failed"
raise ResolverError("The Python mechanize module is required to resolve PutLocker URLs.") raise TechnicalError("The Python mechanize module is required to resolve PutLocker URLs.")
matches = re.search("https?:\/\/(www\.)?putlocker\.com\/(file|embed)\/([A-Z0-9]+)", self.url) matches = re.search("https?:\/\/(www\.)?putlocker\.com\/(file|embed)\/([A-Z0-9]+)", self.url)
@ -29,7 +29,7 @@ class PutlockerTask(Task):
browser.open("http://putlocker.com/embed/%s" % video_id) browser.open("http://putlocker.com/embed/%s" % video_id)
except: except:
self.state = "failed" self.state = "failed"
raise ResolverError("The PutLocker site could not be reached.") raise TechnicalError("The PutLocker site could not be reached.")
try: try:
browser.select_form(nr=0) browser.select_form(nr=0)
@ -42,6 +42,7 @@ class PutlockerTask(Task):
matches = re.search("playlist: '([^']+)'", page) matches = re.search("playlist: '([^']+)'", page)
if matches is None: if matches is None:
self.state = "failed"
raise ResolverError("No playlist was found on the given URL; the PutLocker server for this file may be in maintenance mode, or the given URL may not be a video file. The PutLocker resolver currently only supports video links.") raise ResolverError("No playlist was found on the given URL; the PutLocker server for this file may be in maintenance mode, or the given URL may not be a video file. The PutLocker resolver currently only supports video links.")
playlist = matches.group(1) playlist = matches.group(1)
@ -50,7 +51,7 @@ class PutlockerTask(Task):
browser.open("http://www.putlocker.com%s" % playlist) browser.open("http://www.putlocker.com%s" % playlist)
except: except:
self.state = "failed" self.state = "failed"
raise ResolverError("The playlist file for the given URL could not be loaded.") raise TechnicalError("The playlist file for the given URL could not be loaded.")
matches = re.search("url=\"([^\"]+)\" type=\"video\/x-flv\"", browser.response().read()) matches = re.search("url=\"([^\"]+)\" type=\"video\/x-flv\"", browser.response().read())
@ -64,7 +65,7 @@ class PutlockerTask(Task):
video_title = unescape(re.search('<a href="\/file\/[^"]+"[^>]*><strong>([^<]*)<\/strong><\/a>', page).group(1)) video_title = unescape(re.search('<a href="\/file\/[^"]+"[^>]*><strong>([^<]*)<\/strong><\/a>', page).group(1))
except: except:
self.state = "failed" self.state = "failed"
raise ResolverError("Could not find the video title.") raise TechnicalError("Could not find the video title.")
stream_dict = { stream_dict = {
'url' : video_file, 'url' : video_file,

@ -1,5 +1,5 @@
import re import re
from resolv.shared import ResolverError, unescape, Task from resolv.shared import ResolverError, TechnicalError, unescape, Task
class SockshareTask(Task): class SockshareTask(Task):
result_type = "video" result_type = "video"
@ -13,7 +13,7 @@ class SockshareTask(Task):
import mechanize import mechanize
except ImportError: except ImportError:
self.state = "failed" self.state = "failed"
raise ResolverError("The Python mechanize module is required to resolve Sockshare URLs.") raise TechnicalError("The Python mechanize module is required to resolve Sockshare URLs.")
matches = re.search("https?:\/\/(www\.)?sockshare\.com\/(file|embed)\/([A-Z0-9]+)", self.url) matches = re.search("https?:\/\/(www\.)?sockshare\.com\/(file|embed)\/([A-Z0-9]+)", self.url)
@ -29,7 +29,7 @@ class SockshareTask(Task):
browser.open("http://sockshare.com/embed/%s" % video_id) browser.open("http://sockshare.com/embed/%s" % video_id)
except: except:
self.state = "failed" self.state = "failed"
raise ResolverError("The Sockshare site could not be reached.") raise TechnicalError("The Sockshare site could not be reached.")
try: try:
browser.select_form(nr=0) browser.select_form(nr=0)
@ -42,6 +42,7 @@ class SockshareTask(Task):
matches = re.search("playlist: '([^']+)'", page) matches = re.search("playlist: '([^']+)'", page)
if matches is None: if matches is None:
self.state = "failed"
raise ResolverError("No playlist was found on the given URL; the Sockshare server for this file may be in maintenance mode, or the given URL may not be a video file. The Sockshare resolver currently only supports video links.") raise ResolverError("No playlist was found on the given URL; the Sockshare server for this file may be in maintenance mode, or the given URL may not be a video file. The Sockshare resolver currently only supports video links.")
playlist = matches.group(1) playlist = matches.group(1)
@ -50,7 +51,7 @@ class SockshareTask(Task):
browser.open("http://www.sockshare.com%s" % playlist) browser.open("http://www.sockshare.com%s" % playlist)
except: except:
self.state = "failed" self.state = "failed"
raise ResolverError("The playlist file for the given URL could not be loaded.") raise TechnicalError("The playlist file for the given URL could not be loaded.")
matches = re.search("url=\"([^\"]+)\" type=\"video\/x-flv\"", browser.response().read()) matches = re.search("url=\"([^\"]+)\" type=\"video\/x-flv\"", browser.response().read())
@ -64,7 +65,7 @@ class SockshareTask(Task):
video_title = unescape(re.search('<a href="\/file\/[^"]+"[^>]*><strong>([^<]*)<\/strong><\/a>', page).group(1)) video_title = unescape(re.search('<a href="\/file\/[^"]+"[^>]*><strong>([^<]*)<\/strong><\/a>', page).group(1))
except: except:
self.state = "failed" self.state = "failed"
raise ResolverError("Could not find the video title.") raise TechnicalError("Could not find the video title.")
stream_dict = { stream_dict = {
'url' : video_file, 'url' : video_file,

@ -1,5 +1,5 @@
import re, urllib, urllib2, urlparse import re, urllib, urllib2, urlparse
from resolv.shared import ResolverError, unescape, Task from resolv.shared import ResolverError, TechnicalError, unescape, Task
class YoutubeTask(Task): class YoutubeTask(Task):
result_type = "video" result_type = "video"
@ -19,7 +19,11 @@ class YoutubeTask(Task):
contents = self.fetch_page(self.url) contents = self.fetch_page(self.url)
except urllib2.URLError, e: except urllib2.URLError, e:
self.state = "failed" self.state = "failed"
raise ResolverError("Could not retrieve the specified URL.") raise TechnicalError("Could not retrieve the specified URL.")
if '<meta property="og:video:type"' not in contents:
self.state = "invalid"
raise ResolverError("The specified URL is not a valid YouTube video.")
map_start = "url_encoded_fmt_stream_map=" map_start = "url_encoded_fmt_stream_map="
map_end = "\\u0026amp;" map_end = "\\u0026amp;"
@ -29,21 +33,21 @@ class YoutubeTask(Task):
snippet = contents[pos_start:] snippet = contents[pos_start:]
except ValueError: except ValueError:
self.state = "failed" self.state = "failed"
raise ResolverError("The starting position for the YouTube player configuration could not be found. Is the URL really a valid video page?") raise TechnicalError("The starting position for the YouTube player configuration could not be found. Is the URL really a valid video page?")
try: try:
pos_end = snippet.index(map_end) pos_end = snippet.index(map_end)
stream_map = snippet[:pos_end] stream_map = snippet[:pos_end]
except ValueError: except ValueError:
self.state = "failed" self.state = "failed"
raise ResolverError("The ending position for the YouTube player configuration could not be found.") raise TechnicalError("The ending position for the YouTube player configuration could not be found.")
try: try:
stream_map = urllib.unquote(stream_map) stream_map = urllib.unquote(stream_map)
streams = stream_map.split(',') streams = stream_map.split(',')
except: except:
self.state = "failed" self.state = "failed"
raise ResolverError("The YouTube player configuration is corrupted.") raise TechnicalError("The YouTube player configuration is corrupted.")
stream_pool = [] stream_pool = []
@ -52,7 +56,7 @@ class YoutubeTask(Task):
if len(fields) < 6: if len(fields) < 6:
self.state = "failed" self.state = "failed"
raise ResolverError("The amount of fields in the YouTube player configuration is incorrect.") raise TechnicalError("The amount of fields in the YouTube player configuration is incorrect.")
signature = fields['sig'][0] signature = fields['sig'][0]
video_url = "%s&signature=%s" % (fields['url'][0], signature) video_url = "%s&signature=%s" % (fields['url'][0], signature)
@ -111,7 +115,7 @@ class YoutubeTask(Task):
video_title = unescape(re.search('<meta property="og:title" content="([^"]*)">', contents).group(1)) video_title = unescape(re.search('<meta property="og:title" content="([^"]*)">', contents).group(1))
except: except:
self.state = "failed" self.state = "failed"
raise ResolverError("Could not find the video title.") raise TechnicalError("Could not find the video title.")
self.results = { self.results = {
'title': video_title, 'title': video_title,

@ -12,6 +12,13 @@ class ResolverError(Exception):
def __str__(self): def __str__(self):
return repr(self.val) return repr(self.val)
class TechnicalError(Exception):
def __init__(self, value):
self.val = value
def __str__(self):
return repr(self.val)
class Task(): class Task():
captcha = None captcha = None
cookiejar = None cookiejar = None

@ -1,7 +1,7 @@
from setuptools import setup from setuptools import setup
setup(name='resolv', setup(name='resolv',
version='1.1.0', version='1.2.0',
description='Module for resolving URLs from filehosters, video hosters, and other content hosters', description='Module for resolving URLs from filehosters, video hosters, and other content hosters',
author='Sven Slootweg', author='Sven Slootweg',
author_email='resolv@cryto.net', author_email='resolv@cryto.net',

Loading…
Cancel
Save