<commit_before># Copyright 2013 <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import yaml
import json
import optparse
import gdata.youtube
import gdata.youtube.service

yt_service = gdata.youtube.service.YouTubeService()

# Turn on HTTPS/SSL access.
# Note: SSL is not available at this time for uploads.
yt_service.ssl = True

# Parse the yaml config file
with open('config.yaml', 'r') as config_file:  
    config = yaml.load(config_file.read())

# A complete client login request
yt_service.email = config['user_email']
yt_service.password = config['<PASSWORD>']
yt_service.source = config['source']
yt_service.developer_key = config['developer_key']
yt_service.client_id = config['client_id']

# Connect to the YouTube API
yt_service.ProgrammaticLogin()


# Retrieve a single caption track given its URL
def get_caption_track(url):
    return yt_service.Get("%s" % url, converter=converter)


# This seemingly useless function has to be passed into
# yt_service.Get() to "process" the caption track because of a WONTFIX
# bug <https://code.google.com/p/gdata-issues/issues/detail?id=4289>
# in Google's gdata library.
def converter(url):
    return url


# Get a GDataFeed of all the caption tracks associated with the video
def get_available_caption_tracks(id):
    caption_feed = yt_service.Get('https://gdata.youtube.com/feeds/api/videos/%s/captions' % id)

    return caption_feed


if __name__ == "__main__":
    # Set up the command line argument parser
    # TODO: check for required -i and -o parameters, exit if missing
    parser = optparse.OptionParser()

    parser.add_option('-i', '--input-file',
                      action="store", dest="input_file",
                      help="""Input json file""",
                      default="")

    parser.add_option('-o', '--output-dir',
                      action="store", dest="output_dir",
                      help="""Output directory""",
                      default="")
    
    options, args = parser.parse_args()

    # Open and parse the json video manifest
    with open(options.input_file, 'r') as f:  
        videos = json.load(f)

    # For each video, get the caption tracks and store them locally
    for video in videos:
        video_id = videos[video]['id']

        print "Retrieving caption tracks for video with ID %s..." % video_id
        feed = get_available_caption_tracks(video_id)
    
        inc = 0

        for entry in feed.entry:
            caption_track = get_caption_track(entry.content.src)
            
            # If there's more than one caption track, increment
            # filenames. TODO: get more meaningful information about
            # the differences between tracks and append more helpful
            # distinguishing info.
            
            if inc > 0:
                fn = "%s-%s.sbv" % (video_id, inc)
            else:
                fn = "%s.sbv" % video_id

            print "Saving caption track to %s%s" % (options.output_dir, fn)

            with open("%s%s" % (options.output_dir, fn), "wt") as caption_file:
                caption_file.write(caption_track)<commit_msg>Stitch output path together in more robust way.<commit_after># Copyright 2013 <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import yaml
import json
import optparse
import gdata.youtube
import gdata.youtube.service

yt_service = gdata.youtube.service.YouTubeService()

# Turn on HTTPS/SSL access.
# Note: SSL is not available at this time for uploads.
yt_service.ssl = True

# Parse the yaml config file
with open('config.yaml', 'r') as config_file:  
    config = yaml.load(config_file.read())

# A complete client login request
yt_service.email = config['user_email']
yt_service.password = config['<PASSWORD>']
yt_service.source = config['source']
yt_service.developer_key = config['developer_key']
yt_service.client_id = config['client_id']

# Connect to the YouTube API
yt_service.ProgrammaticLogin()


# Retrieve a single caption track given its URL
def get_caption_track(url):
    return yt_service.Get("%s" % url, converter=converter)


# This seemingly useless function has to be passed into
# yt_service.Get() to "process" the caption track because of a WONTFIX
# bug <https://code.google.com/p/gdata-issues/issues/detail?id=4289>
# in Google's gdata library.
def converter(url):
    return url


# Get a GDataFeed of all the caption tracks associated with the video
def get_available_caption_tracks(id):
    caption_feed = yt_service.Get('https://gdata.youtube.com/feeds/api/videos/%s/captions' % id)

    return caption_feed


if __name__ == "__main__":
    # Set up the command line argument parser
    # TODO: check for required -i and -o parameters, exit if missing
    parser = optparse.OptionParser()

    parser.add_option('-i', '--input-file',
                      action="store", dest="input_file",
                      help="""Input json file""",
                      default="")

    parser.add_option('-o', '--output-dir',
                      action="store", dest="output_dir",
                      help="""Output directory""",
                      default="")
    
    options, args = parser.parse_args()

    # Open and parse the json video manifest
    with open(options.input_file, 'r') as f:  
        videos = json.load(f)

    # For each video, get the caption tracks and store them locally
    for video in videos:
        video_id = videos[video]['id']

        print "Retrieving caption tracks for video with ID %s..." % video_id
        feed = get_available_caption_tracks(video_id)
    
        inc = 0

        for entry in feed.entry:
            caption_track = get_caption_track(entry.content.src)
            
            # If there's more than one caption track, increment
            # filenames. TODO: get more meaningful information about
            # the differences between tracks and append more helpful
            # distinguishing info.
            
            if inc > 0:
                fn = "%s-%s.sbv" % (video_id, inc)
            else:
                fn = "%s.sbv" % video_id

            output_path = os.path.normpath(options.output_dir) + os.sep + fn

            print "Saving caption track to %s" % (output_path)

            with open(output_path, "wt") as caption_file:
                caption_file.write(caption_track)
{'fraction_non_alphanumeric': 0.09772696338641622, 'fraction_numerical': 0.00381107935211651, 'mean_word_length': 2.7974160206718346, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3354750', 'n_tokens_mistral': 2138, 'n_tokens_neox': 2025, 'n_words': 709}