Created
July 23, 2012 19:38
-
-
Save Janiczek/3165723 to your computer and use it in GitHub Desktop.
YouTube downloader
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
| #! /usr/bin/env python | |
| # dependencies: py-gdata | |
| # example usage: | |
| # ./yt.py "words that have to be in title" | |
| import sys | |
| import gdata | |
| import gdata.youtube | |
| import gdata.youtube.service | |
| if len(sys.argv) != 2: | |
| sys.exit('ERROR: surround argument with quotes') | |
| query = sys.argv[1] | |
| num = 50 # max | |
| path = query + ".txt" | |
| f = open(path,"w") | |
| yt_service = gdata.youtube.service.YouTubeService() | |
| all_videos = set() | |
| related = set() | |
| def PrintEntryDetails(entry): | |
| for word in query.lower().split(" "): | |
| if word not in entry.media.title.text.lower(): | |
| return | |
| video_id = entry.id.text[-11:] | |
| if video_id in all_videos: | |
| return | |
| else: | |
| all_videos.add(video_id) | |
| nazev = entry.media.title.text | |
| url = entry.media.player.url[:43] | |
| print video_id, "::", nazev | |
| f.write(url + "\n") | |
| f.flush() | |
| related_feed = yt_service.GetYouTubeRelatedVideoFeed(video_id=video_id) | |
| related.add(related_feed) | |
| def PrintVideoFeed(feed): | |
| for entry in feed.entry: | |
| PrintEntryDetails(entry) | |
| def SearchAndPrint(search_terms,num_of_results): | |
| query = gdata.youtube.service.YouTubeVideoQuery() | |
| query.vq = search_terms | |
| query.max_results = num_of_results | |
| feed = yt_service.YouTubeQuery(query) | |
| PrintVideoFeed(feed) | |
| SearchAndPrint(query,num) | |
| while len(related) > 0: | |
| try: | |
| for rel in related: | |
| PrintVideoFeed(rel) | |
| related.remove(rel) | |
| except RuntimeError: | |
| pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment