Skip to content

Instantly share code, notes, and snippets.

@ziot
Created May 5, 2021 05:24
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 ziot/ca727f789b83c016af6e2fdbd6dde03e to your computer and use it in GitHub Desktop.
Save ziot/ca727f789b83c016af6e2fdbd6dde03e to your computer and use it in GitHub Desktop.
import requests, json, sqlite3, time
con = sqlite3.connect('db/memory.db')
cur = con.cursor()
def getAvatars(userStr):
url = "https://api.twitter.com/1.1/users/lookup.json?screen_name={0}".format(userStr)
headers = {
"Cookie": "",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate",
"Content-Type": "application/json",
"X-Twitter-Auth-Type": "OAuth2Session",
"X-Twitter-Client-Language": "en",
"X-Twitter-Active-User": "yes",
"X-Csrf-Token": "",
"Authorization": "",
"Referer": "https://twitter.com/coin_artist/followers"
}
r = requests.get(url, headers = headers)
return json.loads(str(r.text).encode("utf-8"))
def loadUsers(pagination):
limit = 100
users = []
for row in cur.execute('SELECT user_name FROM users ORDER BY user_id ASC LIMIT {0},{1}'.format((pagination*limit), limit)):
users.append(row[0])
userStr = ",".join(users)
users = getAvatars(userStr)
try:
for user in users:
cur.execute('UPDATE users SET twitter_avatar="{0}" WHERE user_name="{1}"'.format(user["profile_image_url_https"], user["screen_name"]))
except:
print("failed:", users)
# save
con.commit()
for x in range(1,501):
loadUsers(x)
print("{0} finished".format(x))
time.sleep(1)
# close connection
con.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment