Skip to content

Instantly share code, notes, and snippets.

@void4
Created June 6, 2023 13:17
Show Gist options
  • Save void4/575abad9130217aaa3a008af0e53c3c2 to your computer and use it in GitHub Desktop.
Save void4/575abad9130217aaa3a008af0e53c3c2 to your computer and use it in GitHub Desktop.
# Python 3.9+
# You need to get and put your API key here https://developers.google.com/youtube/v3/getting-started
APIKEY = "XYZYOURSECRETAPIKEYXYZ"
from time import sleep
import csv
import requests
f = open("subscriptions.csv")
subs = []
found = 0
notfound = 0
try:
for r, row in enumerate(csv.reader(f)):
if r == 0 or not row:
continue
cid, link, username = row
URL = f"https://youtube.googleapis.com/youtube/v3/channels?part=statistics&id={cid}&key={APIKEY}"
headers = {
"Accept": "application/json",
}
result = requests.get(URL, headers=headers)
j = result.json()
if "items" not in j or len(j["items"]) == 0:
notfound += 1
continue
print(row, j)
stats = j["items"][0]["statistics"]
subs.append([int(stats["subscriberCount"]), username, int(stats["viewCount"]), int(stats["videoCount"]), link])
found += 1
except KeyboardInterrupt:
pass
print(f"Found: {found} Not found: {notfound}")
subs = list(sorted(subs, key=lambda sub:sub[0], reverse=True))
out = open("result.csv", "w+")
writer = csv.writer(out)
for sub in subs:
line = "\t".join([str(s) for s in sub])
print(line)
writer.writerow([sub[1], f"subs: {sub[0]} views: {sub[2]} videos: {sub[3]}", sub[4]])
out.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment