Browse Source

Subir archivos a ''

Initial commit
master
AssBlaster 1 month ago
parent
commit
439725e016
1 changed files with 83 additions and 0 deletions
  1. 83
    0
      down.py

+ 83
- 0
down.py View File

@@ -0,0 +1,83 @@
import requests
import json
import re
import sys
import getopt
import os.path
import time

def downloadBySearch(pattern, field, board, path="./"):
print(board)
print('https://a.4cdn.org/'+ board +'/catalog.json')
catalog = requests.get('https://a.4cdn.org/'+ board +'/catalog.json').json()#print(catalog[0].get('threads')[0].get('com'))
for page in catalog:
for thread in page.get('threads'):
if thread.get(field) != None:
if re.search(pattern, thread.get(field)):
print("Download thread No:" + str(thread.get('no')) + "\nContent: " + thread.get('com'))
for post in requests.get('https://a.4cdn.org/'+ board +'/thread/' + str(thread.get('no')) + ".json").json().get('posts'):
getImg(post, board, path)

def getImg(post, board, path):
if post.get('tim') != None:
img = requests.get('https://i.4cdn.org/'+ board +'/' + str(post.get('tim')) + post.get('ext')).content
filepath = path + post.get('filename') + post.get('ext')
if not os.path.isfile(filepath):
print("Downloading image from post No:" + str(post.get('no')) + " in: " + filepath)
with open(filepath, "wb") as f:
f.write(img)

def downloadThreadImages(thread, board, path="./"):
for post in requests.get('https://a.4cdn.org/'+ board +'/thread/' + thread + ".json").json().get('posts'):
getImg(post, board, path)

def watchThread(thread, board, path="./"):
timestamp = time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(time.time()))

for post in requests.get('https://a.4cdn.org/'+ board +'/thread/' + thread + ".json").json().get('posts'):
getImg(post, board, path)
while True:
print('Refreshing thread No: ' + thread)
response = requests.get('https://a.4cdn.org/' + board + '/thread/'+ thread +'.json', headers={"If-Modified-Since" : timestamp})
if response.status_code == 200:
print("New posts since " + timestamp)
timestamp = time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(time.time()))
for post in response.json().get('posts'):
getImg(post, board, path)
time.sleep(10)

def main(argv):
pattern = ''
field = ''
board = ''
path = ''
try:
opts, args = getopt.getopt(argv, "wb:n:s:f:p:",["board=","number=","search=","field=","path="])
except getopt.GetoptError:
print("The arguments are order-sensitive\n-w <watch (daemon)> -b <board> -n <thread number> -s <regex pattern> -f <field>")
sys.exit(2)
for opt, arg in opts:
if opt == "-w":
watch = True
elif opt in ("-b", "--board"):
board = arg
elif opt in ("-p", "--path"):
path = arg
elif opt in ("-n", "--number"):
if board != '':
if watch:
watchThread(arg, board, path)
downloadThreadImages(arg, board, path)
sys.exit(0)
elif opt in ("-s", "--search"):
pattern = arg
elif opt in ("-f", "--field"):
field = arg
if pattern != '' and field != '' and board != '':
downloadBySearch(pattern, field, board, path)
if __name__ == "__main__":
main(sys.argv[1:])


Loading…
Cancel
Save