f03nix

rplacejsmap.py

Apr 4th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.18 KB | None | 0 0
  1. import math
  2. import sys
  3. import time
  4. import random
  5. import struct
  6.  
  7. import requests
  8. from requests.adapters import HTTPAdapter
  9.  
  10. with open(sys.argv[1]) as textFile:
  11.     bitmp = [line.split(',') for line in textFile]
  12.  
  13. origin = (int(sys.argv[2]), int(sys.argv[3]))
  14. username = sys.argv[4]
  15. password = sys.argv[5]
  16.  
  17. s = requests.Session()
  18. s.mount('https://www.reddit.com', HTTPAdapter(max_retries=5))
  19. s.headers["User-Agent"] = "PlacePlacer"
  20. r = s.post("https://www.reddit.com/api/login/{}".format(username),
  21.            data={"user": username, "passwd": password, "api_type": "json"})
  22. s.headers['x-modhash'] = r.json()["json"]["data"]["modhash"]
  23.  
  24. def get_bad_pixels():
  25.     badpixels = []
  26.     r = s.get("https://www.reddit.com/api/place/board-bitmap", timeout=10)
  27.     boardcols = r.content[4:]
  28.     ay = 0
  29.     for ay in range(len(bitmp)):
  30.         for ax in range(len(bitmp[ay])):
  31.             boardpos = (origin[1] + ay) * 1000 + origin[0] + ax
  32.             boardcol = struct.unpack('B', boardcols[int(boardpos/2)])[0]
  33.             if boardpos % 2 == 0:
  34.                 boardcol = boardcol >> 4
  35.             else:
  36.                 boardcol = boardcol & 15
  37.             if int(bitmp[ay][ax]) != boardcol:
  38.                 badpixels.append((ax,ay))
  39.     return badpixels
  40.  
  41. while True:
  42.     badpixels = get_bad_pixels()
  43.     while len(badpixels) == 0:
  44.         time.sleep(60)
  45.         badpixels = get_bad_pixels()
  46.     badpixel = random.choice(badpixels)
  47.     ax = badpixel[0]
  48.     ay = badpixel[1]
  49.     print("Placing color {} at {},{}".format(bitmp[ay][ax], origin[0] + ax, origin[1] + ay))
  50.     r = s.post("https://www.reddit.com/api/place/draw.json",
  51.             data={"x": str(origin[0] + ax), "y": str(origin[1] + ay), "color": bitmp[ay][ax]})
  52.     try:
  53.         secs = float(r.json()["wait_seconds"])
  54.     except:
  55.         print("Exception in script - {}, retrying in 10 secs".format(r.json()))
  56.         secs = 10
  57.     waitTime = int(secs) + 2
  58.     if "error" not in r.json():
  59.         print("Placed color, waiting {} seconds.".format(waitTime))
  60.     else:
  61.         print("Cooldown already active, waiting {} seconds.".format(waitTime))
  62.     while(waitTime > 0):
  63.         time.sleep(1)
  64.         waitTime -= 1
Add Comment
Please, Sign In to add comment