diff --git a/__init__.py b/__init__.py index d69425b..a65ec0b 100644 --- a/__init__.py +++ b/__init__.py @@ -12,6 +12,8 @@ def resolve(url): return youtube.resolve(url) elif re.match("https?:\/\/(www\.)?filebox\.com\/[a-zA-Z0-9]+", url) is not None: return filebox.resolve(url) + elif re.match("https?:\/\/(www\.)?pastebin\.com\/[a-zA-Z0-9]+", url) is not None: + return pastebin.resolve(url) else: return {} diff --git a/resolvers/__init__.py b/resolvers/__init__.py index 7f6dcbb..cda6834 100644 --- a/resolvers/__init__.py +++ b/resolvers/__init__.py @@ -4,3 +4,4 @@ from sockshare import * from onechannel import * from youtube import * from filebox import * +from pastebin import * diff --git a/resolvers/pastebin.py b/resolvers/pastebin.py new file mode 100644 index 0000000..f418de4 --- /dev/null +++ b/resolvers/pastebin.py @@ -0,0 +1,24 @@ +import re, urllib, urllib2 +from resolv.shared import ResolverError + +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.") + + 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.") + + matches = re.search("

([^<]+)

", contents) + + if matches is None: + raise ResolverError("The provided URL is not a valid paste.") + + paste_title = urllib.unquote(matches.group(1)) + + return { 'title': paste_title, 'files': { 'file': "http://pastebin.com/download.php?i=%s" % paste_id } }