Skip to content

Instantly share code, notes, and snippets.

@binux
Last active December 14, 2015 10:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save binux/5071536 to your computer and use it in GitHub Desktop.
Save binux/5071536 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:
# Author: Binux<i@binux.me>
# http://binux.me
# Created on 2013-03-02 19:50:18
import os
import re
import sys
import requests
import logging
yande_re = re.compile(r'https?://yande\.re/post/show/(\d+)')
danbooru_re = re.compile(r'https?://danbooru\.donmai\.us/posts/(\d+)')
konachan_re = re.compile(r'https?://konachan\.com/post/show/(\d+)')
def download(show_page):
try:
for each in [yande_re, danbooru_re, konachan_re, ]:
m = each.match(show_page)
if m:
break
show_page_id = m.group(1)
if each == danbooru_re:
data = requests.get('http://danbooru.donmai.us/posts.json?tags=id:%s' % show_page_id).json[0]
return 'http://danbooru.donmai.us/data/%s.%s' % (data['md5'], data['file_ext'])
elif each == yande_re:
data = requests.get('https://yande.re/post.json?tags=id:%s' % show_page_id).json[0]
return data.get('file_url', '') if data['file_size'] < 5 * 1024 * 1024 else (data.get('jpeg_url') or data.get('file_url', ''))
elif each == konachan_re:
data = requests.get('http://konachan.com/post.json?tags=id:%s' % show_page_id).json[0]
return data.get('file_url', '') if data['file_size'] < 5 * 1024 * 1024 else (data.get('jpeg_url') or data.get('file_url', ''))
else:
return ''
except KeyboardInterrupt:
raise
except:
logging.exception(show_page)
return ''
if __name__ == '__main__':
page = sys.argv[1]
if page.startswith('http'):
print download(page)
elif os.path.exists(page):
for line in open(page):
line = line.strip()
print download(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment