From aad9b0a632507d2c678541ba606e96922d72f8f5 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Tue, 5 Jun 2012 04:18:34 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + __init__.py | 3 +++ resolvers/__init__.py | 2 ++ resolvers/dummy.py | 2 ++ resolvers/putlocker.py | 49 ++++++++++++++++++++++++++++++++++++++++++ shared.py | 6 ++++++ 6 files changed, 63 insertions(+) create mode 100644 .gitignore create mode 100644 __init__.py create mode 100644 resolvers/__init__.py create mode 100644 resolvers/dummy.py create mode 100644 resolvers/putlocker.py create mode 100644 shared.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..ed18af1 --- /dev/null +++ b/__init__.py @@ -0,0 +1,3 @@ +from resolvers import * + + diff --git a/resolvers/__init__.py b/resolvers/__init__.py new file mode 100644 index 0000000..f84f92c --- /dev/null +++ b/resolvers/__init__.py @@ -0,0 +1,2 @@ +from dummy import * +from putlocker import * diff --git a/resolvers/dummy.py b/resolvers/dummy.py new file mode 100644 index 0000000..deeb57f --- /dev/null +++ b/resolvers/dummy.py @@ -0,0 +1,2 @@ +def resolve(input): + return {'dummy': input} diff --git a/resolvers/putlocker.py b/resolvers/putlocker.py new file mode 100644 index 0000000..6225528 --- /dev/null +++ b/resolvers/putlocker.py @@ -0,0 +1,49 @@ +import re +from resolv.shared import ResolverError + +def resolve(url): + try: + import mechanize + except ImportError: + raise ResolverError("The Python mechanize module is required to resolve Putlocker URLs.") + + 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.") + + video_id = matches.group(3) + + try: + browser = mechanize.Browser() + browser.set_handle_robots(False) + browser.open("http://putlocker.com/embed/%s" % video_id) + except: + raise ResolverError("The Putlocker site could not be reached.") + + try: + browser.select_form(nr=0) + result = browser.submit() + except Exception, e: + raise ResolverError("The file was removed, or the URL is incorrect.") + + matches = re.search("playlist: '([^']+)'", result.read()) + + 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.") + + 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.") + + 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.") + + video_file = matches.group(1) + + return { 'video': video_file } diff --git a/shared.py b/shared.py new file mode 100644 index 0000000..9bfe869 --- /dev/null +++ b/shared.py @@ -0,0 +1,6 @@ +class ResolverError(Exception): + def __init__(self, value): + self.val = value + + def __str__(self): + return repr(self.val)