示例#1
0
文件: views.py 项目: nceruchalu/ycp
def uploadBrowser(request):
    """
    See:
    https://developers.google.com/youtube/2.0/developers_guide_protocol#Browser_based_uploading
        
    Steps:
    1. Submit an Upload API request. This request request contains the
       information about the uploaded video file, but does NOT include the
       actual video file. So this includes meta data like title, description,
       category, keywords.
       
    2. Extract values from the API response. The API response contains an upload
       URL and upload token that enable the user to upload the actual video file
       These will have to be included on the webpage where the user submits the
       video file.
    
    3. After extracting the upload URL and upload token from the API response,
       display a form so that the user can upload the actual video file.
       The form must use the upload URL as the value of the <form> tag's action
       attribute and have a hidden input field containing the upload token.
       The form should verify that the user actually has selected a file to
       upload before allowing the user to actually submit the form. [Use JS]
       
    
    Author:      Nnoduka Eruchalu
    """
    if request.method == 'POST':
        # get the metadata
        form = YoutubeBrowerUploadForm(request.POST)
        if form.is_valid():
            # will only create video info after upload finishes
                        
            # try to create post_url and token to create a video upload form
            api = Api()
            # upload method needs authentication
            api.authenticate()
            data = api.uploadBrowser(
                title = form.cleaned_data['title'],
                description = form.cleaned_data['description'],
                keywords = form.cleaned_data['tags'])
            
            protocol = 'https' if request.is_secure() else 'http'
            next_url = '%s://%s%s' % (protocol, request.get_host(),
                                       reverse('youtubeUploadBrowserReturn'))
            # create video upload form
            return render_to_response("youtube/uploadBrowserVideo.html",
                                      {'youtube_token':data['youtube_token'],
                                       'post_url':data['post_url'],
                                       'next_url':next_url
                                       },
                                      context_instance =RequestContext(request))

    # if request.method != 'POST'
    else:
        form = YoutubeBrowerUploadForm()
    
    return render_to_response("youtube/uploadBrowserMeta.html",
                              {'form':form,
                                'nav_current':'file'},
                              context_instance =RequestContext(request))
示例#2
0
文件: models.py 项目: nceruchalu/ycp
 def entry(self):
     """
     Description: Connect to Youtube Api and retrieves the video entry object
     
     Arguments:   None
     Return:      YouTube API video entry object
     
     Author:      Nnoduka Eruchalu
     """
     api = Api()
     api.authenticate()
     return api.getVideo(self.video_id)
示例#3
0
文件: models.py 项目: nceruchalu/ycp
 def save(self, *args, **kwargs):
     """
     Description: Synchronize the video info on db with that on YouTube
     
     Arguments:   *args, **kwargs
     Return:      None
     
     Author:      Nnoduka Eruchalu
     """
     # if this is a new instance add details from api
     if self._get_pk_val() is None:
         # save the instance so can call tags.set()
         super(Video, self).save(*args, **kwargs)
         
         # connect to api and get the details
         entry = self.entry()
         self.sync_db(entry)
             
         # save the instance
         super(Video, self).save(*args, **kwargs)
             
         # save thumbnails
         for thumbnail in entry.media.thumbnail:
             t = Thumbnail()
             t.url = thumbnail.url
             t.video = self
             t.save()
          
         # can't import on startup... because Media imports this file
         from ycp.apps.media.models import Media
         Media(video=self).save()
     
     # if this is an instance being updated
     else:
         if self.file_upload: # can only modify video if you own it
             # connect to the api and update the video on YouTube
             api = Api()
             api.authenticate()
             # Update the info on youtube
             keywords = [t.name for t in self.tags.all()]
             
             updated_entry = api.updateVideo(self.video_id, self.title,
                                             self.description, keywords,
                                             self.private)
             
             # and sync youtube and the db
             self.sync_db(updated_entry)
             
         # save the instance
         super(Video, self).save(*args, **kwargs)
示例#4
0
文件: models.py 项目: nceruchalu/ycp
    def delete(self, *args, **kwargs):
        """
        Description: Deletes the video from youtube if you own the video
        
        Arguments:   *args, **kwargs
        Return:      None
        
        Author:      Nnoduka Eruchalu
        """
        if self.file_upload:
            api = Api()
            
            # authentication required for delete
            api.authenticate()
            
            # send YouTube delete request
            api.deleteVideo(self.video_id)

        # clear tags()
        self.tags.clear()
                
        # call the super method
        return super(Video, self).delete(*args, **kwargs)