· 6 years ago · Feb 19, 2020, 12:16 AM
1# Author: Cheryl Dugas
2#Modifications by: Paul Chua
3# youtube_data.py searches YouTube for videos matching a search term
4
5# To run from terminal window: python3 youtube_data.py
6
7from apiclient.discovery import build # use build function to create a service object
8
9import unidecode # need for processing text fields in the search results
10import csv
11
12# put your API key into the API_KEY field below, in quotes
13API_KEY = "AIzaSyCliSsy61LY49g9num-HSryF1jkKOf7fyk"
14
15API_NAME = "youtube"
16API_VERSION = "v3" # this should be the latest version
17
18# function youtube_search retrieves the YouTube records\
19class Result:
20 def __init__(self,title,videoId,viewCount,likeCount,dislikeCount,commentCount):
21 self.title = title
22 self.videoId = videoId
23 self.viewCount = viewCount
24 self.likeCount = likeCount
25 self.dislikeCount = dislikeCount
26 self.commentCount = commentCount
27
28#Determines the top 5 videos based off views
29def views(rlist):
30
31 newlist = sorted(rlist, key=lambda x: int(x.viewCount), reverse=True)
32 print\
33 ("Top 5 videos with the highest views")
34 for x in range(0,4):
35 print("Title: " + newlist[x].title + " Video ID: "+ newlist[x].videoId+ " View Count: " + newlist[x].viewCount )
36
37def like(rlist):
38 newlist = sorted(rlist, key=lambda x: int(x.likeCount)/int(x.viewCount), reverse=True)
39 print\
40 ("Top 5 results with the highest Like percentage")
41 for x in range(0,4):
42 likep = int(newlist[x].likeCount)/int(newlist[x].viewCount)
43 print("Title: " + newlist[x].title + " Video ID: "+ newlist[x].videoId+ " Like Percentage: " + str(likep))
44
45def dlike(rlist):
46 newlist = sorted(rlist, key=lambda x: int(x.likeCount)/int(x.viewCount), reverse=True)
47 print\
48 ("Top 5 videos based off dislike percentage")
49 for x in range(0,4):
50 dlikep = int(newlist[x].dislikeCount)/int(newlist[x].viewCount)
51
52 print((x+1)+":" + "Title: " + newlist[x].title + " Video ID: "+ newlist[x].videoId+ " Dislike Percentage: " + str(dlikep))
53
54def write(data):
55 fields = ['Title', 'videoID', 'viewCount', 'likeCount','dislikeCount','commentCount']
56 filename = "answer.csv"
57 # writing to csv file
58 with open(filename, 'w') as csvfile:
59 csvwriter = csv.writer(csvfile)
60 csvwriter.writerow(fields)
61 csvwriter.writerows(data)
62
63def youtube_search(s_term, s_max):
64 print("Top " + s_max + " results for "+ s_term)
65 youtube = build(API_NAME, API_VERSION, developerKey=API_KEY)
66 search_response = youtube.search().list(q=s_term, part="id,snippet", maxResults=s_max).execute()
67 result = []
68 analysis = []
69 # search for videos matching search term;
70 for search_result in search_response.get("items", []):
71 if search_result["id"]["kind"] == "youtube#video":
72 title = search_result["snippet"]["title"]
73 title = unidecode.unidecode(title)
74 videoId = search_result["id"]["videoId"]
75 video_response = youtube.videos().list(id=videoId,part="statistics").execute()
76 for video_results in video_response.get("items",[]):
77 viewCount = video_results["statistics"]["viewCount"]
78 if 'likeCount' not in video_results["statistics"]:
79 likeCount = 0
80 else:
81 likeCount = video_results["statistics"]["likeCount"]
82 if 'dislikeCount' not in video_results["statistics"]:
83 dislikeCount = 0
84 else:
85 dislikeCount = video_results["statistics"]["dislikeCount"]
86 if 'commentCount' not in video_results["statistics"]:
87 commentCount = 0
88 else:
89 commentCount = video_results["statistics"]["commentCount"]
90 #This should get pushed to an ArrayList
91 print(title,videoId,viewCount,likeCount,dislikeCount,commentCount)
92 #This puts the results as a Result object so its easier to perform an analysis on them
93 analysis.append(Result(title,videoId,viewCount,likeCount,dislikeCount,commentCount))
94 #CSV writing of result goes here
95 write(result)
96 #Perform all the analysis on the results
97 views(analysis)
98 like(analysis)
99 dlike(analysis)
100
101#Ask the user for their search term and max
102search_term = input("enter a search term \n")
103search_max = input("enter a max\n")
104
105#Calls the function
106youtube_search(search_term, search_max)