4
YouTube View Count Statistics
6
Queries YouTube and determines the total number of views of Helioviewer.org
10
mysql -uUSERNAME -p DBNAME -e "SELECT youtubeId FROM youtube WHERE youtubeId IS NOT NULL" | sed 1d > youtube_ids
17
import gdata.youtube.service
25
yt_service = gdata.youtube.service.YouTubeService()
29
ids = open(args.file).read().splitlines()
32
retries = {i:0 for i in ids} # Limit number of retries due to network failures
36
entry = yt_service.GetYouTubeVideoEntry(video_id=video_id)
37
counts.append(int(entry.statistics.view_count))
38
except gdata.service.RequestError:
39
retries[video_id] += 1
41
if retries[video_id] > 1:
42
print("Retrying %s (try #%d)" % (video_id, retries[video_id]))
44
#For request errors, append back to end of the queue
45
if retries[video_id] <= 20:
48
except AttributeError:
49
# If entry.statistics is None, stats are unavailable (hidden by user?)
50
print("Skipping %s (Statistics Unavailable)" % video_id)
52
counts = np.array(counts)
57
print("Total views: %d" % counts.sum())
58
print("Min: %d" % counts.min())
59
print("Max: %d" % counts.max())
62
"""Get command-line arguments"""
63
parser = argparse.ArgumentParser(description='Collects statistics on videos uploaded to YouTube from Helioviewer.org.')
64
parser.add_argument('file', metavar='FILE',
65
help='File containing a newline-delimited list of YouTube movie ids.')
66
args = parser.parse_args()
68
if not os.path.isfile(args.file):
69
sys.exit("Invalid file specified: %s" % args.file)
73
if __name__ == '__main__':