Skip to content

Instantly share code, notes, and snippets.

@QuantumFractal
Created February 4, 2023 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuantumFractal/cd1f7cdd5fa15bf422307bcadb4f8dd5 to your computer and use it in GitHub Desktop.
Save QuantumFractal/cd1f7cdd5fa15bf422307bcadb4f8dd5 to your computer and use it in GitHub Desktop.
Tom's First Python Script
#Twitter Bot Cronjob
#Using the python-twitter API
#[tm]
#
#Debug flag
debug_FLAG = False
#Import the Twitter API, Weather API, and time module
import twitter
import pywapi
import time, string
#Create a Client with the bot's credentials
api = twitter.Api(consumer_key='...',
consumer_secret='...',
access_token_key='...',
access_token_secret='...')
#Defines the function for posting a status
def postStatus(status):
#status uses default api setting also deletes duplicates! woot
DD = False
status = str(status)
feed = api.GetUserTimeline('GardnerTweetBot')
for x in xrange(0,len(feed)):
if status == feed[x].text:
api.DestroyStatus(feed[x].id)
debug("Duplicate Status Deleted {ID."+str(feed[x].id)+"}")
DD = True
else:
pass
update = api.PostUpdate(status, None, 38.811013,-94.927207)
debug("[Posted] \'"+update.text+"\'")
def debug(string):
if debug_FLAG == True:
print("[Debug]: "+string)
else:
pass
def getMentionStatus(user):
feed = api.GetUserTimeline(user)
feed = [s.text for s in feed]
feed = str(feed)
debug(feed)
try:
start = feed.index("@GardnerTweetBot >")
end = feed.index("\',")
feed = feed[start+len("@GardnerTweetBot >"):end]
return command
except ValueError:
debug("Status Not Found")
pass
#sets up exit flag
signoff = "."
#Gets weather and prints it in a string
yahoo_result = pywapi.get_weather_from_yahoo('66061', '')
forecast = string.lower(yahoo_result['forecasts'][1]['text'])
current = string.lower(yahoo_result['condition']['text'])
high = yahoo_result['forecasts'][1]['high']
low = yahoo_result['forecasts'][1]['low']
if current == 'wintry mix':
current = 'a wintry mix'
#else:
#forecast = string.lower(forecast[3:])
if current.endswith('y') == True or current == 'fair': #current in itislist:
if forecast.endswith('y') == True:
tweet1 = ("Currently in Gardner, KS, it is " + current + " and it is " + yahoo_result['condition']['temp'] + "F. Tomorrow it will be "+forecast+" with a high of "+high+"F and a low of "+low+"F" +signoff)
else:
tweet1 = ("Currently in Gardner, KS, it is " + current + " and it is " + yahoo_result['condition']['temp'] + "F. Tomorrow there will be "+forecast+" with a high of "+high+"F and a low of "+low+"F"+signoff)
else:
#if string.lower(yahoo)result['condition']['text'] != 'cloudy' and 'partly cloudy' and 'mostly sunny':
if forecast.endswith('y') == True or current == 'fair':
tweet1 = ("Currently in Gardner, KS, there is " + current + " and it is " + yahoo_result['condition']['temp'] + "F. Tomorrow it will be "+forecast+"and a high of "+high+"F and a low of "+low+"F"+signoff)
else:
tweet1 = ("Currently in Gardner, KS, there is " + current + " and it is " + yahoo_result['condition']['temp'] + "F. Tomorrow there will be "+forecast+" with a high of "+high+"F and a low of "+low+"F"+signoff)
#Checks if theres a duplicate tweet, then deletes it
#postStatus, needs to be updated
postStatus(tweet1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment