Skew

WykopAPI Get Notifications

Aug 8th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.26 KB | None | 0 0
  1. ========================== CAŁA KLASA ======================================================
  2.  
  3. class Wykop:
  4.         lastcheck = 0
  5.         normaldelay = 100
  6.         activedelay = 20
  7.         cache = None
  8.         api = None
  9.  
  10.         def __init__(self):
  11.                 self.api = _wykop.WykopAPI("AlSmtKUGQu", "TcR5hevkSV")
  12.                 self._auth()
  13.                 thread.start_new_thread(self.update,())
  14.  
  15.         def update(self):
  16.                 while True:
  17.                         sleep(1)
  18.                         if self.getNextRefresh() <= 0:
  19.                                 self.refresh()
  20.  
  21.         def getNextRefresh(self):
  22.                 delaj = self.normaldelay
  23.                 if self.cache != None and len(self.cache) > 0:
  24.                         delaj = self.activedelay
  25.                 return round(delaj-(time()-self.lastcheck),0)
  26.  
  27.         def setDelay(self, normal, active):
  28.                 self.normaldelay = normal
  29.                 self.activedelay = active
  30.  
  31.         def _auth(self):
  32.                 self.api.authenticate("skew","6QauQGs4yC76dU4gS1U5")
  33.  
  34.         def refresh(self):
  35.                 try:
  36.                         print "Refreshing wykop"
  37.                         self.lastcheck = time()
  38.                         wykopresult = self.api.request("mywykop","notifications")
  39.                         self.cache = []
  40.                         for key in wykopresult:
  41.                                 if key.new == True:
  42.                                         if key.type == "register":
  43.                                                 self.cache.append(key.author+"\nutworzyl konto")
  44.                                         elif key.type == "observe":
  45.                                                 self.cache.append(key.author+"\nobserwuje cie")
  46.                                         elif key.type == "unobserve":
  47.                                                 self.cache.append(key.author+"\nnie obserwuje")
  48.                                         elif key.type == "entry_comment_directed":
  49.                                                 self.cache.append(key.author+"\ndodal komentarz")
  50.                                         elif key.type == "entry_directed":
  51.                                                 self.cache.append(key.author+"\nwola cie!")
  52.                                         elif key.type == "entry_tag":
  53.                                                 self.cache.append(key.author+"\nnapisal wpis")
  54.                                         elif key.type == "link_comment_directed":
  55.                                                 self.cache.append(key.author+"\nwola cie!")
  56.                                         elif key.type == "link_promoted":
  57.                                                 self.cache.append("Twoj link jest\nna glownej!")
  58.                                         elif key.type == "link_directed":
  59.                                                 self.cache.append(key.author+"\ndodal link dla")
  60.                                         elif key.type == "system":
  61.                                                 self.cache.append("Nowy komunikat\nsystemowy!")
  62.                                         elif key.type == "badge":
  63.                                                 self.cache.append("Masz nowe\nodznaczenie")
  64.                                         elif key.type == "support_answer":
  65.                                                 self.cache.append("Nowa odpowiedz\n pomocy!")
  66.                                         elif key.type == "pm":
  67.                                                 self.cache.append(key.author+" napisal ci PW")
  68.                         print "Wykop refresh complete"
  69.                 except:
  70.                         self._auth()
  71.  
  72.         def get(self, ref=False):
  73.                 if ref == True or self.cache == None:
  74.                         self.refresh()
  75.                 return self.cache
  76.  
  77. ============================================== UŻYCIE ==========================================================================
  78. wykop = Wykop()
  79. while True:
  80.         wykopdata = wykop.get()
  81.         if len(wykopdata) > 0:
  82.                 lcd.clear()
  83.                 lcd.writeMsg(wykopdata[0],0.1)
  84.                 sleep(10)
  85.  
  86.         # Co by CPU nie zabić
  87.         time.sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment