def get_folder_contents(self): v = VimeoClient(token=self.vimeo_access_token, key=self.vimeo_client_id, secret=self.vimeo_client_secret) response = v.get('/users/' + self.vimeo_user_id + '/projects/' + self.vimeo_project_id + '/videos') assert response.status_code == 200 return CPSSVimeoCollectionType(response.json())
def __init__(self, conf, blog): self.log = getLogger(__name__) self.conf = conf self.blog = blog self.client = VimeoClient( token=self.conf.vimeo_token, key=self.conf.vimeo_client_id, secret=self.conf.vimeo_client_secret )
def test_auth_modes(self, dev): vimeo = VimeoClient(access_token=test_data['access_token'], dev=bool(dev)) r = vimeo.me.videos() assert 'data' in r['body'], "Access token auth should work" vimeo = VimeoClient(client_id=test_data['client_id'], client_secret=test_data['client_secret'], dev=bool(dev)) r = vimeo.users.emmett9001() assert 'uri' in r['body'], "Basic auth with cid and secret should work" vimeo = VimeoClient(client_id=test_data['client_id'], dev=bool(dev)) r = vimeo.users.emmett9001() assert 'uri' in r['body'], "Basic auth with cid should work"
def get(self): """ Demonstrates asynchronous use of VimeoClient via tornado.gen The caller must be decorated by tornado.gen.coroutine The call to an API function must include the async kwarg set to True The result must be yielded before assignment """ vimeo = VimeoClient("YOUR ACCESS TOKEN") res = yield vimeo.users(query='cats', async=True) self.write(res) self.finish()
def getVimeo(): config = ConfigParser.RawConfigParser() config.read('config.ini') token = config.get('Vimeo', 'token') secret = config.get('Vimeo', 'secret') Client_Identifier = config.get('Vimeo', 'Client_Identifier') return VimeoClient(token=token, key=Client_Identifier, secret=secret)
def upload_video_to_vimeo(video_id, **kwargs): """docstring for upload_to_vimeo""" """ General 4 steps are given here: http://vimeo.com/api/docs/oauth using the python vimeo library I originally did: from vimeo import VimeoClient import settings vio = VimeoClient(format='json',key= settings.VIMEO_KEY,secret=settings.VIMEO_SECRET), url = vio.get_authorization_url(permission='write') ## this returns a URL to be accessed ## get the verifier value after the url above has redirected to the application callback vio.set_verifier('the-access-verifier-string-from-above') access_token = vio.get_access_token() ## get the oauth token + oauth secret str(access_token) OR access_token.__str__() ## these should be stored; they are valid unless the vimeo account holder deletes them ... ## which is never for me ## for any further calls use initialize the VimeoClient as follows vio = VimeoClient(format='json',key=settings.VIMEO_KEY,secret=settings.VIMEO_SECRET, token=settings.VIMEO_OAUTH_TOKEN, token_secret= settings.VIMEO_OAUTH_TOKEN_SECRET) ## then continue with the upload """ from gelder.models import Video logger = upload_video_to_vimeo.get_logger(**kwargs) videourl = Video.objects.get(id=video_id) vio = VimeoClient(format='json', key=settings.VIMEO_KEY, secret=settings.VIMEO_SECRET, token=settings.VIMEO_OAUTH_TOKEN, token_secret=settings.VIMEO_OAUTH_TOKEN_SECRET) # get quota here or instantiate another vio client and check the quota before uploading uploader = vio.get_uploader() uploader.upload(smart_str(videourl.video.path)) ucresponse = uploader.complete() #if ucresponse is not None and ucresponse['video_id']: videourl.extern = 'vimeo' videourl.externurl = ucresponse['video_id'] videourl.on_extern = True videourl.save() logger.info('(success) Boto s3 uploaded video with id: %s to vimeo', (video_id)) #raise Exception([uresponse, str(uresponse), ucresponse]) return True
def main(argv): parser = optparse.OptionParser( usage='Usage: %prog [options]', description="Simple Vimeo uploader") parser.add_option('-k', '--key', help="Consumer key") parser.add_option('-s', '--secret', help="Consumer secret") parser.add_option('-t', '--access-token', help="Access token") parser.add_option('-y', '--access-token-secret', help="Access token secret") (options, args) = parser.parse_args(argv[1:]) if None in (options.key, options.secret): print "Missing key or secret" sys.exit(-1) if None in (options.access_token, options.access_token_secret): client = VimeoClient(options.key, options.secret) client.get_request_token() print client.get_authorize_token_url() verifier = sys.stdin.readline().strip() print "Using ", verifier, " as verifier" print "Token is:", client.get_access_token(verifier) else: client = VimeoClient(options.key, options.secret, token=options.access_token, token_secret=options.access_token_secret)
def get(self): """ Demonstrates synchronous use of VimeoClient Caller does not need to be wrapped in a decorator, result is returned synchronously """ vimeo = VimeoClient("YOUR ACCESS TOKEN") res = vimeo.users.emmett9001() self.write(res) self.finish()
def initialize(self, form): """ Prepare the upload form for this service """ # test the authentication if self.token: try: self.v = VimeoClient(token=self.token, token_secret=self.token_secret, verifier=self.verifier) self.v.cache_timeout = 0 self.uploader = self.v.get_uploader() except: # failed to authenticate, erase tokens self.token = None self.token_secret = None self.verifier = None self.settings.app_state["vimeo_token"] = "" self.settings.app_state["vimeo_token_secret"] = "" self.settings.app_state["vimeo_verifier"] = "" # Show error message messagebox.show(_("Validation Error!"), _("Vimeo authentication has expired.")) self.form = form form.lblUsername.set_property("visible", False) form.txtUsername.set_property("visible", False) form.lblPassword.set_property("visible", False) form.txtPassword.set_property("visible", False) if self.token: # already authorized form.login_divider.set_property("visible", False) form.btnAuthorize.set_property("visible", False) form.lblVerification.set_property("visible", False) form.txtVerification.set_property("visible", False) else: # user needs to authorize OpenShot form.login_divider.set_property("visible", True) form.btnAuthorize.set_property("visible", True) form.lblVerification.set_property("visible", True) form.txtVerification.set_property("visible", True) form.lnkForgot.set_label("http://www.vimeo.com") form.lnkForgot.set_uri("http://www.vimeo.com")
def search_by_keyword(self, query, page=1): client = VimeoClient(settings.VIMEO_CONSUMER_KEY, settings.VIMEO_CONSUMER_SECRET) videos = client.vimeo_videos_search(query=query) data = {'results': []} data['index'] = { 'total_results': len(videos), 'items_per_page': self.NB_RESULTS_PER_PAGE } for video in videos.getchildren(): current_result = { 'id': video.get('id'), 'name': video.get('title'), 'permalink_url': 'http://vimeo.com/%s' % video.get('id'), 'description': '', 'embed_url': 'http://player.vimeo.com/video/%s' % video.get('id'), 'embed_type': '', 'duration': '', 'user_name': '', 'user_url': '' } response = client.vimeo_videos_getThumbnailUrls(video_id=video.get('id')) thumbnails = response.getchildren() information = client.vimeo_videos_getInfo(video_id=video.get('id')) if len(information): current_result.update({ 'description': information.find('description').text, 'duration': information.find('duration').text, 'user_name': information.find('owner').get('display_name'), 'user_url': information.find('owner').get('profileurl') }) if len(thumbnails) > 1: current_result['image_url'] = thumbnails[1].text data['results'].append(current_result) return data
def get(self): """ Demonstrates asynchronous use of VimeoClient via callback function Caller must be wrapped in @tornado.web.asynchronous Call to API function must include the callback kwarg """ vimeo = VimeoClient("YOUR ACCESS TOKEN") def callback(result): self.write(result) self.finish() vimeo.users.emmett9001(callback=callback)
def initialize(self, form): """ Prepare the upload form for this service """ # test the authentication if self.token: try: self.v = VimeoClient( token=self.token, token_secret=self.token_secret, verifier=self.verifier) self.v.cache_timeout = 0 self.uploader = self.v.get_uploader() except: # failed to authenticate, erase tokens self.token = None self.token_secret = None self.verifier = None self.settings.app_state["vimeo_token"] = "" self.settings.app_state["vimeo_token_secret"] = "" self.settings.app_state["vimeo_verifier"] = "" # Show error message messagebox.show( _("Validation Error!"), _("Vimeo authentication has expired.")) self.form = form form.lblUsername.set_property("visible", False) form.txtUsername.set_property("visible", False) form.lblPassword.set_property("visible", False) form.txtPassword.set_property("visible", False) if self.token: # already authorized form.login_divider.set_property("visible", False) form.btnAuthorize.set_property("visible", False) form.lblVerification.set_property("visible", False) form.txtVerification.set_property("visible", False) else: # user needs to authorize OpenShot form.login_divider.set_property("visible", True) form.btnAuthorize.set_property("visible", True) form.lblVerification.set_property("visible", True) form.txtVerification.set_property("visible", True) form.lnkForgot.set_label("http://www.vimeo.com") form.lnkForgot.set_uri("http://www.vimeo.com")
class VimeoService(): def __init__(self, project, settings): self.project = project self.filename = None self.settings = settings self.token = None self.token_secret = None self.verifier = None self.form = None # Add language support _ = Language_Init.Translator(project).lang.gettext self._ = _ # get tokens (if already authorized) if self.settings.app_state["vimeo_token"]: self.token = self.settings.app_state["vimeo_token"] if self.settings.app_state["vimeo_token_secret"]: self.token_secret = self.settings.app_state["vimeo_token_secret"] if self.settings.app_state["vimeo_verifier"]: self.verifier = self.settings.app_state["vimeo_verifier"] def initialize(self, form): """ Prepare the upload form for this service """ # test the authentication if self.token: try: self.v = VimeoClient( token=self.token, token_secret=self.token_secret, verifier=self.verifier) self.v.cache_timeout = 0 self.uploader = self.v.get_uploader() except: # failed to authenticate, erase tokens self.token = None self.token_secret = None self.verifier = None self.settings.app_state["vimeo_token"] = "" self.settings.app_state["vimeo_token_secret"] = "" self.settings.app_state["vimeo_verifier"] = "" # Show error message messagebox.show( _("Validation Error!"), _("Vimeo authentication has expired.")) self.form = form form.lblUsername.set_property("visible", False) form.txtUsername.set_property("visible", False) form.lblPassword.set_property("visible", False) form.txtPassword.set_property("visible", False) if self.token: # already authorized form.login_divider.set_property("visible", False) form.btnAuthorize.set_property("visible", False) form.lblVerification.set_property("visible", False) form.txtVerification.set_property("visible", False) else: # user needs to authorize OpenShot form.login_divider.set_property("visible", True) form.btnAuthorize.set_property("visible", True) form.lblVerification.set_property("visible", True) form.txtVerification.set_property("visible", True) form.lnkForgot.set_label("http://www.vimeo.com") form.lnkForgot.set_uri("http://www.vimeo.com") def get_logo(self): logo_path = os.path.join(self.project.BASE_DIR, "openshot", "uploads", "logos", "vimeo.png") return gtk.gdk.pixbuf_new_from_file(logo_path) def get_export_presets(self): """ Get a tuple of related export presets for this service (if any) """ # get reference to gettext _ = self._ return (_("Web"), _("Vimeo-HD")) def get_authorization_url(self): self.v = VimeoClient() self.v.cache_timeout = 0 return self.v.get_authorization_url(permission="write") def validate(self, form): """ Validate the upload form... check for missing values. """ # get reference to gettext _ = self._ # get code verification_code = form.txtVerification.get_text() title = form.txtTitle.get_text() start, end = form.txtDescription.get_buffer().get_bounds() description = form.txtDescription.get_buffer().get_text(start, end) # Validate the the form is valid if not os.path.isfile(str(self.filename)): # Show error message messagebox.show( _("Validation Error!"), _("Please choose a valid video file.")) return False if not title: # Show error message messagebox.show( _("Validation Error!"), _("Please enter a valid title.")) return False if not self.token: if not description: # Show error message messagebox.show( _("Validation Error!"), _("Please enter a valid description.")) return False if not verification_code: # Show error message messagebox.show( _("Validation Error!"), _("Please enter a valid verification code. Click the 'Authorize' button and login to the website. Confirm the authorization, and copy the verification code." )) return False # form is valid return True def set_file(self, filename): self.filename = filename def start_upload(self, form): # get reference to gettext _ = self._ if not self.token: # Not Authorized Yet. # get code verification_code = form.txtVerification.get_text() try: # pass code and authorize OpenShot (hopefully) self.v.set_verifier(verification_code) access_token = self.v.get_access_token() # save tokens in settings self.verifier = verification_code self.settings.app_state["vimeo_verifier"] = self.verifier self.token = access_token.key self.settings.app_state["vimeo_token"] = self.token self.token_secret = access_token.secret self.settings.app_state[ "vimeo_token_secret"] = self.token_secret # Get uploader object self.uploader = self.v.get_uploader() except: # Show error message messagebox.show( _("Validation Error!"), _("There was an error authorizing OpenShot. Please be sure to enter the correct verification code from vimeo.com." )) # get settings title = form.txtTitle.get_text() start, end = form.txtDescription.get_buffer().get_bounds() description = form.txtDescription.get_buffer().get_text(start, end) try: # enable upload button form.btnUpload.set_sensitive(False) # Upload the file to Vimeo output = self.uploader.upload( self.filename, chunk=True, chunk_size=64 * 1024, chunk_complete_hook=self.on_chunk_complete) upload_ticket_id = output.get("id") output = self.v.videos_upload_complete(ticket_id=upload_ticket_id) video_id = output.get("video_id") # Set the name and description of the video self.v.videos_setTitle(title=title, video_id=video_id) self.v.videos_setDescription( description=description, video_id=video_id) except: # Show error message messagebox.show( _("Validation Error!"), _("There was an error uploading to Vimeo.")) # enable upload button form.btnUpload.set_sensitive(True) return False # enable upload button form.btnUpload.set_sensitive(True) # successful return True def on_chunk_complete(self, *args): #print "on_chunk_complete" total_size = args[0]["total_size"] chunk_size = args[0]["chunk_size"] chunk_id = args[0]["chunk_id"] + 1 # zero based # calculate current bytes transferred current_bytes = chunk_id * chunk_size if current_bytes >= total_size: # don't exceed the total bytes current_bytes = total_size # calculate percentage percent = float(current_bytes) / float(total_size) gobject.idle_add(self.form.update_progressbar, percent) # allow other gtk operations to happen while gtk.events_pending(): gtk.main_iteration()
def start_crawling(id_file, log_file, video_file, channel_file, initial_number): v = VimeoClient(token=YOUR_ACCESS_TOKEN, key=YOUR_CLIENT_ID, secret=YOUR_CLIENT_SECRET) open(log_file, 'w').close() dot_idx = video_file.rfind('.') video_file_name = video_file[:dot_idx] video_file_format = video_file[dot_idx:] nameid_list = read_data_from_csv(id_file, log_file) if nameid_list is None: return vimeo_channels = [] vimeo_videos = [] requests_counter = 0 for id in nameid_list: video_file_number = initial_number video_file = '{}_{}_{}{}'.format(video_file_name, id, video_file_number, video_file_format) channel_response = get_channel_data(v, id, log_file) if channel_response is not None: channel_response['name_id'] = id vimeo_channels.append(channel_response) try: with open(channel_file, 'w') as f: j_str = dumps(vimeo_channels, indent=4, sort_keys=True) f.write(j_str) except Exception as e: log = '[{}] Save or open to file {} FAILED: {}.\n'.format( strftime('%Y-%m-%d %H:%M:%S'), channel_file, str(e)) with open(log_file, 'a') as f: f.write(log) return fields = { 'uri', 'name', 'description', 'link', 'duration', 'width', 'height', 'language', 'created_time', 'modified_time', 'privacy', 'pictures', 'tags', 'stats', 'metadata', 'user.uri', } cmd = '/channels/{}/videos?fields={}&per_page=50&page=1&sort=date&direction=asc'.format( id, ','.join(fields)) while True: video_response = get_video_data(v, cmd, log_file) if video_response is None: break if 'data' not in video_response: break for ele in video_response['data']: ele['date_time'] = strftime('%Y-%m-%d %H:%M:%S') ele['name_id'] = id vimeo_videos.append(ele) eq_idx = cmd.rfind('=') page = cmd[eq_idx + 1:] log = '[{}] From {} Responce size {} Page {} Videos in list {}.\n'.format( strftime('%Y-%m-%d %H:%M:%S'), id, len(video_response['data']), page, len(vimeo_videos)) with open(log_file, 'a') as f: f.write(log) requests_counter = requests_counter + 1 if requests_counter == MAX_REQUESTS_PER_SAVE: requests_counter = 0 log = '[{}] Saving to file...\n'.format( strftime('%Y-%m-%d %H:%M:%S')) with open(log_file, 'a') as f: f.write(log) try: with open(video_file, 'w') as f: j_str = dumps(vimeo_videos, indent=4, sort_keys=True) f.write(j_str) except Exception as e: log = '[{}] Save or open to file {} FAILED: {}.\n'.format( strftime('%Y-%m-%d %H:%M:%S'), video_file, str(e)) with open(log_file, 'a') as f: f.write(log) return log = '[{}] Successfully saved.\n'.format( strftime('%Y-%m-%d %H:%M:%S')) with open(log_file, 'a') as f: f.write(log) if len(vimeo_videos) >= MAX_VIDEOS_PER_FILE: log = '[{}] Creating new file.\n'.format( strftime('%Y-%m-%d %H:%M:%S')) with open(log_file, 'a') as f: f.write(log) video_file_number = video_file_number + 1 video_file = '{}_{}_{}{}'.format(video_file_name, id, video_file_number, video_file_format) vimeo_videos = [] cmd = video_response['paging']['next'] if not cmd: break log = '[{}] Saving to file...\n'.format(strftime('%Y-%m-%d %H:%M:%S')) with open(log_file, 'a') as f: f.write(log) try: with open(video_file, 'w') as f: j_str = dumps(vimeo_videos, indent=4, sort_keys=True) f.write(j_str) except Exception as e: log = '[{}] Save or open to file {} FAILED: {}.\n'.format( strftime('%Y-%m-%d %H:%M:%S'), video_file, str(e)) with open(log_file, 'a') as f: f.write(log) return log = '[{}] Successfully saved.\n'.format( strftime('%Y-%m-%d %H:%M:%S')) with open(log_file, 'a') as f: f.write(log) vimeo_videos = []
def setup(dev): global vimeo global test_data test_data = dev_data if dev else prod_data vimeo = VimeoClient(test_data['access_token'], dev=bool(dev))
def client(self): return VimeoClient(token=self.access_token, key=self.client_id, secret=self.client_secret)
def __init__(self, vimeo_data, channel_file, video_file, log_file): """Creates crawler for dailymotion. Args: vimeo_data (dictionary): Dictionary with vimeo client informations: accessToken, clientId, clientSecret. channel_file (string): Path to a file where metadata from channels will be saved. video_file (string): Path to a file where metadata from videos will be saved. log_file (string): Path to a file where logs will be saved. """ self.v = VimeoClient(token=vimeo_data['accessToken'], key=vimeo_data['clientId'], secret=vimeo_data['clientSecret']) """VimeoClient Object used for sending requests and getting responses using Vimeo API. """ self.channel_file = channel_file """string Path to a file where metadata from channels will be saved. """ dot_idx = video_file.rfind('.') self.video_file_name = video_file[:dot_idx] """string Path to a file without extension where metadata from videos will be saved. """ self.video_file_extension = video_file[dot_idx:] """string Extension for the path to a file where metadata from videos will be saved. """ logging.basicConfig(level=logging.INFO, propagate=False, filename=log_file, format='%(asctime)-15s %(message)s') logging.getLogger("requests").setLevel(logging.WARNING) self.logger = logging.getLogger(__name__) """Logger Object used for logging. """ self.channels_array = [] """array Array that stores names of the channels which will be analyzed. """ self.vimeo_channels = [] """array Array that stores metadata from channels. """ self.total_videos = 0 """int The total number of metadata from videos successfully obtained during crawling. """ self.max_requests_per_save = 10 """int The number of requests after which metadata from videos will be saved to a file. """ self.condition_array = [ '2016-08', '2016-07', '2016-06', '2016-05', '2016-04', '2016-03', '2016-02', '2016-01', '2015-12', '2015-11', '2015-10', '2015-09' ] """array Array that stores permitted dates. Only them fulfill the conditions for filtering. """ self.min_views = 0 """int
class VimeoCrawler(object): def __init__(self, vimeo_data, channel_file, video_file, log_file): """Creates crawler for dailymotion. Args: vimeo_data (dictionary): Dictionary with vimeo client informations: accessToken, clientId, clientSecret. channel_file (string): Path to a file where metadata from channels will be saved. video_file (string): Path to a file where metadata from videos will be saved. log_file (string): Path to a file where logs will be saved. """ self.v = VimeoClient(token=vimeo_data['accessToken'], key=vimeo_data['clientId'], secret=vimeo_data['clientSecret']) """VimeoClient Object used for sending requests and getting responses using Vimeo API. """ self.channel_file = channel_file """string Path to a file where metadata from channels will be saved. """ dot_idx = video_file.rfind('.') self.video_file_name = video_file[:dot_idx] """string Path to a file without extension where metadata from videos will be saved. """ self.video_file_extension = video_file[dot_idx:] """string Extension for the path to a file where metadata from videos will be saved. """ logging.basicConfig(level=logging.INFO, propagate=False, filename=log_file, format='%(asctime)-15s %(message)s') logging.getLogger("requests").setLevel(logging.WARNING) self.logger = logging.getLogger(__name__) """Logger Object used for logging. """ self.channels_array = [] """array Array that stores names of the channels which will be analyzed. """ self.vimeo_channels = [] """array Array that stores metadata from channels. """ self.total_videos = 0 """int The total number of metadata from videos successfully obtained during crawling. """ self.max_requests_per_save = 10 """int The number of requests after which metadata from videos will be saved to a file. """ self.condition_array = [ '2016-08', '2016-07', '2016-06', '2016-05', '2016-04', '2016-03', '2016-02', '2016-01', '2015-12', '2015-11', '2015-10', '2015-09' ] """array Array that stores permitted dates. Only them fulfill the conditions for filtering. """ self.min_views = 0 """int The minimum number of views that fulfill the condition for filtering. """ def add_content_providers(self, csv_file): """Adds names of the channels to be analyzed. Args: csv_file (string): Path to a csv file with names of the channels. """ try: with open(csv_file, 'r') as f: data = reader(f) for row in data: self.channels_array.append(row[1]) self.channels_array.pop(0) except Exception as e: raise Exception('Can not read data from file: {}'.format(str(e))) def perform_filtering(self, video_data): """Performs filtering. Checks whether the video meets the conditions. Args: video_data (dictionary): The dictionary with video's metadata. Returns: boolean: True if video meets the conditions, False otherwise. """ date = video_data['created_time'][:7] if date not in self.condition_array: return False views = video_data['stats']['plays'] if views is None or views < self.min_views: return False return True def analyze_channel(self, channel): """Gets metadata from the channel. Metadata obtained from the channel: 'metadata', 'user.metadata'. Args: channel (string): The id of the channel. Returns: dictionary: The dictionary with basic channel informations (keys: 'channel_id', 'channel_likes') """ try: response = self.v.get('/channels/{}'.format(channel)).json() response = { 'channel_id': channel, 'channel_meta': response['metadata'], 'user_meta': response['user']['metadata'] } except Exception as e: raise Exception('Request for channel {} failed: {}'.format( channel, str(e))) self.vimeo_channels.append(response) try: with open(self.channel_file, 'w') as f: f.write(dumps(self.vimeo_channels, indent=4)) except Exception as e: raise Exception('Can not save vimeo channels to file: {}'.format( str(e))) channel_info = { 'channel_id': channel, 'channel_likes': response['channel_meta']['connections']['users']['total'] } return channel_info def save_videos(self, channel_videos, video_file, total_channel_videos): """Saves metadata from videos for currently analyzed channel to a file. Args: channel_videos (array): Array with metadata from videos for currently analyzed channel. video_file (string): Path to a file where metadata from videos for currently analyzed channel will be saved. total_channel_videos (int): The number of metadata from videos successfully obtained for currently analyzed channel. """ self.logger.info('Saving to file...') self.logger.info( 'Total channel videos: {}'.format(total_channel_videos)) try: with open(video_file, 'w') as f: f.write(dumps(channel_videos, indent=4)) except Exception as e: raise Exception('Can not save videos to file.') self.logger.info('Saving finished.') def analyze_channel_videos(self, channel, channel_info): """Gets metadata from videos for currently analyzed channel. Args: channel (string): The id of the channel. channel_info (dictionary): The dictionary with basic channel informations (keys: 'channel_id', 'channel_likes') """ channel_videos = [] integrity_array = [] video_file = '{}_{}{}'.format(self.video_file_name, channel, self.video_file_extension) request_counter = 0 total_channel_videos = 0 fields = { 'uri', 'name', 'description', 'link', 'duration', 'width', 'height', 'language', 'created_time', 'modified_time', 'privacy', 'pictures', 'tags', 'stats', 'metadata', 'user.uri', } request = '/channels/{}/videos?fields={}&per_page=50&page=1&sort=date&direction=desc'.format( channel, ','.join(fields)) while request is not None: try: response = self.v.get(request).json() except Exception as e: self.logger.error( 'Request for video data from channel {} failed: {}'.format( channel, str(e))) break if 'data' not in response: break request_counter += 1 for video_d in response['data']: if self.perform_filtering(video_d): if video_d['uri'] in integrity_array: continue integrity_array.append(video_d['uri']) video_d.update(channel_info) channel_videos.append(video_d) total_channel_videos += 1 self.total_videos += 1 if request_counter == self.max_requests_per_save: request_counter = 0 self.save_videos(channel_videos, video_file, total_channel_videos) request = response['paging']['next'] self.save_videos(channel_videos, video_file, total_channel_videos) self.logger.info('Total videos: {}'.format(self.total_videos)) def start(self): """Starts crawling. """ self.logger.info('Start crawling') for channel in self.channels_array: self.logger.info('Analyzing channel: {}'.format(channel)) try: channel_info = self.analyze_channel(channel) self.analyze_channel_videos(channel, channel_info) except Exception as e: self.logger.error(str(e)) self.logger.info('Crawling finished.')
def __init__(self, access_token): #, data): """ Initiate the graph and find the OPENi album """ self.connector = VimeoClient(access_token=access_token)
class VimeoService(): def __init__(self, project, settings): self.project = project self.filename = None self.settings = settings self.token = None self.token_secret = None self.verifier = None self.form = None # Add language support _ = Language_Init.Translator(project).lang.gettext self._ = _ # get tokens (if already authorized) if self.settings.app_state["vimeo_token"]: self.token = self.settings.app_state["vimeo_token"] if self.settings.app_state["vimeo_token_secret"]: self.token_secret = self.settings.app_state["vimeo_token_secret"] if self.settings.app_state["vimeo_verifier"]: self.verifier = self.settings.app_state["vimeo_verifier"] def initialize(self, form): """ Prepare the upload form for this service """ # test the authentication if self.token: try: self.v = VimeoClient(token=self.token, token_secret=self.token_secret, verifier=self.verifier) self.v.cache_timeout = 0 self.uploader = self.v.get_uploader() except: # failed to authenticate, erase tokens self.token = None self.token_secret = None self.verifier = None self.settings.app_state["vimeo_token"] = "" self.settings.app_state["vimeo_token_secret"] = "" self.settings.app_state["vimeo_verifier"] = "" # Show error message messagebox.show(_("Validation Error!"), _("Vimeo authentication has expired.")) self.form = form form.lblUsername.set_property("visible", False) form.txtUsername.set_property("visible", False) form.lblPassword.set_property("visible", False) form.txtPassword.set_property("visible", False) if self.token: # already authorized form.login_divider.set_property("visible", False) form.btnAuthorize.set_property("visible", False) form.lblVerification.set_property("visible", False) form.txtVerification.set_property("visible", False) else: # user needs to authorize OpenShot form.login_divider.set_property("visible", True) form.btnAuthorize.set_property("visible", True) form.lblVerification.set_property("visible", True) form.txtVerification.set_property("visible", True) form.lnkForgot.set_label("http://www.vimeo.com") form.lnkForgot.set_uri("http://www.vimeo.com") def get_logo(self): logo_path = os.path.join(self.project.BASE_DIR, "openshot", "uploads", "logos", "vimeo.png") return gtk.gdk.pixbuf_new_from_file(logo_path) def get_export_presets(self): """ Get a tuple of related export presets for this service (if any) """ # get reference to gettext _ = self._ return (_("Web"), _("Vimeo-HD")) def get_authorization_url(self): self.v = VimeoClient() self.v.cache_timeout = 0 return self.v.get_authorization_url(permission="write") def validate(self, form): """ Validate the upload form... check for missing values. """ # get reference to gettext _ = self._ # get code verification_code = form.txtVerification.get_text() title = form.txtTitle.get_text() start, end = form.txtDescription.get_buffer().get_bounds() description = form.txtDescription.get_buffer().get_text(start, end) # Validate the the form is valid if not os.path.isfile(str(self.filename)): # Show error message messagebox.show(_("Validation Error!"), _("Please choose a valid video file.")) return False if not title: # Show error message messagebox.show(_("Validation Error!"), _("Please enter a valid title.")) return False if not self.token: if not description: # Show error message messagebox.show(_("Validation Error!"), _("Please enter a valid description.")) return False if not verification_code: # Show error message messagebox.show( _("Validation Error!"), _("Please enter a valid verification code. Click the 'Authorize' button and login to the website. Confirm the authorization, and copy the verification code." )) return False # form is valid return True def set_file(self, filename): self.filename = filename def start_upload(self, form): # get reference to gettext _ = self._ if not self.token: # Not Authorized Yet. # get code verification_code = form.txtVerification.get_text() try: # pass code and authorize OpenShot (hopefully) self.v.set_verifier(verification_code) access_token = self.v.get_access_token() # save tokens in settings self.verifier = verification_code self.settings.app_state["vimeo_verifier"] = self.verifier self.token = access_token.key self.settings.app_state["vimeo_token"] = self.token self.token_secret = access_token.secret self.settings.app_state[ "vimeo_token_secret"] = self.token_secret # Get uploader object self.uploader = self.v.get_uploader() except: # Show error message messagebox.show( _("Validation Error!"), _("There was an error authorizing OpenShot. Please be sure to enter the correct verification code from vimeo.com." )) # get settings title = form.txtTitle.get_text() start, end = form.txtDescription.get_buffer().get_bounds() description = form.txtDescription.get_buffer().get_text(start, end) try: # enable upload button form.btnUpload.set_sensitive(False) # Upload the file to Vimeo output = self.uploader.upload( self.filename, chunk=True, chunk_size=64 * 1024, chunk_complete_hook=self.on_chunk_complete) upload_ticket_id = output.get("id") output = self.v.videos_upload_complete(ticket_id=upload_ticket_id) video_id = output.get("video_id") # Set the name and description of the video self.v.videos_setTitle(title=title, video_id=video_id) self.v.videos_setDescription(description=description, video_id=video_id) except: # Show error message messagebox.show(_("Validation Error!"), _("There was an error uploading to Vimeo.")) # enable upload button form.btnUpload.set_sensitive(True) return False # enable upload button form.btnUpload.set_sensitive(True) # successful return True def on_chunk_complete(self, *args): #print "on_chunk_complete" total_size = args[0]["total_size"] chunk_size = args[0]["chunk_size"] chunk_id = args[0]["chunk_id"] + 1 # zero based # calculate current bytes transferred current_bytes = chunk_id * chunk_size if current_bytes >= total_size: # don't exceed the total bytes current_bytes = total_size # calculate percentage percent = float(current_bytes) / float(total_size) gobject.idle_add(self.form.update_progressbar, percent) # allow other gtk operations to happen while gtk.events_pending(): gtk.main_iteration()
def get_authorization_url(self): self.v = VimeoClient() self.v.cache_timeout = 0 return self.v.get_authorization_url(permission="write")
def search(): """ Handling of POST requests for autocomplete.js """ if request.method == "POST": max_results = 3 result = [] debug(("Incoming POST request: {}").format(request.json["search"])) yt_search_request = ( "{}/search?q={}&type=playlist&part=id,snippet" + "&fields=items(id/playlistId,snippet(thumbnails/medium/url,title))" + "&maxResults={}&key={}").format( read_config("YOUTUBE_API_URL"), quote(request.json["search"]), max_results, read_config("YOUTUBE_API_KEY")) yt_search_response = urllib_request.urlopen(yt_search_request) youtube = loads(yt_search_response.read().decode()) VIMEO = VimeoClient( token=read_config("VIMEO_TOKEN"), key=read_config("VIMEO_KEY"), secret=read_config("VIMEO_SECRET")) vim_search_request = VIMEO.get(("/channels?query={}&per_page={}").format(quote(request.json["search"]), max_results), params={"fields": "name, uri, pictures.uri, metadata.connections.videos.total"}) vimeo = vim_search_request.json() for playlist in youtube["items"]: req = ( "{}/playlistItems?playlistId={}" + "&part=id&fields=pageInfo/totalResults" + "&maxresults=1&key={}").format( read_config("YOUTUBE_API_URL"), playlist["id"]["playlistId"], read_config("YOUTUBE_API_KEY")) request_send = urllib_request.urlopen(req) videos_in_playlist = loads(request_send.read().decode()) #TODO: decide what to return in case of missing thumbnail thumbnail_url = "" if "thumbnails" in playlist["snippet"]: # api call needed as playlist thumbnail != thumbnail of first video (or not inevitable) thumbnail_url = playlist["snippet"]["thumbnails"]["medium"]["url"] result.append({ "source": "youtube", "id": playlist["id"]["playlistId"], "title": playlist["snippet"]["title"], "thumb": thumbnail_url, "amount": videos_in_playlist["pageInfo"]["totalResults"]}) for video in vimeo["data"]: result.append({ "source": "vimeo", "id": video["uri"].split("/")[2], "title": video["name"], #TODO: check if thumbnail of first video is always thumbnail of channel (or customizable as on YouTube) "thumb": ("https://i.vimeocdn.com/video/{}_100x75.jpg").format(video["pictures"]["uri"].split("/")[4]), "amount": video["metadata"]["connections"]["videos"]["total"] }) return dumps(result)
from vimeo import VimeoClient vimeo = VimeoClient("fd43cedfe52f09848142cbd3501ea0a5") a = vimeo.channels() b = vimeo.videos(query="battle") c = vimeo.videos.get("87374427") for i in a.items(): print i print print print print print print for i in b.items(): print i print print print print print "Comments" print c.comments() print print "Credits" print c.credits()
class Vlog(object): def __init__(self, conf, blog): self.log = getLogger(__name__) self.conf = conf self.blog = blog self.client = VimeoClient( token=self.conf.vimeo_token, key=self.conf.vimeo_client_id, secret=self.conf.vimeo_client_secret ) def get(self, *uri, **param): url = '/{}'.format('/'.join(uri).lstrip('/')) self.log.debug('request vlog info "%s"', url) req = self.client.get(url, **param) res = req.json() if req.status_code != 200: self.log.warning('vlog info error response "%s"', res) return return res def quota(self, size): info = self.get('me') if info: return info['upload_quota']['space']['free'] > size def upload(self, source): res = self.client.post('/me/videos', data=dict( type='pull', link=source )) video = res.json() if res.status_code != 200: self.log.error('video upload error "%s"', video) return return video def change(self, video, *, title, caption, public, tags=[]): res = self.client.patch(video['uri'], data=dict( name=title, description=caption, privacy=dict( embed='public', view=('anybody' if public else 'nobody'), ) )) if res.status_code != 200: self.log.error('video edit error "%s"', res.json()) return res = self.client.put('{}/tags'.format(video['uri']), data=tags) if res.status_code not in [200, 201]: self.log.error('video tag error "%s"', res.json()) return return video def pull_videos(self): for offset in range(1, self.get('me', 'videos').get('total', 1), 25): for post in self.get( 'me', 'videos', data=dict(page=offset) )['data']: yield post