Example #1
0
 def test_create_from_json_oembed(self):
     o_encoded = [
         'https://vimeo.com/api/oembed.json?url=https%3A%2F%2Fvimeo.com%2F7100569',
         'https://www.youtube.com/oembed?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DbDOYN-6gdRE&format=json&maxwidth=550'
     ]
     #get each url
     #and
     #
     test_responses = [
         r'{"provider_url": "https:\/\/www.youtube.com\/", "title": "Auto-Tune the News #8: dragons. geese. Michael Vick. (ft. T-Pain)", "html": "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"https:\/\/www.youtube.com\/e\/bDOYN-6gdRE\"><\/param><param name=\"allowFullScreen\" value=\"true\"><\/param><param name=\"allowscriptaccess\" value=\"always\"><\/param><embed src=\"https:\/\/www.youtube.com\/e\/bDOYN-6gdRE\" type=\"application\/x-shockwave-flash\" width=\"425\" height=\"344\" allowscriptaccess=\"always\" allowfullscreen=\"true\"><\/embed><\/object>", "author_name": "schmoyoho", "height": 344, "thumbnail_width": 480, "width": 425, "version": "1.0", "author_url": "https:\/\/www.youtube.com\/user\/schmoyoho", "provider_name": "YouTube", "thumbnail_url": "http:\/\/i3.ytimg.com\/vi\/bDOYN-6gdRE\/hqdefault.jpg", "type": "video", "thumbnail_height": 360}',
         r'{"type":"video","version":"1.0","provider_name":"Vimeo","provider_url":"https:\/\/vimeo.com\/","title":"Brad!","author_name":"Casey Donahue","author_url":"https:\/\/vimeo.com\/caseydonahue","is_plus":"1","html":"<iframe src=\"https:\/\/player.vimeo.com\/video\/7100569\" width=\"1280\" height=\"720\" frameborder=\"0\"><\/iframe>","width":"1280","height":"720","duration":"118","description":"Brad finally gets the attention he deserves.","thumbnail_url":"http:\/\/b.vimeocdn.com\/ts\/294\/128\/29412830_1280.jpg","thumbnail_width":1280,"thumbnail_height":720,"video_id":"7100569"}',
     ]
     for response in test_responses:
         Sourcefile.create_from_json_oembed(response)
Example #2
0
    def on_thumbnail_response(self, response):
        if response.code != 200:
            self.render(
                "tools/save-video-error.html",
                message=
                "We could not load the thumbnail for this file and therefore could not save this video. Please contact support."
            )
            return

        # save the response
        url = self.get_argument('url')
        current_user = self.get_current_user_object()

        sha1_key = Sourcefile.get_sha1_file_key(file_path=None, file_data=url)
        thumbnail_path = "%s/%s" % (options.uploaded_files, sha1_key)
        fh = open(thumbnail_path, 'wb')
        fh.write(response.body)
        fh.close()
        source_file = Sourcefile.create_from_json_oembed(
            link=url,
            oembed_doc=self.oembed_doc,
            thumbnail_file_path=thumbnail_path)
        #cleanup
        if not options.debug:
            try:
                os.remove(thumbnail_path)
            except:
                pass

        title = ''
        if self.oembed_doc.has_key('title'):
            title = self.oembed_doc['title']

        shared_file = Sharedfile(user_id=current_user.id,
                                 name=url,
                                 content_type='text/html',
                                 source_id=source_file.id,
                                 title=title,
                                 source_url=url)
        shared_file.save()

        share_key = base36encode(shared_file.id)
        shared_file.share_key = share_key
        shared_file.save()

        user_shake = Shake.get('user_id = %s and type=%s', current_user.id,
                               'user')
        shared_file.add_to_shake(self.destination_shake)

        if self.oembed_doc.has_key('description'):
            shared_file.description = self.oembed_doc['description']

        self.write({'path': "/p/%s" % (share_key)})
        self.finish()
