From fd3011e96dd7a4566d2ea70202e9686af684d1c6 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Fri, 8 Jun 2012 19:58:02 +0200 Subject: [PATCH] Merge shared code into __init__.py --- resolv/__init__.py | 20 +++++++++++++++++++- resolv/resolvers/filebox.py | 18 +++++++++--------- resolv/resolvers/mediafire.py | 8 ++++---- resolv/resolvers/onechannel.py | 6 +++--- resolv/resolvers/pastebin.py | 8 ++++---- resolv/resolvers/putlocker.py | 18 +++++++++--------- resolv/resolvers/sockshare.py | 18 +++++++++--------- resolv/resolvers/youtube.py | 14 +++++++------- resolv/shared.py | 15 --------------- 9 files changed, 64 insertions(+), 61 deletions(-) delete mode 100644 resolv/shared.py diff --git a/resolv/__init__.py b/resolv/__init__.py index 6c0ac33..dd7d0dd 100644 --- a/resolv/__init__.py +++ b/resolv/__init__.py @@ -1,6 +1,12 @@ -import re +import re, sys from resolvers import * +from HTMLParser import HTMLParser +# Fix encoding to deal with UTF-8 page contents +reload(sys) +sys.setdefaultencoding("UTF-8") + +# Main functions def resolve(url): if re.match("https?:\/\/(www\.)?putlocker\.com", url) is not None: return putlocker.resolve(url) @@ -32,3 +38,15 @@ def recurse(url): url = result['url'] previous_result = result + +# Exception classes +class ResolverError(Exception): + def __init__(self, value): + self.val = value + + def __str__(self): + return repr(self.val) + +# Utility functions +def unescape(s): + return HTMLParser.unescape.__func__(HTMLParser, s) diff --git a/resolv/resolvers/filebox.py b/resolv/resolvers/filebox.py index c8bdff1..f2afa70 100644 --- a/resolv/resolvers/filebox.py +++ b/resolv/resolvers/filebox.py @@ -1,23 +1,23 @@ import re, time, urllib2 -from resolv.shared import ResolverError +import resolv def resolve(url): matches = re.search("https?:\/\/(www\.)?filebox\.com\/([a-zA-Z0-9]+)", url) if matches is None: - raise ResolverError("The provided URL is not a valid Filebox.com URL.") + raise resolv.ResolverError("The provided URL is not a valid Filebox.com URL.") video_id = matches.group(2) try: contents = urllib2.urlopen("http://www.filebox.com/embed-%s-970x543.html" % video_id).read() except: - raise ResolverError("Could not retrieve the video page.") + raise resolv.ResolverError("Could not retrieve the video page.") matches = re.search("url: '([^']+)',", contents) if matches is None: - raise ResolverError("No video was found on the specified URL.") + raise resolv.ResolverError("No video was found on the specified URL.") video_file = matches.group(1) @@ -37,19 +37,19 @@ def resolve2(url): try: import mechanize except ImportError: - raise ResolverError("The Python mechanize module is required to resolve Filebox.com URLs.") + raise resolv.ResolverError("The Python mechanize module is required to resolve Filebox.com URLs.") matches = re.search("https?:\/\/(www\.)?filebox\.com\/([a-zA-Z0-9]+)", url) if matches is None: - raise ResolverError("The provided URL is not a valid Filebox.com URL.") + raise resolv.ResolverError("The provided URL is not a valid Filebox.com URL.") try: browser = mechanize.Browser() browser.set_handle_robots(False) browser.open(url) except: - raise ResolverError("The Filebox.com site could not be reached.") + raise resolv.ResolverError("The Filebox.com site could not be reached.") time.sleep(6) @@ -58,12 +58,12 @@ def resolve2(url): result = browser.submit() page = result.read() except Exception, e: - raise ResolverError("The file was removed, or the URL is incorrect.") + raise resolv.ResolverError("The file was removed, or the URL is incorrect.") matches = re.search("this\.play\('([^']+)'\)", page) if matches is None: - raise ResolverError("No video file was found on the given URL; the Filebox.com server for this file may be in maintenance mode, or the given URL may not be a video file. The Filebox.com resolver currently only supports video links.") + raise resolv.ResolverError("No video file was found on the given URL; the Filebox.com server for this file may be in maintenance mode, or the given URL may not be a video file. The Filebox.com resolver currently only supports video links.") video_file = matches.group(1) diff --git a/resolv/resolvers/mediafire.py b/resolv/resolvers/mediafire.py index 1e3641a..2692d95 100644 --- a/resolv/resolvers/mediafire.py +++ b/resolv/resolvers/mediafire.py @@ -1,23 +1,23 @@ import re, urllib2 -from resolv.shared import ResolverError, unescape +import resolv def resolve(url): try: contents = urllib2.urlopen(url).read() except: - raise ResolverError("Could not retrieve the specified URL.") + raise resolv.ResolverError("Could not retrieve the specified URL.") matches = re.search('kNO = "([^"]+)";', contents) if matches is None: - raise ResolverError("No download was found on the given URL; the server for this file may be in maintenance mode, or the given URL may not be valid. It is also possible that you have been blocked - CAPTCHA support is not yet present.") + raise resolv.ResolverError("No download was found on the given URL; the server for this file may be in maintenance mode, or the given URL may not be valid. It is also possible that you have been blocked - CAPTCHA support is not yet present.") file_url = matches.group(1) try: file_title = unescape(re.search('([^<]+)<\/title>', contents).group(1)) except: - raise ResolverError("Could not find the download title.") + raise resolv.ResolverError("Could not find the download title.") file_dict = { 'url' : file_url, diff --git a/resolv/resolvers/onechannel.py b/resolv/resolvers/onechannel.py index d94ed7d..ea3aee9 100644 --- a/resolv/resolvers/onechannel.py +++ b/resolv/resolvers/onechannel.py @@ -1,15 +1,15 @@ import re, base64 -from resolv.shared import ResolverError +import resolv def resolve(url): matches = re.search("https?:\/\/(www\.)?1channel\.ch\/external\.php\?.*url=([^&]+)", url) if matches is None: - raise ResolverError("The provided URL is not a valid external 1channel URL.") + raise resolv.ResolverError("The provided URL is not a valid external 1channel URL.") try: real_url = base64.b64decode(matches.group(2)).strip() except TypeError: - raise ResolverError("The provided URL is malformed.") + raise resolv.ResolverError("The provided URL is malformed.") return { 'url': real_url } diff --git a/resolv/resolvers/pastebin.py b/resolv/resolvers/pastebin.py index 4f2089f..60def2f 100644 --- a/resolv/resolvers/pastebin.py +++ b/resolv/resolvers/pastebin.py @@ -1,23 +1,23 @@ import re, urllib, urllib2 -from resolv.shared import ResolverError, unescape +import resolv def resolve(url): matches = re.search("https?:\/\/(www\.)?pastebin\.com\/([a-zA-Z0-9]+)", url) if matches is None: - raise ResolverError("The provided URL is not a valid Pastebin URL.") + raise resolv.ResolverError("The provided URL is not a valid Pastebin URL.") paste_id = matches.group(2) try: contents = urllib2.urlopen(url).read() except: - raise ResolverError("Could not retrieve the specified URL. The specified paste may not exist.") + raise resolv.ResolverError("Could not retrieve the specified URL. The specified paste may not exist.") matches = re.search("<h1>([^<]+)</h1>", contents) if matches is None: - raise ResolverError("The provided URL is not a valid paste.") + raise resolv.ResolverError("The provided URL is not a valid paste.") paste_title = unescape(matches.group(1)) diff --git a/resolv/resolvers/putlocker.py b/resolv/resolvers/putlocker.py index b603e3a..b527d18 100644 --- a/resolv/resolvers/putlocker.py +++ b/resolv/resolvers/putlocker.py @@ -1,5 +1,5 @@ import re -from resolv.shared import ResolverError, unescape +import resolv def resolve(url): try: @@ -10,7 +10,7 @@ def resolve(url): matches = re.search("https?:\/\/(www\.)?putlocker\.com\/(file|embed)\/([A-Z0-9]+)", url) if matches is None: - raise ResolverError("The provided URL is not a valid PutLocker URL.") + raise resolv.ResolverError("The provided URL is not a valid PutLocker URL.") video_id = matches.group(3) @@ -19,38 +19,38 @@ def resolve(url): browser.set_handle_robots(False) browser.open("http://putlocker.com/embed/%s" % video_id) except: - raise ResolverError("The PutLocker site could not be reached.") + raise resolv.ResolverError("The PutLocker site could not be reached.") try: browser.select_form(nr=0) result = browser.submit() page = result.read() except Exception, e: - raise ResolverError("The file was removed, or the URL is incorrect.") + raise resolv.ResolverError("The file was removed, or the URL is incorrect.") matches = re.search("playlist: '([^']+)'", page) if matches is None: - 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 resolv.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) try: browser.open("http://www.putlocker.com%s" % playlist) except: - raise ResolverError("The playlist file for the given URL could not be loaded.") + raise resolv.ResolverError("The playlist file for the given URL could not be loaded.") matches = re.search("url=\"([^\"]+)\" type=\"video\/x-flv\"", browser.response().read()) if matches is None: - raise ResolverError("The playlist file does not contain any video URLs. The PutLocker resolver currently only supports video links.") + raise resolv.ResolverError("The playlist file does not contain any video URLs. The PutLocker resolver currently only supports video links.") video_file = matches.group(1) try: - video_title = unescape(re.search('<a href="\/file\/[^"]+"[^>]*><strong>([^<]*)<\/strong><\/a>', page).group(1)) + video_title = resolv.unescape(re.search('<a href="\/file\/[^"]+"[^>]*><strong>([^<]*)<\/strong><\/a>', page).group(1)) except: - raise ResolverError("Could not find the video title.") + raise resolv.ResolverError("Could not find the video title.") stream_dict = { 'url' : video_file, diff --git a/resolv/resolvers/sockshare.py b/resolv/resolvers/sockshare.py index 079b110..ee21596 100644 --- a/resolv/resolvers/sockshare.py +++ b/resolv/resolvers/sockshare.py @@ -1,16 +1,16 @@ import re -from resolv.shared import ResolverError, unescape +import resolv def resolve(url): try: import mechanize except ImportError: - raise ResolverError("The Python mechanize module is required to resolve SockShare URLs.") + raise resolv.ResolverError("The Python mechanize module is required to resolve SockShare URLs.") matches = re.search("https?:\/\/(www\.)?sockshare\.com\/(file|embed)\/([A-Z0-9]+)", url) if matches is None: - raise ResolverError("The provided URL is not a valid SockShare URL.") + raise resolv.ResolverError("The provided URL is not a valid SockShare URL.") video_id = matches.group(3) @@ -19,38 +19,38 @@ def resolve(url): browser.set_handle_robots(False) browser.open("http://sockshare.com/embed/%s" % video_id) except: - raise ResolverError("The SockShare site could not be reached.") + raise resolv.ResolverError("The SockShare site could not be reached.") try: browser.select_form(nr=0) result = browser.submit() page = result.read() except Exception, e: - raise ResolverError("The file was removed, or the URL is incorrect.") + raise resolv.ResolverError("The file was removed, or the URL is incorrect.") matches = re.search("playlist: '([^']+)'", page) if matches is None: - 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 resolv.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) try: browser.open("http://www.sockshare.com%s" % playlist) except: - raise ResolverError("The playlist file for the given URL could not be loaded.") + raise resolv.ResolverError("The playlist file for the given URL could not be loaded.") matches = re.search("url=\"([^\"]+)\" type=\"video\/x-flv\"", browser.response().read()) if matches is None: - raise ResolverError("The playlist file does not contain any video URLs. The SockShare resolver currently only supports video links.") + raise resolv.ResolverError("The playlist file does not contain any video URLs. The SockShare resolver currently only supports video links.") video_file = matches.group(1) try: video_title = unescape(re.search('<a href="\/file\/[^"]+"[^>]*><strong>([^<]*)<\/strong><\/a>', page).group(1)) except: - raise ResolverError("Could not find the video title.") + raise resolv.ResolverError("Could not find the video title.") stream_dict = { 'url' : video_file, diff --git a/resolv/resolvers/youtube.py b/resolv/resolvers/youtube.py index 6ffd2e1..664c600 100644 --- a/resolv/resolvers/youtube.py +++ b/resolv/resolvers/youtube.py @@ -1,11 +1,11 @@ import re, urllib, urllib2 -from resolv.shared import ResolverError, unescape +import resolv def resolve(url): try: contents = urllib2.urlopen(url).read() except: - raise ResolverError("Could not retrieve the specified URL.") + raise resolv.ResolverError("Could not retrieve the specified URL.") map_start = "url_encoded_fmt_stream_map=" map_end = "\\u0026amp;" @@ -14,19 +14,19 @@ def resolve(url): pos_start = contents.index(map_start) + len(map_start) + 6 snippet = contents[pos_start:] except ValueError: - raise ResolverError("The starting position for the YouTube player configuration could not be found. Is the URL really a valid video page?") + raise resolv.ResolverError("The starting position for the YouTube player configuration could not be found. Is the URL really a valid video page?") try: pos_end = snippet.index(map_end) stream_map = snippet[:pos_end] except ValueError: - raise ResolverError("The ending position for the YouTube player configuration could not be found.") + raise resolv.ResolverError("The ending position for the YouTube player configuration could not be found.") try: stream_map = urllib.unquote(stream_map) streams = stream_map.split(',url=') except: - raise ResolverError("The YouTube player configuration is corrupted.") + raise resolv.ResolverError("The YouTube player configuration is corrupted.") stream_pool = [] @@ -34,7 +34,7 @@ def resolve(url): fields = stream.split('&') if len(fields) < 5: - raise ResolverError("The amount of fields in the YouTube player configuration is incorrect.") + raise resolv.ResolverError("The amount of fields in the YouTube player configuration is incorrect.") video_url = urllib.unquote(fields[0]) quality = fields[1].split("=")[1] @@ -83,6 +83,6 @@ def resolve(url): try: video_title = unescape(re.search('<meta property="og:title" content="([^"]*)">', contents).group(1)) except: - raise ResolverError("Could not find the video title.") + raise resolv.ResolverError("Could not find the video title.") return { 'title': video_title, 'videos': stream_pool } diff --git a/resolv/shared.py b/resolv/shared.py deleted file mode 100644 index a668870..0000000 --- a/resolv/shared.py +++ /dev/null @@ -1,15 +0,0 @@ -from HTMLParser import HTMLParser - -import sys -reload(sys) -sys.setdefaultencoding("UTF-8") - -class ResolverError(Exception): - def __init__(self, value): - self.val = value - - def __str__(self): - return repr(self.val) - -def unescape(s): - return HTMLParser.unescape.__func__(HTMLParser, s)