I want to use Tweets sent out by a twitter account as a command to shut down Ubuntu. For this, I've written a Python script using tweepy, a Python library for Twitter. I scheduled the script to run half past the hour every hour (for eg at 5:30, 6:30, and so on) using
Here's the script:
import tweepy import os from datetime import datetime p=None api= tweepy.API() for status in api.user_timeline('tweetneag4'): while p==None: #so that it checks only the first tweet if status.text.count(str((datetime.now().hour)+1))< 2 and str((datetime.time.now().hour)+1)==10: p=1 pass elif str((datetime.now().hour)+1) in status.text: print "System shut down in 20 mins." os.system('shutdown -h +20') p=1
Here is a sample tweet:
#loadshedding schedule in 10mins: 15:00-21:00 #group4 #nea
The twitter account (@tweetneag4) tweets an alert 30 mins and 10 mins before the power outage begins (here, the account tweets if and only if there's a power outage next hour). What I've tried to do is check at 5:30 whether it posts a tweet regarding 6 o'clock. I've done this by seeing if the tweet contains the string "6"(when checking at 5). When checking for ten o'clock, an additional complication arises because it may contain "10" in two scenarios- first as in 10 o'clock and second as in 10mins later. (I'm sure there's a better way of doing this)
If you see the twitter feed, things should be clearer. How could I improve so that the code become more elegant?