Last active
December 31, 2015 12:49
-
-
Save fnielsen/7988477 to your computer and use it in GitHub Desktop.
YouTube comment reading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gdata.youtube.service | |
from time import sleep | |
class YtsClassic(object): | |
""" | |
http://finnaarupnielsen.wordpress.com/2009/09/21/getting-comments-from-youtube-via-pythons-gda/ | |
""" | |
def __init__(self): | |
self.yts = gdata.youtube.service.YouTubeService() | |
def get_video_comments(self, video_id='jofNR_WkoCE'): | |
""" | |
Has there been changes in the YouTube API? | |
http://apiblog.youtube.com/2010/02/best-practices-for-avoiding-quota.html | |
""" | |
urlpattern = 'http://gdata.youtube.com/feeds/api/videos/' + \ | |
video_id + \ | |
'/comments?start-index=%d&max-results=25' | |
index = 1 | |
url = urlpattern % index | |
comments = [] | |
while url: | |
try: | |
ytfeed = self.yts.GetYouTubeVideoCommentFeed(uri=url) | |
comments.extend([ comment.content.text for comment in ytfeed.entry ]) | |
url = ytfeed.GetNextLink().href | |
sleep(3) | |
except Exception: | |
break | |
return comments | |
def main(): | |
yts_classic = YtsClassic() | |
comments = yts_classic.get_video_comments() | |
print(len(comments)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment