Alyssa

reddit_place.py

Apr 1st, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.96 KB | None | 0 0
  1. import datetime
  2. import requests
  3. import time
  4. import urllib
  5. current_wait = 0
  6. save_interval = 60
  7. last_save = datetime.datetime.now()
  8. url = "https://www.reddit.com/api/place/draw.json"
  9. auths = [['mod_hash', 'reddit_session']]
  10. for auth in auths:
  11.     auth.append(datetime.datetime.now())
  12.     auth.append(current_wait)
  13. result = []
  14. hex_chars = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
  15. def update_map():
  16.     global result
  17.     result = []
  18.     r = requests.get("https://www.reddit.com/api/place/board-bitmap",headers={"cookie":"reddit_session=xd_get_ur_own","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"})
  19.     raw_bitmap = r.content
  20.     for i in range(10000):
  21.         result.append([])
  22.     x = 0
  23.     y = 0
  24.     bitmap = []
  25.     hex_chars = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
  26.     for pixel in raw_bitmap:
  27.         bitmap.append(pixel >> 4)
  28.         bitmap.append(pixel & 0x0F)
  29.  
  30.     for px in bitmap:
  31.         x += 1
  32.         result[y].append(px)
  33.         if x == 1000:
  34.             x = 0
  35.             y += 1
  36. def get_pixel(x, y):
  37.     return result[y][x]
  38. def place_pixel(auth, x, y, color, auth_index):
  39.     global current_wait
  40.     auth["user-agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
  41.     resp = requests.post(url, headers=auth, data={"x":x,"y":y,"color":color})
  42.     rjson = resp.json()
  43.     print("Placing pixel at " + str(x) + ", " + str(y) + " w/ color " + str(color))
  44.     current_wait = rjson["wait_seconds"]
  45.     auths[auth_index][3] = current_wait
  46.     auths[auth_index][2] = datetime.datetime.now()
  47.     time.sleep(1)
  48.     return rjson
  49. def get_area(min_x, min_y, max_x, max_y):
  50.     min_x += 8
  51.     max_x += 8
  52.     area = []
  53.     return [row[min_x: max_x+1] for row in result[min_y: max_y+1]]
  54.  
  55. def print_area(min_x, min_y, max_x, max_y):
  56.     area = get_area(min_x, min_y, max_x, max_y)
  57.     output = ""
  58.     for row in area:
  59.         for c in row:
  60.             output += hex_chars[c]
  61.         output += "\n"
  62.     return output
  63. def find_auth():
  64.     now = datetime.datetime.now()
  65.     c = 0
  66.     for auth in auths:
  67.         use_when = auth[2] + datetime.timedelta(seconds=auth[3])
  68.         if now > use_when:
  69.             return [auth, c]
  70.         c += 1
  71.     return False
  72. inner_min_x = 280
  73. inner_min_y = 85
  74. inner_max_x = 348
  75. inner_max_y = 88
  76. inner_img = [
  77. "336636333663363336336633666666336633663666666666666636333636366366666",
  78. "363636366636663636363636366663666366663663636636363336636636363336336",
  79. "363636663636663636336636363363636363636636363636363636636633366366363",
  80. "336636333663363336363633663366336633636636363663663336326666366366363"
  81. ]
  82. outer_min_x = 278
  83. outer_min_y = 83
  84. outer_max_x = 350
  85. outer_max_y = 90
  86. outer_img = [
  87. "3333333333333333333333333333333333333333333333333333333333333333333333333",
  88. "3666666666666666666666666666666666666666666666666666666666666666666666663",
  89. "3633663633366336333633663366666633663366366666666666663633363636636666663",
  90. "3636363636663666363636363636666366636666366363663636333663663636333633663",
  91. "3636363666363666363633663636336363636363663636363636363663663336636636363",
  92. "3633663633366336333636363366336633663363663636366366333632666636636636363",
  93. "3666666666666666666666666666666666666666666666666666666666666666666666663",
  94. "3333333333333333333333333333333333333333333333333333333333333333333333333"
  95. ]
  96. def fix(diffs, img, min_x, min_y):
  97.     for diff in diffs:
  98.         use_auth = find_auth()
  99.         if use_auth:
  100.             place_pixel({"cookie":"reddit_session=" + urllib.parse.quote(use_auth[0][1]), "x-modhash": use_auth[0][0]}, diff[0], diff[1], img[diff[2]][diff[3]], use_auth[1])
  101. while True:
  102.     update_map()
  103.     if datetime.datetime.now() > last_save + datetime.timedelta(seconds=save_interval):
  104.         last_save = datetime.datetime.now()
  105.         filename = "map_" + last_save.isoformat() + "bmp"
  106.         with open("maps/" + filename, "w") as f:
  107.             to_write = ""
  108.             for row in result:
  109.                 for hx in row:
  110.                     to_write += hex_chars[hx]
  111.                 to_write += "\n"
  112.     inner = get_area(inner_min_x, inner_min_y, inner_max_x, inner_max_y)
  113.     c = inner_min_y
  114.     diff = []
  115.     lolc = 0
  116.     for row in inner:
  117.         #print(print_area(inner_min_x, inner_min_y, inner_max_x, inner_max_y))
  118.         #print("\n".join(inner_img))
  119.         differences = [i for i in range(len(row)) if hex_chars[row[i]] != inner_img[lolc][i]]
  120.         for vandal in differences:
  121.             diff.append([c, vandal+inner_min_x, c-inner_min_y, vandal])
  122.         c += 1
  123.         lolc += 1
  124.     if len(diff) == 0:
  125.         minor_diff = []
  126.         outer = get_area(outer_min_x, outer_min_y, outer_max_x, outer_max_y)
  127.         print("Cur: ")
  128.         print(print_area(outer_min_x, outer_min_y, outer_max_x, outer_max_y))
  129.         out = ""
  130.         for r in outer_img:
  131.             out += r + "\n"
  132.         print("Desired: ")
  133.         print(out)
  134.         c = outer_max_y
  135.         lolc = 0
  136.         for row in outer:
  137.             differences = [i for i in range(len(row)) if hex_chars[row[i]] != outer_img[lolc][i]]
  138.             for vandal in differences:
  139.                 minor_diff.append([c, vandal+outer_min_x, c-outer_min_y, vandal])
  140.         c += 1
  141.         lolc += 1
  142.         fix(minor_diff, outer_img, outer_min_x, outer_min_y)
  143.     else:
  144.         fix(diff, inner_img, inner_min_x, inner_min_y)
  145.     time.sleep(15)
Add Comment
Please, Sign In to add comment