Guest User

Untitled

a guest
May 12th, 2020
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. # ppfun by portasynthinca3
  2. # distributed under WTFPL
  3.  
  4. import requests
  5. import json
  6. import datetime
  7. import colorsys
  8. import math
  9. #from winsound import Beep
  10. #import keyboard
  11.  
  12. class PPFun_Exception(Exception):
  13. pass
  14.  
  15. # the canvas class
  16. class PPFun_canv():
  17. ident_no = 0
  18. ident = ''
  19. title = ''
  20. colors = []
  21. desc = ''
  22. size = 0
  23. _cooldown = 0
  24. _cooldown_upd = datetime.datetime.now()
  25.  
  26. # class constructor
  27. def __init__(self, desc, iid):
  28. self.ident_no = int(iid)
  29. self.ident = desc['ident']
  30. self.title = desc['title']
  31. self.desc = desc['desc']
  32. self.size = desc['size']
  33. # first two colors are ocean blue and land white
  34. # I have no idea of why do you need to duplicate them, especially
  35. # if sending color numbers 0 and 1 causes an error, but okay
  36. self.colors = [(c[0], c[1], c[2]) for c in desc['colors']][2:]
  37.  
  38. # returns the number of the most similar color
  39. def approx_color(self, rgb):
  40. best_color_id = 0
  41. best_color_diff = 1000
  42. (r, g, b) = rgb
  43. (y, i, q) = colorsys.rgb_to_yiq(r / 255, g / 255, b / 255)
  44. # go through the color list
  45. for c in self.colors:
  46. (cr, cg, cb) = c
  47. # convert the color to YIQ
  48. (cy, ci, cq) = colorsys.rgb_to_yiq(cr / 255, cg / 255, cb / 255)
  49. # calculate the difference
  50. diff = math.sqrt(1.0 * ((cy - y) ** 2)
  51. + 1.0 * ((ci - i) ** 2)
  52. + 1.0 * ((cq - q) ** 2))
  53. # if that difference is smaller than the current best one, choose it
  54. if diff < best_color_diff:
  55. best_color_diff = diff
  56. best_color_id = self.colors.index(c)
  57. return best_color_id + 2
  58.  
  59. # sets a pixel on this canvas
  60. def set_pixel(self, pos, color):
  61. (x, y) = pos
  62. # make a request
  63. base_address = 'https://pixelplanet.fun'
  64. r = requests.post(base_address + '/api/pixel', data=json.dumps({
  65. 'cn': self.ident_no,
  66. 'x': x,
  67. 'y': y,
  68. 'clr': color,
  69. 'token': None
  70. }), headers={
  71. 'Accept': '*/*',
  72. 'Content-Type': 'application/json',
  73. 'Origin': base_address,
  74. 'Referer' : base_address,
  75. 'Accept-Language': "en-US",
  76. 'UserAgent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36"
  77. })
  78. # parse the response
  79. response = r.json()
  80. if 'errors' in response:
  81. for err in response['errors']:
  82. raise PPFun_Exception(err['msg'])
  83. return False
  84. if 'waitSeconds' in response:
  85. self._cooldown = response['waitSeconds']
  86. self._cooldown_upd = datetime.datetime.now()
  87. return response['success']
  88.  
  89. # returns the remaining cooldown time in seconds
  90. def remaining_cooldown(self):
  91. adjust = datetime.datetime.now() - self._cooldown_upd
  92. self._cooldown = self._cooldown - adjust.total_seconds()
  93. self._cooldown_upd = datetime.datetime.now()
  94. if self._cooldown < 0:
  95. self._cooldown = 0
  96. return self._cooldown
  97.  
  98. # the main class
  99. class PPFun_api():
  100. json_canvases = {}
  101. name = ''
  102.  
  103. # class constructor
  104. def __init__(self):
  105. # make an initial request
  106. me = requests.get('https://pixelplanet.fun/api/me').json()
  107. self.name = me['name']
  108. self.json_canvases = me['canvases']
  109.  
  110. # returns a canvas object by its identifier
  111. def get_canv(self, ident):
  112. # go through the JSON canvas descriptions
  113. for k in self.json_canvases:
  114. c_json = self.json_canvases[k]
  115. c_ident = c_json['ident']
  116. # search for a specific identifier
  117. if c_ident == ident:
  118. return PPFun_canv(c_json, k)
  119. # no such canvases exist
  120. return None
  121.  
  122. # returns a list of canvas identifiers, names, descriptions and sizes
  123. def list_canv(self):
  124. result = []
  125. # go through the JSON canvas descriptions
  126. for k in self.json_canvases:
  127. c_json = self.json_canvases[k]
  128. result.append({'identifier': c_json['ident'],
  129. 'title': c_json['title'],
  130. 'description': c_json['desc'],
  131. 'size': c_json['size']})
  132. return result
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. from cv2 import imread
  140. import numpy as np
  141. from random import randrange
  142.  
  143. ppf = PPFun_api()
  144. canv = ppf.get_canv('d')
  145.  
  146.  
  147.  
  148.  
  149. inp = input('Path to image: ')
  150. im = np.array(imread(inp))
  151. im = np.rot90(im, 3)
  152. im = np.fliplr(im)
  153.  
  154.  
  155. og_pos = [0, 0]
  156.  
  157. inp = input("X Coordinates: ")
  158. og_pos[0] = int(inp)
  159. inp = input("Y Coordinates: ")
  160. og_pos[1] = int(inp)
  161.  
  162. print("Press 'esc' to exit.")
  163.  
  164. pos = [0, 0]
  165.  
  166. succ = False
  167.  
  168. main_loop = True
  169. while main_loop:
  170. x_ = randrange(len(im))
  171. y_ = randrange(len(im[x_]))
  172. pos[0] = og_pos[0]+x_
  173. pos[1] = og_pos[1]+y_
  174. color = im[x_, y_]
  175. if not np.array_equal(color, np.array([0, 0, 0])):
  176. paint_c = canv.approx_color(tuple([color[2], color[1], color[0]]))
  177. try:
  178. succ = canv.set_pixel(tuple(pos), paint_c)
  179. except Exception as err:
  180. print("Error: ")
  181. #WINDOWS ONLY
  182. #Import "Beep" from the "winsound" module and unhash the following code
  183.  
  184. #freq = 2000
  185. #Beep(freq, 1000)
  186. #if err == "Captcha occured":
  187. # Beep(freq, 500)
  188. # Beep(freq, 500)
  189. input(err)
  190.  
  191. #If you have the keyboard module installed, import it and unhas this code so you can more conveniently exit the script
  192. #installation: 'pip install keyboard'
  193. #if keyboard.is_pressed('esc'):
  194. # main_loop = False
  195.  
  196. if succ:
  197. print(str(pos)+"painting... ", end="\r")
  198. else:
  199. print("waiting... ", end="\r")
Add Comment
Please, Sign In to add comment