3 from datetime
import datetime
4 from urllib
import urlencode
5 from feed
import HtmlFeed
, XmlFeed
, get_json
6 from utils
import unescape
8 RE_YOUTUBE_DURATION
= re
.compile(r
'P((?:\d)+D)?T?(\d+H)?(\d+M)?(\d+S)?')
12 def __init__(self
, api_key
, yt_delay
):
13 self
.api_key
= api_key
14 self
.yt_delay
= yt_delay
15 self
.yt_cache
= {} # {videoId: {'ts': datetime-timestamp, 'payload': data}
,... }
16 self
.yt_chanvids
= {} # {channel: {videoId: datetime-timestamp, ...}
,... }
18 def _check_link_eligibility(self
, channel
, videoId
):
19 if channel
not in self
.yt_chanvids
:
20 self
.yt_chanvids
[channel
] = { videoId: datetime.now() }
21 elif videoId
not in self
.yt_chanvids
[channel
]:
22 self
.yt_chanvids
[channel
][videoId
] = datetime
.now()
24 if (datetime
.now() - self
.yt_chanvids
[channel
][videoId
]).total_seconds() < self
.yt_delay
:
27 self
.yt_chanvids
[channel
][videoId
] = datetime
.now()
30 def search(self
, query
, userip
=None):
31 url
= 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&'
32 parameters
= {'q': query}
34 parameters
['userip'] = userip
35 url
+= urlencode(parameters
)
39 def image_search(self
, query
, userip
=None):
40 url
= 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&'
41 parameters
= {'q': query}
43 parameters
['userip'] = userip
44 url
+= urlencode(parameters
)
48 def yt_search(self
, query
, num
=0, userip
=None):
49 url
= 'https://www.googleapis.com/youtube/v3/search?part=snippet&order=relevance&type=video&maxResults=5&'
50 parameters
= {'q': query, 'key': self.api_key}
52 parameters
['userip'] = userip
54 url
+= urlencode(parameters
)
58 video_info
= js
['items'][num
]
60 return self
.yt_video(video_info
['id']['videoId'], userip
)
62 def yt_video(self
, video
, userip
=None):
63 for entry
in self
.yt_cache
.keys():
64 if (datetime
.now() - self
.yt_cache
[entry
]['ts']).total_seconds() > 300: # Cache data for 5 minutes
65 del self
.yt_cache
[entry
]
67 return self
.yt_cache
[video
]['payload']
69 url
= 'https://www.googleapis.com/youtube/v3/videos?&part=snippet,contentDetails,statistics&'
70 parameters
= {'id': video, 'key': self.api_key}
72 parameters
['userip'] = userip
74 url
+= urlencode(parameters
)
76 video_info
= js
['items'][0]
77 m
= RE_YOUTUBE_DURATION
.search(video_info
['contentDetails']['duration'])
78 days
, hours
, minutes
, seconds
= map(lambda x
: int(x
[:-1]) if x
else 0, m
.groups())
79 stats
= video_info
['statistics'] if 'statistics' in video_info
else {'favoriteCount': 0, 'viewCount': 0, 'likeCount': 0, 'dislikeCount': 0}
82 'title': video_info
['snippet']['title'],
83 'duration': 86400 * days
+ 3600 * hours
+ 60 * minutes
+ seconds
,
84 'uploaded': video_info
['snippet']['publishedAt'],
85 'id': video_info
['id'],
86 'favorite_count': int(stats
['favoriteCount']),
87 'view_count': int(stats
['viewCount']),
88 'liked': int(stats
['likeCount']) if 'likeCount' in stats
else 0,
89 'disliked': int(stats
['dislikeCount']) if 'dislikeCount' in stats
else 0
91 self
.yt_cache
[video
] = {'ts': datetime.now(), 'payload': data}