Created
November 26, 2018 04:53
-
-
Save M4cs/8f761e4df6ce7921fac56efd1d51f81a to your computer and use it in GitHub Desktop.
Twitter Info Grabber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def checkconfig(): | |
from configparser import ConfigParser | |
import cfg | |
config = ConfigParser() | |
config.read('config.ini') | |
if config['DEFAULT'] == {}: | |
cfg.addtwitter() | |
else: | |
tack = config['DEFAULT']['twitter_api_consumer_key'] | |
tacs = config['DEFAULT']['twitter_api_consumer_secret'] | |
taat = config['DEFAULT']['twitter_api_access_token'] | |
taas = config['DEFAULT']['twitter_api_access_secret'] | |
return tack, tacs, taat, taas | |
def twitterinfo(abc): | |
import tweepy | |
import re, os | |
tack, tacs, taat, taas = checkconfig() | |
auth = tweepy.OAuthHandler(tack, tacs) | |
auth.set_access_token(taat, taas) | |
api = tweepy.API(auth) | |
userdetails = {} | |
recentactivity = {} | |
try: | |
userinfo = api.get_user(screen_name=abc) | |
except: | |
print("Error! Check Username or API Keys Again...") | |
return recentactivity, userdetails | |
userdetails['User ID'] = userinfo.id | |
userdetails['Followers'] = userinfo.followers_count | |
userdetails['Following'] = userinfo.friends_count | |
userdetails['Geolocation Enabled'] = userinfo.geo_enabled | |
try: | |
userdetails['Homepage'] = userinfo.entities['url']['urls'][0]['display_url'] | |
except KeyError: | |
pass | |
userdetails['Language'] = userinfo.lang | |
userdetails['Num. Of Tweets'] = userinfo.statuses_count | |
userdetails['Profile Description'] = userinfo.description.encode('utf-8') | |
userdetails['Profile Set Location'] = userinfo.location | |
userdetails['Time Zone'] = userinfo.time_zone | |
userdetails['Verified Account'] = userinfo.verified | |
f = open('tmptweets.txt', 'w+') | |
for tweet in tweepy.Cursor(api.user_timeline, id=username).items(1000): | |
f.write(str(tweet.text.encode('utf-8'))) | |
f.write('\n') | |
f = open('tmptweets.txt', 'r') | |
q = f.read() | |
strings = re.findall(r'(?:\#+[\w_]+[\w\'_\-]*[\w_]+)', q) | |
tusers = re.findall(r'(?:@[\w_]+)', q) | |
f.close() | |
os.remove('tmptweets.txt') | |
hashlist = [] | |
userlist = [] | |
for item in strings: | |
item = item.strip('#') | |
item = item.lower() | |
hashlist.append(item) | |
for item2 in tusers: | |
item2 = item2.strip('@') | |
item2 = item2.lower() | |
userlist.append(item2) | |
recentactivity = { | |
'Hashtag Interactions': hashlist[:25], | |
'User Interactions': userlist[:25] | |
} | |
return recentactivity, userdetails | |
def main(abc): | |
import requests | |
checkconfig() | |
r = requests.get('https://twitter.com/%s' % abc) | |
if r.status_code == 200: | |
recentactivity, userdetails = twitterinfo(abc) | |
return [recentactivity, userdetails] | |
else: | |
return None | |
def output(data, abc): | |
from collections import Counter | |
from terminaltables import SingleTable | |
if data and data[0]: | |
hashlist = data[0]['Hashtag Interactions'] | |
userlist = data[0]['User Interactions'] | |
userdetails = data[1] | |
usertable = [['Info', 'Value']] | |
for k,v in userdetails.items(): | |
try: | |
usertable.append([k, str(v)]) | |
except UnicodeEncodeError: | |
print("Error: %s" % UnicodeEncodeError) | |
print("") | |
uitable = [['User', '# Of Interactions']] | |
count = Counter(userlist).most_common() | |
for usr, cnt in count: | |
uitable.append(["@" + usr, str(cnt)]) | |
hashtable = [['Hashtag', '# Of Uses']] | |
counth = Counter(hashlist).most_common() | |
for hasht, cnt in counth: | |
hashtable.append(["#" + hasht, str(cnt)]) | |
table1 = SingleTable(usertable) | |
table2 = SingleTable(uitable) | |
table3 = SingleTable(hashtable) | |
print("User Info") | |
print(table1.table) | |
print("\nRecent User Interactions") | |
print(table2.table) | |
print("\nRecent Hashtage Uses") | |
print(table3.table) | |
else: | |
print("No Twitter User Data Found") | |
print("[i] Enter Username To Search [i]".center(45, " ")) | |
username = input("$ ") | |
result = main(str(username)) | |
output(result, str(username)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment