Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.18 KB | None | 0 0
  1. """
  2. Script shall be called every X minutes. Pseudocode:
  3. get catalog at http://a.4cdn.org/ic/catalog.json
  4. for each thread with prefix "/las/" and "last_modified" greater than locally saved:
  5.     get thread at http://a.4cdn.org/ic/thread/{threadnumber}.json
  6.     for each new reply:
  7.         if name, image and securetrip are valid:
  8.             if name isn't already used:
  9.                 create folder (name!!securetrip)
  10.             if folder exists (thus securetrip is valid):
  11.                 add image to the folder ({timestamp in millis}.{ext})
  12. """
  13.  
  14. import requests, re, os
  15.  
  16. USER_DIR = "users/"
  17. STATE_FILE = "state.txt"
  18. BOARD = "ic"
  19. GENERAL_PREFIX = "/las/"
  20.  
  21. def get(url):
  22.     return requests.get(url).json()
  23.    
  24. def get_catalog(board):
  25.     catalog = get("http://a.4cdn.org/{}/catalog.json".format(board))
  26.     threads = []
  27.     for page in catalog:
  28.         threads.extend(page["threads"])
  29.     return threads
  30.    
  31. def get_thread(board, thread):
  32.     return get("http://a.4cdn.org/{}/thread/{}.json".format(board, thread))["posts"]
  33.  
  34. def get_new_posts(board, thread, time):
  35.     return [post for post in get_thread(board, thread) if post["time"] > time]
  36.  
  37. def load_state():
  38.     try:
  39.         with open(STATE_FILE, "r") as file:
  40.             return tuple(int(time) for time in file.read().split(" "))
  41.     except FileNotFoundError:
  42.         return (0, 0)
  43.        
  44. def save_state(last_report, last_update):
  45.     with open(STATE_FILE, "w") as file:
  46.         file.write("{} {}".format(last_report, last_update))
  47.        
  48. def is_name_valid(name):
  49.     return name is not "Anonymous" and re.match(r"^[a-zA-Z0-9 \-_]{1,32}$", name)
  50.  
  51. def is_submission(post):
  52.     return "name" in post and is_name_valid(post["name"]) and "trip" in post and post["trip"].startswith("!!") and "tim" in post
  53.    
  54. def load_users():
  55.     if not os.path.exists(USER_DIR):
  56.         os.makedirs(USER_DIR)
  57.     users = {}
  58.     for dir in os.listdir(USER_DIR):
  59.         tokens = dir.split("!!")
  60.         users[tokens[0]] = "!!" + tokens[1]
  61.     return users
  62.  
  63. def save_remote_file(url, path):
  64.     if not os.path.exists(path):
  65.         request = requests.get(url, stream=True)
  66.         if request.status_code == 200:
  67.             with open(path, 'wb') as file:
  68.                 for chunk in request.iter_content(1024):
  69.                     file.write(chunk)
  70.         else:
  71.             print("FAILED: {} {} {}".format(url, path, request.status_code))
  72.  
  73. def merge_user(users, name, trip):
  74.     if name not in users:
  75.         users[name] = trip
  76.         return True
  77.     else:
  78.         return users[name] == trip
  79.  
  80. def merge_submission(board, post):
  81.     path = USER_DIR + post["name"] + post["trip"]
  82.     if not os.path.exists(path):
  83.         os.makedirs(path)
  84.     filename = "{}{}".format(post["tim"], post["ext"])
  85.     save_remote_file("http://i.4cdn.org/{}/{}".format(board, filename), path + "/" + filename)
  86.  
  87. if __name__ == "__main__":
  88.     last_report, last_update = load_state()
  89.     users = load_users()
  90.    
  91.     current = last_update
  92.     for thread in get_catalog(BOARD):
  93.         if "sub" in thread and thread["sub"].startswith(GENERAL_PREFIX) and thread["last_modified"] > current:
  94.             last_update = max(last_update, thread["last_modified"])
  95.             print("UPDATE: {} at {}".format(thread["no"], thread["last_modified"]))
  96.             for post in get_new_posts(BOARD, thread["no"], current):
  97.                 if is_submission(post) and merge_user(users, post["name"], post["trip"]):
  98.                     merge_submission(BOARD, post)
  99.        
  100.     save_state(last_report, last_update)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement