Guest User

Untitled

a guest
Jan 26th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import websocket,urllib,urllib2,time,threading,json,unicodedata
  4.  
  5. debug=True
  6. showmessages=True
  7.  
  8. name="Cube Clamperl"
  9. password= hm, no.
  10. room="lobby"
  11.  
  12. print 'test1'
  13.  
  14. #ws=websocket.create_connection("ws://sim.smogon.com:8000/showdown/websocket")
  15. ws=websocket.create_connection("ws://pokestrat.com:8000/showdown/websocket")
  16.  
  17. print 'test2'
  18.  
  19. ws.recv()
  20. txt=ws.recv()
  21. Ptool=[i for i in range(len(txt)) if txt[i]=='|']
  22. data = urllib.urlencode({'act': 'login', 'name': name, 'pass' : password, 'challengekeyid': txt[Ptool[1]+1], 'challenge':txt[Ptool[2]+1:]})
  23. req = urllib2.Request("http://play.pokemonshowdown.com/action.php", data, { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36)' })
  24. response = urllib2.urlopen(req)
  25. result = response.read()
  26. Ptool2=[i for i in range(len(result)) if result[i]=='"']
  27. assertion=result[Ptool2[-2]+1:Ptool2[-1]]
  28. ws.send("|/trn %s,0,%s" % (name, assertion) )
  29. time.sleep(2)
  30. ws.send("|/join "+room)
  31. ws.send(room+"|/avatar 267")
  32. #ws.send(room+"|Hello, world !")
  33.  
  34. def parse(text,character,stopat=0):
  35.     if stopat:
  36.         p=[-1]+[i for i in range(len(text)) if text[i]==character][:(stopat-1)]+[len(text)]
  37.     else:
  38.         p=[-1]+[i for i in range(len(text)) if text[i]==character]+[len(text)]
  39.     l=[]
  40.     for i in range(len(p)-1):
  41.         l+=[ text[ p[i]+1:p[i+1] ] ]
  42.     return l
  43.  
  44. class ThreadReception(threading.Thread):
  45.     def ___init___(self):
  46.         threading.Thread.__init__(self)
  47.     def run(self):
  48.         condition=True
  49.         while condition:
  50.             message=ws.recv()
  51.             if showmessages:
  52.                 print message
  53.             analysis(message)
  54.         th.__Thread__.stop()
  55.         ws.close()
  56.  
  57. def analysis(message):
  58.     nparsed=parse(message,"\n")
  59.     if len(nparsed)<6:
  60.         for i in nparsed:
  61.             analysischat(i)
  62. #    elif nparsed[0][:4]=="|pm|":
  63. #        analysis5(nparsed[0])
  64. #        if debug: print 7
  65.  
  66. def analysischat(line):
  67.     parsed=parse(line,"|",stopat=5)
  68.     if len(parsed)>1 and parsed[1]=="c:":
  69.         if parsed[4][:5]=="!trad":
  70.             if parsed[4][5]==" ":
  71.                 data=parsed[4][6:]
  72.                 fr_to_us=json.loads( urllib.urlopen("http://pokemontrash.com/api/fr/to/us/"+data).read() )
  73.                 if fr_to_us['exists']:
  74.                     ws.send(room+"|"+fr_to_us['from']+" : "+fr_to_us['to'])
  75.                 else:
  76.                     us_to_fr=json.loads( urllib.urlopen("http://pokemontrash.com/api/us/to/fr/"+data).read() )
  77.                     if us_to_fr['exists']:
  78.                         ws.send(room+"|"+us_to_fr['from']+" : "+us_to_fr['to'])
  79.                     else:
  80.                         ws.send(room+"|Erreur.")
  81.             else:
  82.                 try:
  83.                     data=parsed[4][10:]
  84.                     dic=json.loads( urllib.urlopen("http://pokemontrash.com/api/"+parsed[4][5:7]+"/to/"+parsed[4][7:9]+"/"+data).read() )
  85.                     if dic['exists']:
  86.                         ws.send(room+"|"+dic['from']+" : "+dic['to'])
  87.                     else:
  88.                         ws.send(room+"|Erreur.")
  89.                 except:
  90.                     ws.send(room+"|Les id de langages disponibles sont : de it fr us es")
  91.         if parsed[4][:6]=="!strat":
  92.             shiftedtxt=''
  93.             txt=parsed[4][7:].lower().decode('unicode-escape')
  94.             hope=''.join((c for c in unicodedata.normalize('NFD', txt) if unicodedata.category(c) != 'Mn'))
  95.             for i in hope:
  96.                 if i==' ':
  97.                     shiftedtxt+='-'
  98.                 else:
  99.                     shiftedtxt+=i
  100.             ws.send(room+"|http://pokestrat.com/fiche_pokemon/"+shiftedtxt+".php")
  101.            
  102. th=ThreadReception()
  103. time.sleep(4)
  104. th.start()
Advertisement
Add Comment
Please, Sign In to add comment