official.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import urllib.request #importing to use its urlencode function
  2. import json #for decoding a JSON response
  3. #
  4. def get_infoString(ChannelIdentifier):
  5. API_KEY = 'AIzaSyDuwkgFVRLOa3gkBU4aeDjVBuogLQ1ZZXE' # What? How? Learn here: https://www.youtube.com/watch?v=JbWnRhHfTDA
  6. ch_url = 'https://www.googleapis.com/youtube/v3/channels?part=statistics&id='+ChannelIdentifier+'&key='+API_KEY
  7. ch_response = urllib.request.urlopen(ch_url) #makes the call to YouTube
  8. ch_info = json.load(ch_response)
  9. subscribes = ch_info['items'][0]['statistics']['subscriberCount']
  10. url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId='+ChannelIdentifier+'&maxResults=150&type=video&key='+API_KEY
  11. response = urllib.request.urlopen(url) #makes the call to YouTube
  12. videos = json.load(response) #decodes the response so we can work with it
  13. videoMetadata = [] #declaring our list
  14. for video in videos['items']:
  15. if video['id']['kind'] == 'youtube#video':
  16. videoMetadata.append(video['id']['videoId']) #Appends each videoID and link to our list
  17. #
  18. # In this second part, a loop will run through the listvideoMetadata
  19. # During each step the details a specific video are retrieved and displayed
  20. # The structure of the API-return can be tested with the API explorer (which you can excecute without OAuth):
  21. # https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.videos.list?part=snippet%252CcontentDetails%252Cstatistics&id=Ks-_Mh1QhMc&_h=1&
  22. #
  23. vlist = []
  24. for metadata in tqdm(videoMetadata,leave=False):
  25. SpecificVideoID = metadata
  26. SpecificVideoUrl = 'https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id='+SpecificVideoID+'&key='+API_KEY
  27. response = urllib.request.urlopen(SpecificVideoUrl) #makes the call to a specific YouTube
  28. videos = json.load(response) #decodes the response so we can work with it
  29. videoMetadata = [] #declaring our list
  30. for video in videos['items']:
  31. if video['kind'] == 'youtube#video':
  32. #print(video['statistics'].keys())
  33. '''
  34. print("Upload date: "+video['snippet']['publishedAt']) # Here the upload date of the specific video is listed
  35. print("Number of views: "+video['statistics']['viewCount']) # Here the number of views of the specific video is listed
  36. print("Number of likes: "+video['statistics']['likeCount']) # etc
  37. #print("Number of dislikes: "+video['statistics']['dislikeCount'])
  38. print("Number of favorites:"+video['statistics']['favoriteCount'])
  39. print("Number of comments: "+video['statistics']['commentCount'])
  40. print("\n")
  41. '''
  42. commentCount = 0
  43. try:
  44. commentCount=video['statistics']['commentCount']
  45. except:
  46. pass
  47. likeCount=0
  48. try:
  49. likeCount = video['statistics']['likeCount']
  50. except:
  51. pass
  52. vlist.append([video['snippet']['channelTitle'],subscribes,video['snippet']['description'],video['snippet']['title']
  53. ,video['snippet']['publishedAt'],video['statistics']['viewCount'],likeCount
  54. ,video['statistics']['favoriteCount'],commentCount])
  55. return vlist
  56. import csv
  57. from youtubesearchpython import *
  58. from tqdm import tqdm
  59. import time
  60. pages = 100
  61. max_channels = 8 * pages
  62. search = ChannelsSearch('科技', limit = 8)
  63. c_list = []
  64. print('Filtering Channels................')
  65. for p in tqdm(range(pages)):
  66. for channel in search.result()['result']:
  67. if channel['subscribers'] is None:
  68. continue
  69. subscribes = channel['subscribers'].replace(' subscribers','').replace(' subscriber','')
  70. if 'K' in subscribes:
  71. subscribes = subscribes.replace('K','')
  72. if '.' in subscribes :
  73. subscribes=subscribes.replace('.','')
  74. subscribes+='00'
  75. else:
  76. subscribes+='000'
  77. if 'M' in subscribes:
  78. subscribes = subscribes.replace('M','')
  79. if '.' in subscribes :
  80. zeros = len(subscribes)-subscribes.index('.')-1
  81. subscribes=subscribes.replace('.','')
  82. subscribes += '000000'[:6-zeros]
  83. if int(subscribes)>500:
  84. c_list.append(channel)
  85. search.next()
  86. with open('youtubeReport.csv', 'w', newline='',encoding='UTF-8') as csvfile:
  87. writer = csv.writer(csvfile)
  88. writer.writerow(['channelTitle','subscribes', 'description', 'videoTitle','publishedAt','viewCount','likeCount','favoriteCount','commentCount'])
  89. for ch in tqdm(c_list):
  90. channel_id = ch['id']
  91. vlist = get_infoString(channel_id)
  92. for v in vlist:
  93. writer.writerow([v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7]])