summaryrefslogtreecommitdiff
path: root/devfiles/scripts/videos-from-csv.py
blob: 69dcabb352bc277055308f4e4a54480acc262fed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/python

import sys,os,errno
import csv
import string
import urllib
from string import Template

BASE_URL="http://foursquaregrow.s3-website-us-east-1.amazonaws.com/"

def mkjson(chapter, number, title, length, pdf, videos):
    vtemplate = Template("""{
    "id": "$id",
    "number": "$number",
    "title": "$title",
    "length": $length,
    "pdf": "$pdf",
    "urls": [""")

    urltemplate = Template("""{"src":"$src", "type":"$type"},""")

    directory = string.lower("videos/" + chapter)
    try:
        os.makedirs(directory)
    except OSError as exc:
        if exc.errno == errno.EEXIST and os.path.isdir(directory):
            pass
        else:
            raise
    filename = string.lower(directory + "/" + chapter + "-" + number + ".json")
    with open(filename, 'w') as outfile:
        outfile.write(vtemplate.substitute(dict(id=string.lower(chapter+"-"+number),
            chapter=chapter, number=number, title=title, length=length, pdf=BASE_URL + urllib.quote(pdf))))

        for type,src in videos.iteritems():
            outfile.write(urltemplate.substitute(dict(type=type, src=BASE_URL + urllib.quote(src))))

        outfile.seek(-1, 2)
        outfile.write("]\n}")

# This script reads lines from the given csv file and creates json files for
# each video in the videos/ directory.

filename = sys.argv[1];
with open(filename, 'rb') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        chapter = row[0]
        number = row[1]
        title = row[2]
        length = row[3]
        pdf = row[4]
        h264 = row[5]

        videos = { "video/mp4": h264 }

        mkjson(chapter, number, title, length, pdf, videos)