Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import urllib.request
  2. import json
  3. import datetime
  4. from time import time
  5.  
  6. class Py3status:
  7.  
  8. cache_timeout = 10
  9. cid=""
  10. cookie=''
  11. color_good="#00FF00"
  12. color_bad="#FF0000"
  13.  
  14. def check_hubbit(self, i3s_output_list, i3s_config):
  15. try:
  16. opener = urllib.request.build_opener()
  17. opener.addheaders.append(('Cookie', self.cookie))
  18.  
  19. hubbit_url = 'http://hubbit.chalmers.it/stats/%s.json' % self.cid
  20. response = opener.open(hubbit_url).read()
  21. jsonResponse = json.loads(response.decode('utf-8'))
  22.  
  23. date = datetime.datetime.strptime(jsonResponse['last_session'][0:-10], '%Y-%m-%dT%H:%M:%S')
  24.  
  25. last_session_duration = jsonResponse['last_session_duration']
  26.  
  27. response = {
  28. ' cache_timeout': time() + self.cache_timeout,
  29. 'full_text': "Hubben %dmin" % (last_session_duration/60) if date > datetime.datetime.now() else "Not in hubben :(",
  30. 'color': self.color_good if date > datetime.datetime.now() else self.color_bad
  31. }
  32. except:
  33. response = {
  34. 'cache_timeout': time() + self.cache_timeout,
  35. 'full_text': "Unexpected error"
  36. }
  37.  
  38.  
  39. return response
  40.  
  41.  
  42. if __name__ == "__main__":
  43. from time import sleep
  44. x = Py3status()
  45. config = {
  46. 'color_good': '#00FF00',
  47. 'color_bad': '#FF0000',
  48. 'cid': "gujoel",
  49. 'cookie': "chalmersItAuth=34df98c621bd1a54b8e7675bc69a6ecb83579542"
  50. }
  51. while True:
  52. print(x.check_hubbit([], config))
  53. sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement