<commit_before># -*- coding: utf-8 -*-

# This file is part of PyBOSSA.
#
# PyBOSSA is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBOSSA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBOSSA.  If not, see <http://www.gnu.org/licenses/>.

import vimeo
import json
import config
import requests


def get_videos_tag(per_page=20, page=1, tags='science'):
    """
    Gets videos for a given tag
    :arg string hashtag: Vimeo hashtag to get the videos
    :returns: A list of Videos.
    :rtype: list
    """
    try:
        v = vimeo.Client(key=config.CONSUMER_KEY, secret=config.CONSUMER_SECRET)
        results = v.get('vimeo.videos.getByTag', tag=tags, per_page=per_page,
                        page=page)
        results = json.loads(results)
        videos = results['videos']['video']
        oembeds = []
        for v in videos:
            req = 'http://vimeo.com/%s' % v['id']
            url = 'http://vimeo.com/api/oembed.json?url=%s' % req
            payload = {'maxwidth': 512}
            res = requests.get(url, params=payload)
            if res.status_code == 200:
                oembeds.append(json.loads(res.text)['html'])
        # print oembeds
        return oembeds
    except:
        raise
        raise Exception("config.py file not found, please copy config.py.template \
                        to config.py and fill in the OAuth parameters")


def get_videos_channel(per_page=20, page=1, channel_id='technolust'):
    """
    Gets videos for a given channel
    :arg string channel id: Vimeo channel id to get the videos
    :returns: A list of Videos.
    :rtype: list
    """
    try:
        v = vimeo.Client(key=config.CONSUMER_KEY, secret=config.CONSUMER_SECRET)
        results = v.get('vimeo.channels.getVideos', channel_id=channel_id, per_page=per_page,
                        page=page)
        results = json.loads(results)
        videos = results['videos']['video']
        oembeds = []
        for v in videos:
            req = 'http://vimeo.com/%s' % v['id']
            url = 'http://vimeo.com/api/oembed.json?url=%s' % req
            payload = {'maxwidth': 512}
            res = requests.get(url, params=payload)
            if res.status_code == 200:
                oembeds.append(json.loads(res.text)['html'])
        # print oembeds
        return oembeds
    except:
        raise
        raise Exception("config.py file not found, please copy config.py.template \
                        to config.py and fill in the OAuth parameters")


def get_videos_group(per_page=20, page=1, group_id='science'):
    """
    Gets videos for a given group
    :arg string channel id: Vimeo group id to get the videos
    :returns: A list of Videos.
    :rtype: list
    """
    try:
        v = vimeo.Client(key=config.CONSUMER_KEY, secret=config.CONSUMER_SECRET)
        results = v.get('vimeo.groups.getVideos', group_id=group_id, per_page=per_page,
                        page=page)
        results = json.loads(results)
        videos = results['videos']['video']
        oembeds = []
        for v in videos:
            req = 'http://vimeo.com/%s' % v['id']
            url = 'http://vimeo.com/api/oembed.json?url=%s' % req
            payload = {'maxwidth': 512}
            res = requests.get(url, params=payload)
            if res.status_code == 200:
                oembeds.append(json.loads(res.text)['html'])
        # print oembeds
        return oembeds
    except:
        raise
        raise Exception("config.py file not found, please copy config.py.template \
                        to config.py and fill in the OAuth parameters")

def get_videos_category(per_page=20, page=1, category='nature'):
    """
    Gets videos for a given group
    :arg string channel id: Vimeo group id to get the videos
    :returns: A list of Videos.
    :rtype: list
    """
    try:
        v = vimeo.Client(key=config.CONSUMER_KEY, secret=config.CONSUMER_SECRET)
        results = v.get('vimeo.categories.getRelatedVideos', category=category, per_page=per_page,
                        page=page)
        results = json.loads(results)
        videos = results['videos']['video']
        oembeds = []
        for v in videos:
            req = 'http://vimeo.com/%s' % v['id']
            url = 'http://vimeo.com/api/oembed.json?url=%s' % req
            payload = {'maxwidth': 512}
            res = requests.get(url, params=payload)
            if res.status_code == 200:
                oembeds.append(json.loads(res.text)['html'])
        # print oembeds
        return oembeds
    except:
        raise
        raise Exception("config.py file not found, please copy config.py.template \
                        to config.py and fill in the OAuth parameters")


def get_videos_user_tag(per_page=20, page=1, user_id='craigprotzel', tag='itp'):
    """
    Gets videos for a given user and tag
    :arg string channel id: Vimeo user and tag id to get the videos
    :returns: A list of Videos.
    :rtype: list
    """
    try:
        v = vimeo.Client(key=config.CONSUMER_KEY, secret=config.CONSUMER_SECRET)
        results = v.get('vimeo.videos.search', user_id=user_id, query=tag, per_page=per_page,
                        page=page)
        results = json.loads(results)
        videos = results['videos']['video']
        oembeds = []
        for v in videos:
            req = 'http://vimeo.com/%s' % v['id']
            url = 'http://vimeo.com/api/oembed.json?url=%s' % req
            payload = {'maxwidth': 512}
            res = requests.get(url, params=payload)
            if res.status_code == 200:
                oembeds.append(json.loads(res.text)['html'])
        # print oembeds
        return oembeds
    except:
        raise
        raise Exception("config.py file not found, please copy config.py.template \
                        to config.py and fill in the OAuth parameters")