Example #3
0
    def on_thumbnail_response(self, response):
        if response.code != 200:
            self.render(
                "tools/save-video-error.html",
                message=
                "We could not load the thumbnail for this file and therefore could not save this video. Please contact support."
            )
            return

        #if the thumbnail url needs to be extracted (Flickr) let's see if
        # we got back HTML that points to the thumbnail
        if self.oembed_doc['provider_name'] == "Flickr" and response.headers[
                'Content-Type'] == 'text/html; charset=utf-8':
            #if we're here, that means we need to extract the thumbnail and make a call to the actual jpg
            s = re.search(
                '<link rel="image_src" href="http://farm(\d).static.flickr.com/(\d+)/(\d+)_([a-zA-Z0-9]+)_m.jpg">',
                response.body)
            try:
                if s and s.group(0) and s.group(1) and s.group(2) and s.group(
                        3) and s.group(4):
                    self.oembed_doc[
                        'thumbnail_url'] = "http://farm%s.static.flickr.com/%s/%s_%s_b.jpg" % (
                            s.group(1), s.group(2), s.group(3), s.group(4))
                    request = HTTPRequest(self.oembed_doc['thumbnail_url'],
                                          'GET')
                    http = tornado.httpclient.AsyncHTTPClient()
                    http.fetch(request, self.on_thumbnail_response)
            except:
                self.render(
                    "tools/save-video-error.html",
                    message=
                    "We could not load the thumbnail for this file and therefore could not save this video. Please contact support."
                )
                return
        elif self.oembed_doc['provider_name'] == "Vine" and response.headers[
                'Content-Type'] == 'text/html; charset=utf-8':
            # if we're here, that means we need to extract the thumbnail and make a call to the actual jpg
            # use BeautfilSoup to parse for the title and meta tag. We'll do this bit of danger in a
            # try block and shrug if something bad happens
            try:
                soup = BeautifulSoup(
                    response.body, convertEntities=BeautifulSoup.HTML_ENTITIES)
                self.oembed_doc['title'] = soup.title.text
                thumbnail = soup.find('meta', {"property": "og:image"})
                if thumbnail:
                    self.oembed_doc['thumbnail_url'] = thumbnail.attrMap[
                        'content']

                    request = HTTPRequest(self.oembed_doc['thumbnail_url'],
                                          'GET')
                    http = tornado.httpclient.AsyncHTTPClient()
                    http.fetch(request, self.on_thumbnail_response)
                    return
            except:
                pass
            # either we failed to find a thumbnail url, or an exception was raised
            # while attempting to fetch.
            self.render(
                "tools/save-video-error.html",
                message=
                "We could not load the thumbnail for this file and therefore could not save this video. Please contact support."
            )
        else:
            # save the response
            url = self.get_argument('url')
            current_user = self.get_current_user_object()

            sha1_key = Sourcefile.get_sha1_file_key(file_path=None,
                                                    file_data=url)
            thumbnail_path = "%s/%s" % (options.uploaded_files, sha1_key)
            fh = open(thumbnail_path, 'wb')
            fh.write(response.body)
            fh.close()
            source_file = Sourcefile.create_from_json_oembed(
                link=url,
                oembed_doc=self.oembed_doc,
                thumbnail_file_path=thumbnail_path)
            #cleanup
            if not options.debug:
                try:
                    os.remove(thumbnail_path)
                except:
                    pass

            title = ''
            if self.oembed_doc.has_key('title'):
                title = self.oembed_doc['title']

            shared_file = Sharedfile(user_id=current_user.id,
                                     name=url,
                                     content_type='text/html',
                                     source_id=source_file.id,
                                     title=title,
                                     source_url=url)
            shared_file.save()

            share_key = base36encode(shared_file.id)
            shared_file.share_key = share_key
            shared_file.save()

            user_shake = Shake.get('user_id = %s and type=%s', current_user.id,
                                   'user')
            shared_file.add_to_shake(self.destination_shake)

            if self.oembed_doc.has_key('description'):
                shared_file.description = self.oembed_doc['description']

            self.write({'path': "/p/%s" % (share_key)})
            self.finish()