<commit_before># Universal Subtitles, universalsubtitles.org
# 
# Copyright (C) 2010 Participatory Culture Foundation
# 
# This program 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.
# 
# This program 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 this program.  If not, see 
# http://www.gnu.org/licenses/agpl-3.0.html.

from vidscraper.sites import vimeo
from vidscraper.errors import Error as VidscraperError
from base import VideoType, VideoTypeError
from django.conf import settings
from django.utils.html import strip_tags

vimeo.VIMEO_API_KEY = getattr(settings, 'VIMEO_API_KEY')
vimeo.VIMEO_API_SECRET = getattr(settings, 'VIMEO_API_SECRET')

class VimeoVideoType(VideoType):

    abbreviation = 'V'
    name = 'Vimeo.com'   
    site = 'vimeo.com'
    
    def __init__(self, url):
        self.url = url
        self.videoid = self._get_vimeo_id(url)
        try:
            self.shortmem = vimeo.get_shortmem(url)
        except VidscraperError, e:
            raise VideoTypeError(e[0])   
        
    @property
    def video_id(self):
        return self.videoid
    
    def convert_to_video_url(self):
        return 'http://vimeo.com/%s' % self.videoid

    @classmethod    
    def video_url(cls, obj):
        """
        This method can be called with wither a VideoType object or
        an actual VideoURL object, therefore the if statement
        """
        if obj.videoid:
            return 'http://vimeo.com/%s' % obj.videoid
        else:
            return obj.url
    
    @classmethod
    def matches_video_url(cls, url):
        return bool(vimeo.VIMEO_REGEX.match(url))

    def create_kwars(self):
        return { 'videoid': self.videoid }
    
    def set_values(self, video_obj):
        if vimeo.VIMEO_API_KEY and vimeo.VIMEO_API_SECRET:
            video_obj.thumbnail = vimeo.get_thumbnail_url(self.url, self.shortmem) or ''
            video_obj.small_thumbnail = vimeo.get_small_thumbnail_url(self.url, self.shortmem) or ''
            video_obj.title = vimeo.scrape_title(self.url, self.shortmem)
            video_obj.description = strip_tags(vimeo.scrape_description(self.url, self.shortmem))
            video_obj.save()
        return video_obj
    
    def _get_vimeo_id(self, video_url):
        return vimeo.VIMEO_REGEX.match(video_url).groupdict().get('video_id') <commit_msg>Site now allows private Vimeo videos. For https://unisubs.sifterapp.com/projects/12298/issues/442725/comments<commit_after># Universal Subtitles, universalsubtitles.org
# 
# Copyright (C) 2010 Participatory Culture Foundation
# 
# This program 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.
# 
# This program 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 this program.  If not, see 
# http://www.gnu.org/licenses/agpl-3.0.html.

from vidscraper.sites import vimeo
from vidscraper.errors import Error as VidscraperError
from base import VideoType, VideoTypeError
from django.conf import settings
from django.utils.html import strip_tags

vimeo.VIMEO_API_KEY = getattr(settings, 'VIMEO_API_KEY')
vimeo.VIMEO_API_SECRET = getattr(settings, 'VIMEO_API_SECRET')

class VimeoVideoType(VideoType):

    abbreviation = 'V'
    name = 'Vimeo.com'   
    site = 'vimeo.com'
    
    def __init__(self, url):
        self.url = url
        self.videoid = self._get_vimeo_id(url)
        try:
            self.shortmem = vimeo.get_shortmem(url)
        except VidscraperError, e:
            # we're not raising an error here because it 
            # disallows us from adding private Vimeo videos.
            pass
        
    @property
    def video_id(self):
        return self.videoid
    
    def convert_to_video_url(self):
        return 'http://vimeo.com/%s' % self.videoid

    @classmethod    
    def video_url(cls, obj):
        """
        This method can be called with wither a VideoType object or
        an actual VideoURL object, therefore the if statement
        """
        if obj.videoid:
            return 'http://vimeo.com/%s' % obj.videoid
        else:
            return obj.url
    
    @classmethod
    def matches_video_url(cls, url):
        return bool(vimeo.VIMEO_REGEX.match(url))

    def create_kwars(self):
        return { 'videoid': self.videoid }
    
    def set_values(self, video_obj):
        if vimeo.VIMEO_API_KEY and vimeo.VIMEO_API_SECRET:
            try:
                video_obj.thumbnail = vimeo.get_thumbnail_url(self.url, self.shortmem) or ''
                video_obj.small_thumbnail = vimeo.get_small_thumbnail_url(self.url, self.shortmem) or ''
                video_obj.title = vimeo.scrape_title(self.url, self.shortmem)
                video_obj.description = strip_tags(vimeo.scrape_description(self.url, self.shortmem))
                video_obj.save()
            except Exception:
                # in case the Vimeo video is private.
                pass
        return video_obj
    
    def _get_vimeo_id(self, video_url):
        return vimeo.VIMEO_REGEX.match(video_url).groupdict().get('video_id') 
{'fraction_non_alphanumeric': 0.08108563722184761, 'fraction_numerical': 0.004383007417397168, 'mean_word_length': 2.803205128205128, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '742544', 'n_tokens_mistral': 1905, 'n_tokens_neox': 1862, 'n_words': 522}