kumistatus/twitools.py

60 lines
1.2 KiB
Python
Raw Normal View History

2016-03-16 12:44:50 +00:00
import configparser, tweepy
class SetupException(Exception):
def __str__(self):
return "Seems like config.cfg has not been created yet. Run setup.py to do so."
def getSetting(section, setting):
config = configparser.RawConfigParser()
config.read('config.cfg')
return config.get(section, setting)
def cke():
try:
return getSetting("Twitter", "cke")
except:
raise SetupException()
def cse():
try:
return getSetting("Twitter", "cse")
except:
raise SetupException()
def ato():
try:
return getSetting("Twitter", "ato")
except:
raise SetupException()
def ase():
try:
return getSetting("Twitter", "ase")
except:
raise SetupException()
def user():
return twObject().whoami()
class twObject:
def __init__(self, cke = cke(), cse = cse(), ato = ato(), ase = ase()):
self.auth = tweepy.OAuthHandler(cke, cse)
self.auth.set_access_token(ato, ase)
self.api = tweepy.API(self.auth)
def delete(self, id):
self.api.destroy_status(id)
def search(self, query, savepoint = 0):
tweets = list(tweepy.Cursor(self.api.search, q=query, since_id=savepoint).items())
tweets.reverse()
return tweets
def tweet(self, text):
self.api.update_status(text)
def whoami(self):
return self.auth.get_username()