Advertisement
Guest User

Untitled

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