Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.31 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """
  4. PixelPlaceBot is a bot that ruins your fun time on Pixel Place. Who would have
  5. thought.
  6.  
  7. put_pixel(x,y,colour) places a pixel,
  8. calculate_grid(grid) calculates the importance of many bad pixels in an image.
  9. get_map() gets the map from Pixel Place.
  10. show_map() shows the map (using false colour) with matplotlib.
  11. largest_elements() picks the points in an array with the largest value.
  12. demonstrate_kill() demonstrates the defacing powers of calculate_grid.
  13.  
  14.    This program is free software: you can redistribute it and/or modify
  15.    it under the terms of the GNU General Public License as published by
  16.    the Free Software Foundation, either version 3 of the License, or
  17.    (at your option) any later version.
  18.  
  19.    This program is distributed in the hope that it will be useful,
  20.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.    GNU General Public License for more details.
  23.  
  24.    You should have received a copy of the GNU General Public License
  25.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  26. """
  27.  
  28. import subprocess
  29. import shlex
  30. import numpy as np
  31. import math
  32. import json
  33. import random
  34.  
  35.  
  36. def put_pixel(x, y, colour):
  37.     s = subprocess.Popen(shlex.split("curl 'https://pixelplace.io/back/placepixelnew.php?history=1' -H 'Host: pixelplace.io' -H 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: https://pixelplace.io/?activateKey=zjlahb051orvx3091yyd84x2pzwb8wn7f8hxji2l6uh3yhc0gbdogbr5anhij4qyd7wwpzivtzzshcsxo5l4gdar0r9jwzi97e6xeot7osz54r2sxoxe1751zflvf45micjw1d4p54uawx3um08n7ep6tb28f7vyjfvls0ay45812bvob4cjj2pcdrlszhrjwm4pmenrkvtm7pbjto2cqsp3kbwksn3pa8exm1o7xhu465o0trdkj3o4eko78rw&activateToken=jzbgmd4tamoftt3tnv78eawjph7xpzu8z5pmiugtg489xc3l7btmlq5b7d8wc25b8uyroek5isfg5j1cuvzgl4rsi0pu3v6bq42finl0g0hljjyeeyu03mtlmjfpfl15q4l8r6987rtras5pq0ptmje93uzig1o659fxgp5ngzeqskfil5c7orgrlga1hy7m7nkncqbtpqjhbz0w4c3t3klp1vqity50mpoyf0r5qbn2anyf0284mttnpk6ijbj' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'X-Requested-With: XMLHttpRequest' -H 'Cookie: 60gpBAK=R1224193598; PHPSESSID=54e3ba68e785f6fee73c28d98cc2ba16; authKey=zjlahb051orvx3091yyd84x2pzwb8wn7f8hxji2l6uh3yhc0gbdogbr5anhij4qyd7wwpzivtzzshcsxo5l4gdar0r9jwzi97e6xeot7osz54r2sxoxe1751zflvf45micjw1d4p54uawx3um08n7ep6tb28f7vyjfvls0ay45812bvob4cjj2pcdrlszhrjwm4pmenrkvtm7pbjto2cqsp3kbwksn3pa8exm1o7xhu465o0trdkj3o4eko78rw; authToken=664l8cip30yr5zjtsy5so16xps99ncxuj1gsezhizfa5fuy8s40h67fvzo5n12ik4ydjxu2xad3p72y06yhc6w75kctmfb7jak37f55pi9fpbdphc7uj32pofiauuie42hchmh74qnu11kjerexvgnjv5uq0c44flgx8y4cos7qtra8jp6e5ty1yssz5w3likiqim37fay928hlxn03gy5frxexuiic203kn6s2hqcjzt5xg51x36cv4rsy9abc; 60gpD=R2432247896' -H 'Connection: keep-alive' --data 'attemptPlace=true&x={}&y={}&c={}&b=1'".format(x, y, colour)), stdout=subprocess.PIPE)
  38.     return json.loads(s.stdout.read())
  39.  
  40.  
  41. def get_map():
  42.     s = subprocess.Popen(shlex.split("curl 'https://pixelplace.io/back/getcanvasnew.php?getCanvasData=1&history=1&umt=0&_=1513294113600' -H 'Host: pixelplace.io' -H 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: https://pixelplace.io/?activateKey=zjlahb051orvx3091yyd84x2pzwb8wn7f8hxji2l6uh3yhc0gbdogbr5anhij4qyd7wwpzivtzzshcsxo5l4gdar0r9jwzi97e6xeot7osz54r2sxoxe1751zflvf45micjw1d4p54uawx3um08n7ep6tb28f7vyjfvls0ay45812bvob4cjj2pcdrlszhrjwm4pmenrkvtm7pbjto2cqsp3kbwksn3pa8exm1o7xhu465o0trdkj3o4eko78rw&activateToken=jzbgmd4tamoftt3tnv78eawjph7xpzu8z5pmiugtg489xc3l7btmlq5b7d8wc25b8uyroek5isfg5j1cuvzgl4rsi0pu3v6bq42finl0g0hljjyeeyu03mtlmjfpfl15q4l8r6987rtras5pq0ptmje93uzig1o659fxgp5ngzeqskfil5c7orgrlga1hy7m7nkncqbtpqjhbz0w4c3t3klp1vqity50mpoyf0r5qbn2anyf0284mttnpk6ijbj' -H 'X-Requested-With: XMLHttpRequest' -H 'Cookie: 60gpBAK=R1224193598; PHPSESSID=54e3ba68e785f6fee73c28d98cc2ba16; authKey=zjlahb051orvx3091yyd84x2pzwb8wn7f8hxji2l6uh3yhc0gbdogbr5anhij4qyd7wwpzivtzzshcsxo5l4gdar0r9jwzi97e6xeot7osz54r2sxoxe1751zflvf45micjw1d4p54uawx3um08n7ep6tb28f7vyjfvls0ay45812bvob4cjj2pcdrlszhrjwm4pmenrkvtm7pbjto2cqsp3kbwksn3pa8exm1o7xhu465o0trdkj3o4eko78rw; authToken=664l8cip30yr5zjtsy5so16xps99ncxuj1gsezhizfa5fuy8s40h67fvzo5n12ik4ydjxu2xad3p72y06yhc6w75kctmfb7jak37f55pi9fpbdphc7uj32pofiauuie42hchmh74qnu11kjerexvgnjv5uq0c44flgx8y4cos7qtra8jp6e5ty1yssz5w3likiqim37fay928hlxn03gy5frxexuiic203kn6s2hqcjzt5xg51x36cv4rsy9abc' -H 'Connection: keep-alive'"), stdout=subprocess.PIPE)
  43.     m = json.loads(s.stdout.read())
  44.     grid = [[0 for x in range(1010)] for y in range(1010)]
  45.     try:
  46.         for x, y, c in [z.split(".") for z in m["data"]["pixels"]]:
  47.             x, y, c = int(x), int(y), int(c)
  48.             grid[y][x] = c
  49.     except:
  50.         return grid
  51.     return grid
  52.  
  53.  
  54. def show_map(grid):
  55.     import matplotlib.pyplot as plt
  56.     plt.imshow(np.array(grid, dtype=float))
  57.     plt.show()
  58.  
  59.  
  60. def calculate_grid(grid):
  61.     weights = grid * 0  # make an array of the same size as grid but blank
  62.     # PASS 1: Populate a set of bad pixels.
  63.     badpixels = set()
  64.     for cy, crow in enumerate(grid):
  65.         for cx, cpos in enumerate(crow):
  66.             if cpos:
  67.                 badpixels.add((cx, cy))
  68.     # PASS 2: Calculate the resulting order.
  69.     for oy, orow in enumerate(grid):
  70.         for ox, opos in enumerate(orow):
  71.             if opos:
  72.                 weights[oy][ox] = 1
  73.                 for cx, cy in badpixels:
  74.                     try:
  75.                         weights[oy][ox] += 1. / math.sqrt((cy - oy)
  76.                                                           ** 2 + (cx - ox)**2)
  77.                     except Exception as e:
  78.                         # print e, e.args, e.message
  79.                         pass
  80.     return weights
  81.  
  82.  
  83. def largest_elements(grid):
  84.     biggestnum = 0
  85.     biggestpos = []
  86.     for y, row in enumerate(grid):
  87.         for x, cell in enumerate(row):
  88.             if cell > biggestnum:
  89.                 biggestpos = []
  90.             if cell >= biggestnum:
  91.                 biggestnum = cell
  92.                 biggestpos.append((x, y))
  93.     return biggestpos
  94.  
  95.  
  96. def updatefig(*args):
  97.     global badart
  98.     global step
  99.     if badart.mean() > 0:
  100.         pos = largest_elements(calculate_grid(badart))
  101.         nextdeface = random.choice(pos)
  102.         print "Removing ({},{}).".format(*nextdeface)
  103.         x, y = nextdeface
  104.         badart[y][x] = 0
  105.         step += 1
  106.     im.set_array(badart)
  107.     return im,
  108.  
  109.  
  110. def demonstrate_kill():
  111.     import matplotlib.pyplot as plt
  112.     import matplotlib.animation as animation
  113.     import sys
  114.     # deface a swastika as demonstration
  115.     # arguably the one at 45 degrees is problematic,
  116.     # but who draws that on a square grid?
  117.     # answer (rot13): "ab bar!"
  118.     badart = np.array([
  119.         [1, 0, 1, 1, 1],
  120.         [1, 0, 1, 0, 0],
  121.         [1, 1, 1, 1, 1],
  122.         [0, 0, 1, 0, 1],
  123.         [1, 1, 1, 0, 1]]).astype(np.float32, copy=False)
  124.     fig = plt.figure()
  125.     im = plt.imshow(badart, cmap=plt.get_cmap("binary"))
  126.     step = 0
  127.  
  128.     ani = animation.FuncAnimation(fig, updatefig, interval=500, blit=True)
  129.     plt.show()
  130.     print "Done in {} steps.".format(step)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement