-
Notifications
You must be signed in to change notification settings - Fork 2
/
youtubeGraph.py
108 lines (102 loc) · 3.63 KB
/
youtubeGraph.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
## @file youtubeGraph.py
#
# @brief this file contains retrival of youtube related information
#
# @author JunHao
#
# @section libraries_main Libraries/Modules
# - youtube (local)
# - for crawling youtube
# - database(local)
# - access to database class
# - datetime
# - to get today date
# - itertools
# - to flatten 2d list.
#imports
from youtube import *
from database import database
import datetime
import itertools
## Documentation for setYoutubeChannelStats Method
# This method store data retrive from crawler into the database.
def setYoutubeChannelStats(channelUrl):
"""! Get youtube channel information and save to database
@param channelUrl;
"""
youtube = Channel()
data = []
data.append(str(datetime.date.today()))
temp = youtube.searchurl(channelUrl)
for i in range(len(temp)):
data.append(temp[i])
db = database('youtube')
uid = db.getUniqueID(channelUrl)
db.createTable(uid,'date','created','Vidcount','Subcount','Totalview')
db.insertTable(data,uid,'date','created','Vidcount','Subcount','Totalview')
db.dbClose()
## Documentation for getYoutubeChannelStats Method
# This method retrive data the database.
def getYoutubeChannelStats(channelUrl):
"""! Get youtube channel information from database
@param channelUrl;
@return data; youtube channel stats. Date created, Vidcount, Subcount, TotalViews
"""
db = database('youtube')
uid = db.getUniqueID(channelUrl)
data = db.getTableData(uid)
db.dbClose()
data = data.pop()
data.pop(0)
return data #return latest channel stats.
##Documentation for getYoutubeTrends Method
# This method fetch youtube trend views in each trending category
def getYoutubeTrends(countryCode = "SG"):
"""! Get trending category information
@param countryCode; Default SG. Supports MY and PH as well
@return trendData; List type:[('totalmusicviews', #), ('totalsportsviews', #), ('totalgamesviews', #), ('totalnewsviews', #)]
"""
youtube = youtubeVid()
youtube.getTrendingVideo()
trendData = youtube.getDBvids(countryCode)
return trendData
##Documenetation for setRevenueData Method
# This method fetch the latest 12 months revenue data from a channel and save it to the database.
def setRevenueData(channelUrl):
"""! Get channel past 12 months revenue data and save to the database.
@param channelUrl;
"""
data = []
data.append(str(datetime.date.today()))
db = database("youtubeRevenue")
uid = db.getUniqueID(channelUrl)
db.createTable(uid,'Date','jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec')
youtube = Channel()
result = youtube.getRevenueData(channelUrl)
print(result)
result.pop(0) #get rid of amount of videos in said months.
result = result[0]
for i in range(0,len(result)):
data.append(result[i])
db.insertTable(data,uid,'Date','jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec')
db.dbClose()
##Documenetation for getRevenueData Method
# This method fetch the latest 12 months revenue data for a channel from the database.
def getRevenueData(channelUrl):
"""! Get channel most recently crawled past 12 months revenue data
@param channelUrl;
@return result; where ["Data",int janValue,...,int decValue]
"""
db = database("youtubeRevenue")
uid = db.getUniqueID(channelUrl)
data = db.getTableData(uid)
db.dbClose()
if len(data) > 1:
data = data.pop()
else:
data = list(itertools.chain(*data)) #convert to 1d list
result = []
result.append(data[0])
for i in range(1,len(data)):
result.append(int(float(data[i])))
return result