I'm using the Youtube API to search using a keyword query:
import gdata.youtube
import gdata.youtube.service
def youtube_query(query_text,
max_results=50,
start_index=1,
racy='exclude',
orderby='relevance'):
client = gdata.youtube.service.YouTubeService()
query = gdata.youtube.service.YouTubeVideoQuery()
query.vq = query_text
query.max_results = max_results
query.start_index = start_index
query.racy = racy
#query.format = 5
query.orderby = orderby
feed = client.YouTubeQuery(query)
resultsCount = int(feed.tota开发者_如何学运维l_results.text)
entries = []
try:
while resultsCount > int(query.start_index):
print repr(feed.entry)
entries += feed.entry
query.start_index = int(query.start_index) + int(query.max_results)
feed = client.YouTubeQuery(query)
except gdata.service.RequestError:
#
# Cannot request beyond 1000 items.
#
pass
return entries
Each entry is a YoutubeVideoEntry.
The date each video is uploaded should definitely be accessible, as it is displayed for every video:
I need the time as well. Does anybody know if this can be done?
You can use the entry.published.text
attribute:
import datetime
import time
feed = client.YouTubeQuery(query)
for entry in feed.entry:
_tmp = time.strptime(entry.published.text, '%Y-%m-%dT%H:%M:%S.000Z')
ptime = datetime.datetime(*_tmp[:6])
print ptime
print ptime.time()
Output:
2010-01-27 19:42:42
19:42:42
You can also use the parse_date function in the iso8601 library, which will return a date object:
import iso8601
uploaded_time = iso8601.parse_date("2007-01-25T12:00:00Z")
print uploaded_time
Output:
datetime.datetime(2013, 8, 21, 20, 54, 34, tzinfo=<iso8601.iso8601.Utc object at 0x1045d7b10>)
精彩评论