Steven Smith revised this gist . Go to revision
1 file changed, 29 insertions
4chanimage.py(file created)
| @@ -0,0 +1,29 @@ | |||
| 1 | + | from util import hook, http | |
| 2 | + | import urllib | |
| 3 | + | import urllib2 | |
| 4 | + | import json | |
| 5 | + | import re | |
| 6 | + | ||
| 7 | + | BASE_URL = "https://mediacru.sh/" | |
| 8 | + | API_URL = BASE_URL + "api/" | |
| 9 | + | ||
| 10 | + | ||
| 11 | + | def mediacrush_upload(address): | |
| 12 | + | try: | |
| 13 | + | data = http.get_json(API_URL + "upload/url", post_data=urllib.urlencode({'url': address})) | |
| 14 | + | return BASE_URL + data["hash"] | |
| 15 | + | except urllib2.HTTPError as e: | |
| 16 | + | data = json.loads(e.read()) | |
| 17 | + | if "file" in data: | |
| 18 | + | return BASE_URL + data["file"]["original"][1:].split(".")[0] | |
| 19 | + | else: | |
| 20 | + | return data | |
| 21 | + | ||
| 22 | + | ||
| 23 | + | fourchan_re = (r'(.*:)//(images.4chan.org)(.*)', re.I) | |
| 24 | + | ||
| 25 | + | @hook.regex(*fourchan_re) | |
| 26 | + | def fourchan_url(match): | |
| 27 | + | location = match.group(3) | |
| 28 | + | response = mediacrush_upload("https://images.4chan.org" + location) | |
| 29 | + | return "4chan backup: " + response | |
Newer
Older