-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsubcount v2git.py
56 lines (40 loc) · 1.5 KB
/
subcount v2git.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 20 10:55:46 2020
@author: herald jose
"""
from urllib.request import urlopen
import json
#provide API key here
key='your_API_key_here'
def channelid(ans):
#to eliminate spaces between search queries with %20
str1='%20'.join([str(ele) for ele in ans])
#Use yt API to give search results for username
site1=urlopen('https://www.googleapis.com/youtube/v3/search?part=snippet&q='+str1 +'&type=channel&key='+key)
#loads data to a json file format
a1 = json.load(site1)
#to get channelid and channelname using username
ucid=str(a1.get("items")[0].get("id").get('channelId'))
channelname=a1.get("items")[0].get("snippet").get('title')
#returns channelid & channelname
return(ucid,channelname)
def returnurl(ucid):
#creates and returns url for statistics usin channelid
u='https://www.googleapis.com/youtube/v3/channels?id='+ucid+'&key='+key+'&part=statistics'
return(u)
def fetchsubs(url):
site=urlopen(url)
a = json.load(site)
#returns subs count
return(int(a.get("items")[0].get("statistics").get("subscriberCount")))
def printresult(subs,username):
print(username,'Has' , str(subs) ,'Subscribers!!' )
def main():
ans1=str(input('Input Username : ' )).split()
channel_id,name=channelid(ans1)
url=returnurl(channel_id)
subs=fetchsubs(url)
printresult(subs,name)
if __name__ == '__main__':
main()