f03nix

rplaceimage.py

Apr 4th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 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 PIL import Image
  9. from requests.adapters import HTTPAdapter
  10.  
  11. img = Image.open(sys.argv[1])
  12. origin = (int(sys.argv[2]), int(sys.argv[3]))
  13. username = sys.argv[4]
  14. password = sys.argv[5]
  15.  
  16. def find_palette(point):
  17.     rgb_code_dictionary = {
  18.         (255, 255, 255): 0,
  19.         (228, 228, 228): 1,
  20.         (136, 136, 136): 2,
  21.         (34, 34, 34): 3,
  22.         (255, 167, 209): 4,
  23.         (229, 0, 0): 5,
  24.         (229, 149, 0): 6,
  25.         (160, 106, 66): 7,
  26.         (229, 217, 0): 8,
  27.         (148, 224, 68): 9,
  28.         (2, 190, 1): 10,
  29.         (0, 211, 211): 11,
  30.         (0, 131, 199): 12,
  31.         (0, 0, 234): 13,
  32.         (207, 110, 228): 14,
  33.         (130, 0, 128): 15
  34.     }
  35.  
  36.     def distance(c1, c2):
  37.         (r1, g1, b1) = c1
  38.         (r2, g2, b2) = c2
  39.         return math.sqrt((r1 - r2) ** 2 + (g1 - g2) ** 2 + (b1 - b2) ** 2)
  40.  
  41.     colors = list(rgb_code_dictionary.keys())
  42.     closest_colors = sorted(colors, key=lambda color: distance(color, point))
  43.     closest_color = closest_colors[0]
  44.     code = rgb_code_dictionary[closest_color]
  45.     return code
  46.  
  47. s = requests.Session()
  48. s.mount('https://www.reddit.com', HTTPAdapter(max_retries=5))
  49. s.headers["User-Agent"] = "PlacePlacer"
  50. r = s.post("https://www.reddit.com/api/login/{}".format(username),
  51.            data={"user": username, "passwd": password, "api_type": "json"})
  52. s.headers['x-modhash'] = r.json()["json"]["data"]["modhash"]
  53.  
  54. bitmp = []
  55.  
  56. for ay in range(img.height):
  57.     bitmp.append([])
  58.     for ax in range(img.width):
  59.         bitmp[ay].append(find_palette(img.getpixel((ax,ay))))
  60.  
  61. print "Data preperation done, pushing pixels."
  62.  
  63. def get_bad_pixels():
  64.     badpixels = []
  65.     r = s.get("https://www.reddit.com/api/place/board-bitmap", timeout=10)
  66.     boardcols = r.content[4:]
  67.     ay = 0
  68.     for ay in range(len(bitmp)):
  69.         for ax in range(len(bitmp[ay])):
  70.             boardpos = (origin[1] + ay) * 1000 + origin[0] + ax
  71.             boardcol = struct.unpack('B', boardcols[int(boardpos/2)])[0]
  72.             if boardpos % 2 == 0:
  73.                 boardcol = boardcol >> 4
  74.             else:
  75.                 boardcol = boardcol & 15
  76.             if int(bitmp[ay][ax]) != boardcol:
  77.                 badpixels.append((ax,ay))
  78.     return badpixels
  79.  
  80. while True:
  81.     badpixels = get_bad_pixels()
  82.     while len(badpixels) == 0:
  83.         time.sleep(60)
  84.         badpixels = get_bad_pixels()
  85.     badpixel = random.choice(badpixels)
  86.     ax = badpixel[0]
  87.     ay = badpixel[1]
  88.     print("Placing color {} at {},{}".format(bitmp[ay][ax], origin[0] + ax, origin[1] + ay))
  89.     r = s.post("https://www.reddit.com/api/place/draw.json",
  90.             data={"x": str(origin[0] + ax), "y": str(origin[1] + ay), "color": bitmp[ay][ax]})
  91.     try:
  92.         secs = float(r.json()["wait_seconds"])
  93.     except:
  94.         print("Exception in script - {}, retrying in 10 secs".format(r.json()))
  95.         secs = 10
  96.     waitTime = int(secs) + 2
  97.     if "error" not in r.json():
  98.         print("Placed color, waiting {} seconds.".format(waitTime))
  99.     else:
  100.         print("Cooldown already active, waiting {} seconds.".format(waitTime))
  101.     while(waitTime > 0):
  102.         time.sleep(1)
  103.         waitTime -= 1
Add Comment
Please, Sign In to add comment