diff --git a/resolv/__init__.py b/resolv/__init__.py index dd7d0dd..6c0ac33 100644 --- a/resolv/__init__.py +++ b/resolv/__init__.py @@ -1,12 +1,6 @@ -import re, sys +import re 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) @@ -38,15 +32,3 @@ 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 f2afa70..c8bdff1 100644 --- a/resolv/resolvers/filebox.py +++ b/resolv/resolvers/filebox.py @@ -1,23 +1,23 @@ import re, time, urllib2 -import resolv +from resolv.shared import ResolverError def resolve(url): matches = re.search("https?:\/\/(www\.)?filebox\.com\/([a-zA-Z0-9]+)", url) if matches is None: - raise resolv.ResolverError("The provided URL is not a valid Filebox.com URL.") + raise 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 resolv.ResolverError("Could not retrieve the video page.") + raise ResolverError("Could not retrieve the video page.") matches = re.search("url: '([^']+)',", contents) if matches is None: - raise resolv.ResolverError("No video was found on the specified URL.") + raise 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 resolv.ResolverError("The Python mechanize module is required to resolve Filebox.com URLs.") + raise 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 resolv.ResolverError("The provided URL is not a valid Filebox.com URL.") + raise 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 resolv.ResolverError("The Filebox.com site could not be reached.") + raise 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 resolv.ResolverError("The file was removed, or the URL is incorrect.") + raise ResolverError("The file was removed, or the URL is incorrect.") matches = re.search("this\.play\('([^']+)'\)", page) if matches is None: - 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.") + 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.") video_file = matches.group(1) diff --git a/resolv/resolvers/mediafire.py b/resolv/resolvers/mediafire.py index 2692d95..1e3641a 100644 --- a/resolv/resolvers/mediafire.py +++ b/resolv/resolvers/mediafire.py @@ -1,23 +1,23 @@ import re, urllib2 -import resolv +from resolv.shared import ResolverError, unescape def resolve(url): try: contents = urllib2.urlopen(url).read() except: - raise resolv.ResolverError("Could not retrieve the specified URL.") + raise ResolverError("Could not retrieve the specified URL.") matches = re.search('kNO = "([^"]+)";', contents) if matches is None: - 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.") + 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.") file_url = matches.group(1) try: file_title = unescape(re.search('([^<]+)<\/title>', contents).group(1)) except: - raise resolv.ResolverError("Could not find the download title.") + raise 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 ea3aee9..d94ed7d 100644 --- a/resolv/resolvers/onechannel.py +++ b/resolv/resolvers/onechannel.py @@ -1,15 +1,15 @@ import re, base64 -import resolv +from resolv.shared import ResolverError def resolve(url): matches = re.search("https?:\/\/(www\.)?1channel\.ch\/external\.php\?.*url=([^&]+)", url) if matches is None: - raise resolv.ResolverError("The provided URL is not a valid external 1channel URL.") + raise ResolverError("The provided URL is not a valid external 1channel URL.") try: real_url = base64.b64decode(matches.group(2)).strip() except TypeError: - raise resolv.ResolverError("The provided URL is malformed.") + raise ResolverError("The provided URL is malformed.") return { 'url': real_url } diff --git a/resolv/resolvers/pastebin.py b/resolv/resolvers/pastebin.py index 60def2f..4f2089f 100644 --- a/resolv/resolvers/pastebin.py +++ b/resolv/resolvers/pastebin.py @@ -1,23 +1,23 @@ import re, urllib, urllib2 -import resolv +from resolv.shared import ResolverError, unescape def resolve(url): matches = re.search("https?:\/\/(www\.)?pastebin\.com\/([a-zA-Z0-9]+)", url) if matches is None: - raise resolv.ResolverError("The provided URL is not a valid Pastebin URL.") + raise ResolverError("The provided URL is not a valid Pastebin URL.") paste_id = matches.group(2) try: contents = urllib2.urlopen(url).read() except: - raise resolv.ResolverError("Could not retrieve the specified URL. The specified paste may not exist.") + raise ResolverError("Could not retrieve the specified URL. The specified paste may not exist.") matches = re.search("<h1>([^<]+)</h1>", contents) if matches is None: - raise resolv.ResolverError("The provided URL is not a valid paste.") + raise 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 b527d18..b603e3a 100644 --- a/resolv/resolvers/putlocker.py +++ b/resolv/resolvers/putlocker.py @@ -1,5 +1,5 @@ import re -import resolv +from resolv.shared import ResolverError, unescape 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 resolv.ResolverError("The provided URL is not a valid PutLocker URL.") + raise 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 resolv.ResolverError("The PutLocker site could not be reached.") + raise ResolverError("The PutLocker site could not be reached.") try: browser.select_form(nr=0) result = browser.submit() page = result.read() except Exception, e: - raise resolv.ResolverError("The file was removed, or the URL is incorrect.") + raise ResolverError("The file was removed, or the URL is incorrect.") matches = re.search("playlist: '([^']+)'", page) if matches is None: - 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.") + 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) try: browser.open("http://www.putlocker.com%s" % playlist) except: - raise resolv.ResolverError("The playlist file for the given URL could not be loaded.") + raise 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 resolv.ResolverError("The playlist file does not contain any video URLs. The PutLocker resolver currently only supports video links.") + raise 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 = resolv.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: - raise resolv.ResolverError("Could not find the video title.") + raise 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 ee21596..079b110 100644 --- a/resolv/resolvers/sockshare.py +++ b/resolv/resolvers/sockshare.py @@ -1,16 +1,16 @@ import re -import resolv +from resolv.shared import ResolverError, unescape def resolve(url): try: import mechanize except ImportError: - raise resolv.ResolverError("The Python mechanize module is required to resolve SockShare URLs.") + raise 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 resolv.ResolverError("The provided URL is not a valid SockShare URL.") + raise 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 resolv.ResolverError("The SockShare site could not be reached.") + raise ResolverError("The SockShare site could not be reached.") try: browser.select_form(nr=0) result = browser.submit() page = result.read() except Exception, e: - raise resolv.ResolverError("The file was removed, or the URL is incorrect.") + raise ResolverError("The file was removed, or the URL is incorrect.") matches = re.search("playlist: '([^']+)'", page) if matches is None: - 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.") + 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) try: browser.open("http://www.sockshare.com%s" % playlist) except: - raise resolv.ResolverError("The playlist file for the given URL could not be loaded.") + raise 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 resolv.ResolverError("The playlist file does not contain any video URLs. The SockShare resolver currently only supports video links.") + raise 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 resolv.ResolverError("Could not find the video title.") + raise 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 664c600..6ffd2e1 100644 --- a/resolv/resolvers/youtube.py +++ b/resolv/resolvers/youtube.py @@ -1,11 +1,11 @@ import re, urllib, urllib2 -import resolv +from resolv.shared import ResolverError, unescape def resolve(url): try: contents = urllib2.urlopen(url).read() except: - raise resolv.ResolverError("Could not retrieve the specified URL.") + raise 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 resolv.ResolverError("The starting position for the YouTube player configuration could not be found. Is the URL really a valid video page?") + raise 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 resolv.ResolverError("The ending position for the YouTube player configuration could not be found.") + raise 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 resolv.ResolverError("The YouTube player configuration is corrupted.") + raise ResolverError("The YouTube player configuration is corrupted.") stream_pool = [] @@ -34,7 +34,7 @@ def resolve(url): fields = stream.split('&') if len(fields) < 5: - raise resolv.ResolverError("The amount of fields in the YouTube player configuration is incorrect.") + raise 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 resolv.ResolverError("Could not find the video title.") + raise ResolverError("Could not find the video title.") return { 'title': video_title, 'videos': stream_pool } diff --git a/resolv/shared.py b/resolv/shared.py new file mode 100644 index 0000000..a668870 --- /dev/null +++ b/resolv/shared.py @@ -0,0 +1,15 @@ +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)