twitools/twitools/__init__.py

84 lines
2.3 KiB
Python

import dbtools, tweepy, setuptools
class twObject:
def __init__(self, cke = setuptools.cke(), cse = setuptools.cse(), ato = setuptools.ato(), ase = setuptools.ase()):
self.auth = tweepy.OAuthHandler(cke, cse)
self.auth.set_access_token(ato, ase)
self.api = tweepy.API(self.auth)
def retweet(self, id):
self.api.retweet(id)
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, include_entities=True).items())
tweets.reverse()
return tweets
def whoami(self):
return self.auth.get_username()
def tweet(self, text, reply = 0):
return self.api.update_status(text, reply)
def authenticate(self):
try:
authurl = self.auth.get_authorization_url()
except tweepy.TweepError:
return False
print(authurl)
print()
pin = input("PIN: ")
print()
try:
self.auth.get_access_token(pin)
except tweepy.TweepError:
return False
return self.auth.access_token, self.auth.access_token_secret
def getFollowerIDs(section = setuptools.TWITTER):
''' Returns 5,000 follower IDs at most '''
for id in list(twoHelper(section).api.followers_ids(screen_name=twObject().whoami())):
yield int(id)
def getFollowingIDs(section = setuptools.TWITTER):
for id in list(twoHelper(section).api.friends_ids(screen_name=twObject().whoami())):
yield int(id)
def getNameByID(uid, section = setuptools.TWITTER):
return twoHelper(section).api.get_user(uid).screen_name
def getNamesByIDs(fids=getFollowerIDs(), section = setuptools.TWITTER):
for page in setuptools.paginate(fids, 100):
followers = twoHelper(section).api.lookup_users(user_ids=page)
for follower in followers:
yield {"id": follower.id, "name": follower.screen_name}
def getTweet(tid, section = setuptools.TWITTER):
return twoHelper(section).api.get_status(tid)
def twoHelper(section = setuptools.TWITTER):
try:
return twObject(ato = setuptools.ato(section), ase = setuptools.ase(section))
except:
return twObject()
def tweet(text, ref = 0, section = setuptools.TWITTER):
return twoHelper(section).tweet(text, ref)
def twoBotHelper(cid):
db = dbtools.dbHelper()
ato = db.ato(cid)
ase = db.ase(cid)
if ato:
return twObject(ato = db.ato(cid), ase = db.ase(cid))
else:
raise ValueError("Unknown user %s" % cid)