Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ppfun by portasynthinca3
- # distributed under WTFPL
- import requests
- import json
- import datetime
- import colorsys
- import math
- #from winsound import Beep
- #import keyboard
- class PPFun_Exception(Exception):
- pass
- # the canvas class
- class PPFun_canv():
- ident_no = 0
- ident = ''
- title = ''
- colors = []
- desc = ''
- size = 0
- _cooldown = 0
- _cooldown_upd = datetime.datetime.now()
- # class constructor
- def __init__(self, desc, iid):
- self.ident_no = int(iid)
- self.ident = desc['ident']
- self.title = desc['title']
- self.desc = desc['desc']
- self.size = desc['size']
- # first two colors are ocean blue and land white
- # I have no idea of why do you need to duplicate them, especially
- # if sending color numbers 0 and 1 causes an error, but okay
- self.colors = [(c[0], c[1], c[2]) for c in desc['colors']][2:]
- # returns the number of the most similar color
- def approx_color(self, rgb):
- best_color_id = 0
- best_color_diff = 1000
- (r, g, b) = rgb
- (y, i, q) = colorsys.rgb_to_yiq(r / 255, g / 255, b / 255)
- # go through the color list
- for c in self.colors:
- (cr, cg, cb) = c
- # convert the color to YIQ
- (cy, ci, cq) = colorsys.rgb_to_yiq(cr / 255, cg / 255, cb / 255)
- # calculate the difference
- diff = math.sqrt(1.0 * ((cy - y) ** 2)
- + 1.0 * ((ci - i) ** 2)
- + 1.0 * ((cq - q) ** 2))
- # if that difference is smaller than the current best one, choose it
- if diff < best_color_diff:
- best_color_diff = diff
- best_color_id = self.colors.index(c)
- return best_color_id + 2
- # sets a pixel on this canvas
- def set_pixel(self, pos, color):
- (x, y) = pos
- # make a request
- base_address = 'https://pixelplanet.fun'
- r = requests.post(base_address + '/api/pixel', data=json.dumps({
- 'cn': self.ident_no,
- 'x': x,
- 'y': y,
- 'clr': color,
- 'token': None
- }), headers={
- 'Accept': '*/*',
- 'Content-Type': 'application/json',
- 'Origin': base_address,
- 'Referer' : base_address,
- 'Accept-Language': "en-US",
- 'UserAgent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36"
- })
- # parse the response
- response = r.json()
- if 'errors' in response:
- for err in response['errors']:
- raise PPFun_Exception(err['msg'])
- return False
- if 'waitSeconds' in response:
- self._cooldown = response['waitSeconds']
- self._cooldown_upd = datetime.datetime.now()
- return response['success']
- # returns the remaining cooldown time in seconds
- def remaining_cooldown(self):
- adjust = datetime.datetime.now() - self._cooldown_upd
- self._cooldown = self._cooldown - adjust.total_seconds()
- self._cooldown_upd = datetime.datetime.now()
- if self._cooldown < 0:
- self._cooldown = 0
- return self._cooldown
- # the main class
- class PPFun_api():
- json_canvases = {}
- name = ''
- # class constructor
- def __init__(self):
- # make an initial request
- me = requests.get('https://pixelplanet.fun/api/me').json()
- self.name = me['name']
- self.json_canvases = me['canvases']
- # returns a canvas object by its identifier
- def get_canv(self, ident):
- # go through the JSON canvas descriptions
- for k in self.json_canvases:
- c_json = self.json_canvases[k]
- c_ident = c_json['ident']
- # search for a specific identifier
- if c_ident == ident:
- return PPFun_canv(c_json, k)
- # no such canvases exist
- return None
- # returns a list of canvas identifiers, names, descriptions and sizes
- def list_canv(self):
- result = []
- # go through the JSON canvas descriptions
- for k in self.json_canvases:
- c_json = self.json_canvases[k]
- result.append({'identifier': c_json['ident'],
- 'title': c_json['title'],
- 'description': c_json['desc'],
- 'size': c_json['size']})
- return result
- from PIL import Image
- import numpy as np
- from random import randrange
- ppf = PPFun_api()
- canv = ppf.get_canv('d')
- inp = input('Path to image: ')
- im = np.array(Image.open(inp))
- im = np.rot90(im, 3)
- im = np.fliplr(im)
- og_pos = [0, 0]
- inp = input("X Coordinates: ")
- og_pos[0] = int(inp)
- inp = input("Y Coordinates: ")
- og_pos[1] = int(inp)
- print("Press 'esc' to exit.")
- pos = [0, 0]
- succ = False
- main_loop = True
- while main_loop:
- x_ = randrange(len(im))
- y_ = randrange(len(im[x_]))
- pos[0] = og_pos[0]+x_
- pos[1] = og_pos[1]+y_
- color = im[x_, y_]
- if color[3] == 255:
- paint_c = canv.approx_color(tuple([color[0], color[1], color[2]]))
- try:
- succ = canv.set_pixel(tuple(pos), paint_c)
- except Exception as err:
- print("Error: ")
- #WINDOWS ONLY
- #Import "Beep" from the "winsound" module and unhash the following code
- #freq = 2000
- #Beep(freq, 1000)
- #if err == "Captcha occured":
- # Beep(freq, 500)
- # Beep(freq, 500)
- input(err)
- #If you have the keyboard module installed, import it and unhas this code so you can more conveniently exit the script
- #installation: 'pip install keyboard'
- #if keyboard.is_pressed('esc'):
- # main_loop = False
- if succ:
- print(str(pos)+"painting... ", end="\r")
- else:
- print("waiting... ", end="\r")
Add Comment
Please, Sign In to add comment