Skip to content

Instantly share code, notes, and snippets.

@n1ckfg
Last active November 14, 2022 22:31
Show Gist options
  • Save n1ckfg/df70c6fa1dabac4fe55cb551364adcc5 to your computer and use it in GitHub Desktop.
Save n1ckfg/df70c6fa1dabac4fe55cb551364adcc5 to your computer and use it in GitHub Desktop.
# https://www.geeksforgeeks.org/python-tweepy-getting-the-id-of-a-user/
import tweepy
import json
import time
import urllib.request
# You get these from https://developer.twitter.com/en/apps
# New signups at https://developer.twitter.com
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
def doParse(url, header):
data = None
lines = []
with open(url) as data_file:
data = json.load(data_file)
print("Found " + str(len(data)) + " " + header)
for i, obj in enumerate(data):
try:
user_id = int(obj[header]["accountId"])
user = api.get_user(user_id=user_id)
lines.append(user.screen_name)
lines.append(user.name)
lines.append(user.description)
# This part resolves t.co urls. If you have a t.co url already,
# you can do this without Twitter API access.
try:
req = urllib.request.urlopen(user.url)
lines.append(req.url)
print(" ...Resolved url " + req.url)
except:
print(" ...No url available")
lines.append("")
print("Parsed " + str(i+1) + " / " + str(len(data)) + " " + header)
except:
print("Error parsing " + str(i+1) + " / " + str(len(data)) + " " + header)
time.sleep(1.1) # You need to make less than than 1 API request per second.
with open(header + ".txt", "w") as f:
f.write('\n'.join(lines))
f.close()
# ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
doParse("source/following.json", "following")
doParse("source/follower.json", "follower")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment