Guest User

Untitled

a guest
Nov 15th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. REFRESHTIME = 10
  2.  
  3. username = ""
  4. mdp = ""
  5. idTopic = 44868
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. import json
  23. import urllib3
  24. import sys
  25. import time
  26. import threading
  27. import queue
  28. urllib3.disable_warnings()
  29. http = urllib3.PoolManager()
  30.  
  31. class Client() :
  32. def __init__(self) :
  33. self.pseudo = ""
  34. self.mdp = ""
  35. self.token = ""
  36. self.id = ""
  37.  
  38. def connection(self, pseudo, mdp) : #ok
  39. r = http.request('POST', 'https://avenoel.org/api/v1/auth', fields = {'username' : pseudo, 'password' : mdp})
  40. liste = json.loads(r.data)
  41. self.token = liste["api_token"]
  42. if liste["error"] == None :
  43. print("Authentification faite :)")
  44. else :
  45. print(liste["error"])
  46.  
  47. def pseudoFromId(self, id) :
  48. r = http.request('GET', 'https://avenoel.org/api/v1/user/'+str(id))
  49. liste = json.loads(r.data)
  50. return(liste["data"]["username"])
  51.  
  52. def getAllTopics(self) : #ok
  53. """Retourne une liste des topics, faire liste[k]["api_url"] pour l'url, "title" pour le titre"""
  54. r = http.request('GET', 'https://avenoel.org/api/v1/topics')
  55. liste = json.loads(r.data)
  56. return liste["data"]
  57.  
  58. def getTopic(self, id) : #ok
  59. """Retourne les infos d'un topic et de son auteur, il faut passer l'id du topic"""
  60. r = http.request('GET', 'https://avenoel.org/api/v1/topics/'+str(id), fields = {'with_user' : True})
  61. liste = json.loads(r.data)
  62. return liste
  63.  
  64. def getAllMessages(self, id) : #ok
  65. """Retourne les infos d'un topic et de son auteur, il faut passer l'id (un int) du topic
  66. "content" pour le contenu d'un message"""
  67. r = http.request('GET', 'https://avenoel.org/api/v1/messages/', fields = {'topic_id' : id})
  68. liste = json.loads(r.data)
  69. return liste
  70.  
  71. def getLastMessages(self, nb) : #ok
  72. """Retourne les infos d'un topic et de son auteur, il faut passer l'id (un int) du topic
  73. "content" pour le contenu d'un message"""
  74. r = http.request('GET', 'https://avenoel.org/api/v1/messages/', fields = {'topic_id' : self.id, 'reverse' : True, 'size' : nb})
  75. liste = json.loads(r.data)
  76. return liste
  77.  
  78. def postMessage(self, id, message) : #ok
  79. if self.token == "" :
  80. print("Pas de token d'api")
  81. r = http.request('POST', 'https://avenoel.org/api/v1/messages', headers = {'X-Authorization' : self.token}, fields = {'topic_id' : id, 'content' : message})
  82. liste = json.loads(r.data)
  83. return liste
  84.  
  85. def send(self, msg) :
  86. if self.id == "" :
  87. print("Pas de blabla")
  88. else :
  89. l = self.postMessage(self.id, msg)
  90.  
  91. def affichage(li, lasti) :
  92. """Affiche une liste de getLastMessages par exemple"""
  93. dernierMessage = ""
  94. if li :
  95. dernierMessage = li["messages"][0]["content"]
  96. tmp = 0
  97. if li["messages"] :
  98. for k in li["messages"] :
  99. if k["content"] == lasti :
  100. break
  101. tmp+=1
  102. for indice in range(tmp) :
  103. k = li["messages"][tmp-indice-1]
  104. tmpPseudo = c.pseudoFromId(k["user_id"])
  105. if lasti == k["content"] :
  106. break
  107. liste = k["content"].split("\n")
  108. for i in liste :
  109. if i and not i[0] == ">" and not i == "\r" :
  110. print("<"+tmpPseudo+">", end = " ")
  111. for k in i.split() :
  112. if not "https://image.noelshack.com/" in k :
  113. print(k, end = " ")
  114. else :
  115. print(k[:k.index("https://image.noelshack.com/")], end = " ")
  116. print("")
  117. return dernierMessage
  118.  
  119.  
  120. c = Client()
  121. c.connection(username, mdp)
  122. c.id = idTopic
  123. timeout = 5 # seconds
  124. last_work_time = 0
  125. lastmsg = ""
  126.  
  127. def treat_input(linein):
  128. c.send(linein)
  129. idle_work()
  130.  
  131. def idle_work():
  132. global last_work_time
  133. now = time.time()
  134. # do some other stuff every REFRESHTIME seconds of idleness
  135. if now - last_work_time > REFRESHTIME :
  136. last_work_time = now
  137. li = c.getLastMessages(10)
  138. global lastmsg
  139. lastmsg = affichage(li, lastmsg)
  140.  
  141. # will hold all input read, until the work thread has chance
  142. # to deal with it
  143. input_queue = queue.Queue()
  144.  
  145. # will signal to the work thread that it should exit when
  146. # it finishes working on the currently available input
  147. no_more_input = threading.Lock()
  148. no_more_input.acquire()
  149.  
  150. # will signal to the work thread that it should exit even if
  151. # there's still input available
  152. interrupted = threading.Lock()
  153. interrupted.acquire()
  154.  
  155. # work thread' loop: work on available input until main
  156. # thread exits
  157. def treat_input_loop():
  158. while not interrupted.acquire(blocking=False):
  159. try:
  160. treat_input(input_queue.get(timeout=timeout))
  161. except queue.Empty:
  162. # if no more input, exit
  163. if no_more_input.acquire(blocking=False):
  164. break
  165. else:
  166. idle_work()
  167.  
  168. work_thread = threading.Thread(target=treat_input_loop)
  169. work_thread.start()
  170.  
  171. # main loop: stuff input in the queue until there's either
  172. # no more input, or the program gets interrupted
  173. try:
  174. for line in sys.stdin:
  175. if line: # optional: skipping empty lines
  176. input_queue.put(line)
  177.  
  178. # inform work loop that there will be no new input and it
  179. # can exit when done
  180. no_more_input.release()
  181.  
  182. # wait for work thread to finish
  183. work_thread.join()
  184.  
  185. except KeyboardInterrupt:
  186. interrupted.release()
  187. input_cleanup()
Advertisement
Add Comment
Please, Sign In to add comment