def get_videos_album(per_page=20, page=1, album_id='1659487'):
    """
    Gets videos for a given album
    :arg string channel id: Vimeo group id to get the videos
    :returns: A list of Videos.
    :rtype: list
    """
    try:
        v = vimeo.Client(key=config.CONSUMER_KEY, secret=config.CONSUMER_SECRET)
        results = v.get('vimeo.albums.getVideos', album_id=album_id, per_page=per_page,
                        page=page)
        results = json.loads(results)
        videos = results['videos']['video']
        oembeds = []
        for v in videos:
            req = 'http://vimeo.com/%s' % v['id']
            url = 'http://vimeo.com/api/oembed.json?url=%s' % req
            payload = {'maxwidth': 512}
            res = requests.get(url, params=payload)
            if res.status_code == 200:
                oembeds.append(json.loads(res.text)['html'])
        # print oembeds
        return oembeds
    except:
        raise
        raise Exception("config.py file not found, please copy config.py.template \
                        to config.py and fill in the OAuth parameters")<commit_msg>New command line client for interacting with Vimeo and create task files for PyBossa.<commit_after># -*- coding: utf-8 -*-

# This file is part of PyBOSSA.
#
# PyBOSSA is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBOSSA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBOSSA.  If not, see <http://www.gnu.org/licenses/>.

import vimeo
import json
import config
import requests
import click

@click.command()
@click.option('--per-page', default=20, help='Number of videos per page')
@click.option('--page', default=1, help='Page to get videos from')
@click.option('--by-tags', help='Get videos with these tags', default=None)
@click.option('--by-channel', help='Get videos from a channel', default=None)
@click.option('--by-group', help='Get videos from a group', default=None)
@click.option('--by-category', help='Get videos from a category', default=None)
@click.option('--by-user', help='Get videos from a user ID', default=None)
@click.option('--tagged', help='To use in conjunction with --by-user', default=None)
@click.option('--by-album', help='Get videos from an album', default=None)
def cli(per_page, page, by_tags, by_channel, by_group, by_category, by_user,
        tagged, by_album):
    try:
        action = None
        filename = None
        query = dict(page=page, per_page=per_page)
        v = vimeo.Client(key=config.CONSUMER_KEY, secret=config.CONSUMER_SECRET)
        if by_tags:
            click.secho("Getting videos from tags: %s" % by_tags,
                        fg="yellow")
            action = 'vimeo.videos.getByTag'
            query['tag'] = by_tags
            filename = 'video_tasks_from_tags.json'
        elif by_channel:
            click.secho("Getting videos from channel: %s" % by_channel,
                        fg="yellow")
            action = 'vimeo.channels.getVideos'
            query['channel_id'] = by_channel
            filename = 'video_tasks_from_channel.json'
        elif by_group:
            click.secho("Getting videos from group: %s" % by_group,
                        fg="yellow")
            action = 'vimeo.groups.getVideos'
            query['group_id'] = by_group
            filename = 'video_tasks_from_group.json'
        elif by_category:
            click.secho("Getting videos from category: %s" % by_category,
                        fg="yellow")
            action = 'vimeo.categories.getRelatedVideos'
            query['category'] = by_category
            filename = 'video_tasks_from_category.json'
        elif by_user and tagged:
            click.secho("Getting videos tagged: %s from user: %s" %
                        (tagged, by_user))
            action = 'vimeo.videos.search'
            query['user_id'] = by_user
            query['query'] = tagged
            filename = 'video_tasks_from_user_tagged.json'
        elif by_album:
            click.secho("Getting videos from album: %s " % by_album)
            action = 'vimeo.albums.getVideos'
            query['album_id'] = by_album
            filename = 'video_tasks_from_album.json'
        else:
            raise click.UsageError("Please use --help to see the available options")
        results = v.get(action, **query)
        write_results_to_json_file(results, filename)
        click.secho("Done!", fg="green")
    except:
        raise
        raise Exception("config.py file not found, please copy config.py.template \
                        to config.py and fill in the OAuth parameters")

def write_results_to_json_file(results, filename):
    results = json.loads(results)
    videos = results['videos']['video']
    oembeds = []
    with click.progressbar(videos, label="Processing videos") as bar:
        for v in bar:
            req = 'http://vimeo.com/%s' % v['id']
            url = 'http://vimeo.com/api/oembed.json?url=%s' % req
            payload = {'maxwidth': 512}
            res = requests.get(url, params=payload)
            if res.status_code == 200:
                tmp = dict()
                tmp['oembed'] = json.loads(res.text)['html']
                oembeds.append(tmp)
    file = open(filename, 'w')
    file.write(json.dumps(oembeds))
    file.close()


if __name__ == '__main__':
    cli()
{'fraction_non_alphanumeric': 0.113948107750785, 'fraction_numerical': 0.006114691786481574, 'mean_word_length': 2.284396200814111, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '6553821', 'n_tokens_mistral': 3770, 'n_tokens_neox': 3624, 'n_words': 1018}