-
Notifications
You must be signed in to change notification settings - Fork 0
/
Finder.py
135 lines (101 loc) · 4.26 KB
/
Finder.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python
from apiclient.discovery import build
from apiclient.errors import HttpError
# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
# tab of
# https://cloud.google.com/console
# Please ensure that you have enabled the YouTube Data API for your project.
class Finder:
DEVELOPER_KEY = ""
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
def __init__(self, dev_key):
self.DEVELOPER_KEY = dev_key
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
self.youtube = build(self.YOUTUBE_API_SERVICE_NAME, self.YOUTUBE_API_VERSION,
developerKey=self.DEVELOPER_KEY)
def youtube_search(self, **kwargs):
# Call the search.list method to retrieve results matching the specified
# query term.
if "q" in kwargs and kwargs['q']:
search_response = self.youtube.search().list(
q=kwargs['q'],
part="id,snippet",
channelId=kwargs['channelId'],
order=kwargs['order'],
publishedAfter=kwargs['publishedAfter']
).execute()
else:
search_response = self.youtube.search().list(
part="id,snippet",
channelId=kwargs['channelId'],
order=kwargs['order'],
publishedAfter=kwargs['publishedAfter']
).execute()
videos = []
# Add each result to the appropriate list, and then display the lists of
# matching videos, channels, and playlists.
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
videos.append(
{
"id": search_result["id"]["videoId"],
"tittle": search_result["snippet"]["title"],
"published_at": search_result["snippet"]["publishedAt"]
}
)
return videos
"""search for given channel without the unwanted words"""
def search(self, channelId, unwanted_words, starting_date):
# adds a - to every unwanted word. its a logical not operator for google
if unwanted_words:
unwanted_words = [" -" + s for s in unwanted_words]
unwanted_words = "a|e|i|o|u " + "".join(unwanted_words)
else:
unwanted_words = False
try:
return self.youtube_search(
q=unwanted_words,
channelId=channelId,
type="video",
part="snippet",
order="date",
publishedAfter=starting_date
)
except HttpError as e:
print("An HTTP error %d occurred:\n %s") % (e.resp.status, e.content)
def get_channel_id(self, channel_name):
channel_name.replace(" ", "%20")
search_response = self.youtube.search().list(
q=channel_name,
part="snippet",
type="channel"
).execute()
channels = search_response.get("items", [])
if len(channels) > 1:
print("multiple channels were found choose one from: \n")
for id, channel in enumerate(channels):
channel_data = "%s> %s: %s\n" % (`id`, channel['snippet']['title'], channel['snippet']['description'])
print channel_data.encode('ascii', "ignore")
choosen_channel = raw_input("Type the number of the choosen one: ")
return channels[int(choosen_channel)]['snippet']['channelId']
if len(channels) == 1:
return channels[0]['id']['channelId']
print("No channel was found\n")
return False
def get_channels_with_name(self, channel_name):
search_response = self.youtube.search().list(
q=channel_name,
part="snippet",
type="channel"
).execute()
return search_response.get("items", [])
def get_channel_data(self, channel_id):
search_response = self.youtube.search().list(
q=channel_id,
part="snippet",
type="channel"
).execute()
channel_data = search_response.get("items", [])[0]
return channel_data