Skip to content

Instantly share code, notes, and snippets.

@auxiliary-character
Last active February 8, 2017 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auxiliary-character/cd33be787f2378ff8bc6827817774bdb to your computer and use it in GitHub Desktop.
Save auxiliary-character/cd33be787f2378ff8bc6827817774bdb to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import urllib.request
import json
import sys
import time
def get_url(url):
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36')]
return opener.open(url)
def get_json(url):
with get_url(url) as f:
return json.loads(f.read().decode("utf-8"))
def save_file(f,nonce):
if ("tim" in f) and ("filename" in f) and ("ext" in f):
print(f["tim"],f["filename"],f["ext"])
with get_url("https://media.8ch.net/file_store/{0}{1}".format(f["tim"],f["ext"])) as u:
with open("{0:07}-{1}{2}".format(nonce,f["filename"],f["ext"]),"wb") as f:
f.write(u.read())
def main():
"""example: $ ./betterthancamo.py civ 39912"""
board = sys.argv[1]
number = sys.argv[2]
thread = get_json("https://8ch.net/{0}/res/{1}.json".format(board,number))
nonce = 0
for post in thread["posts"]:
time.sleep(1)
nonce+=1
save_file(post,nonce)
if "extra_files" in post:
for extra in post["extra_files"]:
time.sleep(1)
nonce+=1
save_file(extra,nonce)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment