twitter bot comcast speed test


SUBMITTED BY: crazyren

DATE: May 24, 2016, 12:07 a.m.

FORMAT: Python 3

SIZE: 2.4 kB

HITS: 706

  1. #!/usr/bin/python
  2. import os
  3. import sys
  4. import csv
  5. import datetime
  6. import time
  7. import twitter
  8. def test():
  9. #run speedtest-cli
  10. print 'running test'
  11. a = os.popen("python /home/pi/speedtest/speedtest-cli --simple").read()
  12. print 'ran'
  13. #split the 3 line result (ping,down,up)
  14. lines = a.split('\n')
  15. print a
  16. ts = time.time()
  17. date =datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
  18. #if speedtest could not connect set the speeds to 0
  19. if "Cannot" in a:
  20. p = 100
  21. d = 0
  22. u = 0
  23. #extract the values for ping down and up values
  24. else:
  25. p = lines[0][6:11]
  26. d = lines[1][10:14]
  27. u = lines[2][8:12]
  28. print date,p, d, u
  29. #save the data to file for local network plotting
  30. out_file = open('/var/www/assets/data.csv', 'a')
  31. writer = csv.writer(out_file)
  32. writer.writerow((ts*1000,p,d,u))
  33. out_file.close()
  34. #connect to twitter
  35. TOKEN=""
  36. TOKEN_KEY=""
  37. CON_SEC=""
  38. CON_SEC_KEY=""
  39. my_auth = twitter.OAuth(TOKEN,TOKEN_KEY,CON_SEC,CON_SEC_KEY)
  40. twit = twitter.Twitter(auth=my_auth)
  41. #try to tweet if speedtest couldnt even connet. Probably wont work if the internet is down
  42. if "Cannot" in a:
  43. try:
  44. tweet="Hey @Comcast @ComcastCares why is my internet down? I pay for 150down\\10up in Washington DC? #comcastoutage #comcast"
  45. twit.statuses.update(status=tweet)
  46. except:
  47. pass
  48. # tweet if down speed is less than whatever I set
  49. elif eval(d)<50:
  50. print "trying to tweet"
  51. try:
  52. # i know there must be a better way than to do (str(int(eval())))
  53. tweet="Hey @Comcast why is my internet speed " + str(int(eval(d))) + "down\\" + str(int(eval(u))) + "up when I pay for 150down\\10up in Washington DC? @ComcastCares @xfinity #comcast #speedtest"
  54. twit.statuses.update(status=tweet)
  55. except Exception,e:
  56. print str(e)
  57. pass
  58. return
  59. if __name__ == '__main__':
  60. test()
  61. print 'completed'

comments powered by Disqus