fmansilla

Untitled

May 21st, 2017
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. import threading
  2. import websocket, time, random, json
  3. from proxylist import ProxyList
  4. import numpy as np
  5. from struct import *
  6. from urllib import request as urlrequest
  7.  
  8. class Tile:
  9. color = 0
  10. isBusy = False
  11. isCompleted = False
  12.  
  13. def __init__(self, color=0):
  14. self.color = color
  15.  
  16.  
  17. def IniciarTiles():
  18. tiles = np.empty(shape=(4000,4000), dtype='object')
  19. tiles.fill(Tile())
  20. return tiles
  21.  
  22. def IniciarConfiguracion():
  23. tiles = np.empty(shape=(4000,4000), dtype='object')
  24. tiles[1121, 3162] = Tile(4)
  25. tiles[1121, 3163] = Tile(4)
  26. tiles[1121, 3164] = Tile(4)
  27. tiles[1121, 3165] = Tile(4)
  28. tiles[1121, 3166] = Tile(4)
  29. tiles[1121, 3167] = Tile(4)
  30. tiles[1121, 3168] = Tile(4)
  31. tiles[1121, 3169] = Tile(4)
  32. tiles[1121, 3170] = Tile(4)
  33. tiles[1121, 3171] = Tile(4)
  34. tiles[1121, 3172] = Tile(4)
  35. tiles[1121, 3173] = Tile(4)
  36. tiles[1121, 3174] = Tile(4)
  37. tiles[1121, 3175] = Tile(4)
  38. tiles[1121, 3176] = Tile(4)
  39. tiles[1121, 3177] = Tile(4)
  40.  
  41. return tiles
  42.  
  43. def BuscarTile(tilesPintar):
  44. for (x,y), value in np.ndenumerate(tilesPintar):
  45. #No Existe, paso
  46. if not value:
  47. continue
  48.  
  49. #Está listo
  50. if value.isCompleted:
  51. continue
  52.  
  53. #No está ocupado
  54. if not value.isBusy:
  55. return {'x': x, 'y': y, 'value': value}
  56.  
  57. return False
  58.  
  59. def Pintar(host, port):
  60. while(1):
  61. #Debug
  62. #websocket.enableTrace(True)
  63.  
  64. url = 'http://pixelcanvas.io/api/ws'
  65.  
  66. req = urlrequest.Request(url, data=None,
  67. headers={
  68. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
  69. })
  70. response = urlrequest.urlopen(req)
  71. data = response.read()
  72. encoding = response.info().get_content_charset('utf-8')
  73. ws_url = json.loads(data.decode(encoding))
  74.  
  75. websocket.setdefaulttimeout(600)
  76. ws = websocket.WebSocket()
  77. try:
  78. ws.connect(ws_url['url'], header=["Origin: http://pixelcanvas.io", "Pragma: no-cache", "Cache-Control: no-cache", "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36", "Accept-Encoding: gzip, deflate, sdch", "Accept-Language: es-ES,es;q=0.8"])
  79. except:
  80. print("Error en Socket %s:%s"% (host, port))
  81. return
  82.  
  83.  
  84. target = BuscarTile(tilesPintar)
  85.  
  86. if not target:
  87. print("No quedan más target, thread finalizado")
  88. return
  89.  
  90. target['value'].isBusy = True
  91. print("Pintando con: %s:%s, Target %d-%d "% (host, port, target['x'], target['y']))
  92.  
  93. posx = target['x']
  94. posy = target['y']
  95. color = target['value'].color
  96.  
  97. posxHex = posx.to_bytes(2, 'big')
  98. posyHex = posy.to_bytes(2, 'big')
  99. colorHex = color.to_bytes(1, 'big')
  100. t = 1
  101. for i in range(4):
  102. t += 1
  103. timeHex = t.to_bytes(1, 'big')
  104. ws.send_binary("\x75\x00"+timeHex.decode())
  105. time.sleep(1)
  106.  
  107. ws.send_binary("\x01"+posxHex.decode()+posyHex.decode()+colorHex.decode())
  108.  
  109. #01 04 41 0c 49 03
  110. #01 = ID PAQ
  111. #04 41 = PosX
  112. #0c 40 = PosY
  113. #03 = Color
  114. #ws.send_binary("\x01"+posxHex.decode()+posyHex.decode()+colorHex.decode())
  115. ws.close()
  116.  
  117. target['value'].isBusy = False
  118. target['value'].isCompleted = True
  119.  
  120. print("Esperando 140 segundos")
  121. time.sleep(140)
  122.  
  123. def IniciarThreads():
  124. for val in pl:
  125. threading.Thread(None, Pintar, None, (val.host, val.port), None).start()
  126.  
  127. def CLI():
  128. while(1):
  129. i = input("Ingrese opcion (q=Salir): ")
  130.  
  131. if "q" in i:
  132. exit()
  133. else:
  134. time.sleep(1)
  135. continue
  136.  
  137. print("Cargando Tiles")
  138. tiles = IniciarTiles()
  139.  
  140. print("Cargando Tiles a rellenar")
  141. tilesPintar = IniciarConfiguracion()
  142.  
  143. print("Iniciando Threads")
  144. IniciarThreads()
  145. #CLI()
Advertisement
Add Comment
Please, Sign In to add comment