-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathyoutube.py
58 lines (50 loc) · 2.24 KB
/
youtube.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
""" Simple Youtube Analytics """
from datetime import date
import requests
from database import Database
import helpers
class Youtube():
""" A YouTube analytics object """
def __init__(self):
data_file = open("secrets/youtube.txt", "r")
self.channel_id = data_file.readline()
self.key = data_file.readline()
self.database = Database()
def create_table(self):
""" Creates table in sqlite database for youtube """
columns = [{"name": "subscribers", "type": "int"},
{"name": "date", "type": "datetime default current_timestamp"}]
self.database.create_table("youtube", columns)
def subscribers(self):
""" Get number of subs, daily subs, print out """
subscribers = self.get_subscribers()
self.write_subscribers_to_db(subscribers)
daily_subscriptions = self.daily_subscribers()
daily_sub_string = helpers.pretty_num_to_s(daily_subscriptions)
print("YouTube")
print("{0} Subscribers ({1})".format(helpers.num_to_s(int(subscribers)), daily_sub_string))
def get_subscribers(self):
""" Get the number of subscribers to channel """
payload = {"part": "statistics",
"channel": self.channel_id,
"key": self.key,
"forUsername": "MrFish235"}
url = "https://www.googleapis.com/youtube/v3/channels"
request = requests.get(url, params=payload)
return request.json()["items"][0]["statistics"]["subscriberCount"]
def write_subscribers_to_db(self, subscribers):
""" Write the number of subscribers to the database """
self.database.cursor.execute("INSERT INTO youtube (subscribers) VALUES({0})"\
.format(subscribers))
self.database.connection.commit()
def daily_subscribers(self):
""" Get the number of subscribers since midnight """
daily_data = self.database.cursor\
.execute("SELECT * from youtube WHERE date >= '{0}'"\
.format(date.today())).fetchall()
first = daily_data[0]
last = daily_data[-1]
return last[0] - first[0]
if __name__ == "__main__":
YOUTUBE = Youtube()
YOUTUBE.subscribers()