def test_serialize__files(self):
        """
        Tests that a video with associated files can still be serialized and
        deserialized.

        """
        video = Video("http://www.youtube.com/watch?v=J_DV9b0x7v4")
        now = datetime.datetime.now()
        video.files = [VideoFile(url='http://google.com',
                                 expires=now,
                                 length=100,
                                 width=50,
                                 height=50,
                                 mime_type="video/x-flv"),
                       VideoFile(url='http://xkcd.com',
                                 expires=now,
                                 length=75,
                                 width=80,
                                 height=80,
                                 mime_type="application/x-shockwave-flash"),]

        data = video.serialize()
        # verify that the data we expect is in the serialized version.
        self.assertEqual(data['files'][0]['url'], "http://google.com")
        self.assertEqual(data['files'][1]['mime_type'],
                         "application/x-shockwave-flash")
        self.assertEqual(data['files'][0]['expires'], now.isoformat())

        # Verify that the data can be deserialized as a video.
        new_video = Video.deserialize(data)
        self.assertEqual(dict(video.items()), dict(new_video.items()))
def video_from_vidscraper_video(video_dict, site_pk,
                                import_app_label=None, import_model=None,
                                import_pk=None, status=None, author_pks=None,
                                category_pks=None, clear_rejected=False):
    vidscraper_video = VidscraperVideo.deserialize(video_dict, API_KEYS)
    import_class = get_model(import_app_label, import_model)
    try:
        source_import = import_class.objects.get(
           pk=import_pk,
           status=import_class.STARTED)
    except import_class.DoesNotExist:
        logging.warn('Retrying %r: expected %s instance (pk=%r) missing.',
                     vidscraper_video.url, import_class.__name__, import_pk)
        video_from_vidscraper_video.retry()

    try:
        try:
            vidscraper_video.load()
        except Exception:
            source_import.handle_error(
                ('Skipped %r: Could not load video data.'
                 % vidscraper_video.url),
                is_skip=True, with_exception=True)
            return

        if category_pks:
            categories = Category.objects.filter(pk__in=category_pks)
        else:
            categories = None

        if author_pks:
            authors = User.objects.filter(pk__in=author_pks)
        else:
            authors = None

        video = Video.from_vidscraper_video(vidscraper_video, status=status,
                                            source_import=source_import,
                                            authors=authors,
                                            categories=categories,
                                            site_pk=site_pk,
                                            commit=False,
                                            update_index=False)
        try:
            video.clean_fields()
            # If clear_rejected is True, we've already deleted any rejected
            # videos, so there's no need to explicitly exclude them.
            # If clear_rejected is False, this is not the first run, and
            # so rejected videos need to not be excluded in this check.
            video._check_for_duplicates(exclude_rejected=False)
            video.validate_unique()
        except ValidationError, e:
            source_import.handle_error(("Skipping %r: %r" % (
                                        vidscraper_video.url, e.message)),
                                       is_skip=True)
            return
        else:
Exemple #3
0
    def test_serialize(self):
        video = Video("http://www.youtube.com/watch?v=J_DV9b0x7v4")
        # we load the video data this way to avoid depending on the network
        video_data = CARAMELL_DANSEN_API_DATA.copy()
        video_data['tags'] = list(video_data['tags'])
        video._apply(video_data)

        data = video.serialize()
        # verify that the data we expect is in the serialized version.
        self.assertEqual(data['url'], video.url)
        self.assertEqual(data['title'], video.title)
        self.assertEqual(data['publish_datetime'],
                         video.publish_datetime.isoformat())

        # Verify that the data can be deserialized as a video.
        new_video = Video.deserialize(data)
        self.assertEqual(video.url, new_video.url)
        self.assertEqual(dict(video.items()), dict(new_video.items()))

        # Verify that data can be deserialized more than once.
        # (i.e. that it is not modified in-place during deserialization.)
        Video.deserialize(data)
Exemple #4
0
    def test_serialize(self):
        video = Video("http://www.youtube.com/watch?v=J_DV9b0x7v4")
        # we load the video data this way to avoid depending on the network
        video_data = CARAMELL_DANSEN_API_DATA.copy()
        video_data['tags'] = list(video_data['tags'])
        video._apply(video_data)

        data = video.serialize()
        # verify that the data we expect is in the serialized version.
        self.assertEqual(data['url'], video.url)
        self.assertEqual(data['title'], video.title)
        self.assertEqual(data['publish_datetime'],
                         video.publish_datetime.isoformat())

        # Verify that the data can be deserialized as a video.
        new_video = Video.deserialize(data)
        self.assertEqual(video.url, new_video.url)
        self.assertEqual(dict(video.items()), dict(new_video.items()))
    def test_serialize__partial(self):
        """
        Tests that a video with only some fields can still be serialized and
        deserialized.

        """
        video = Video("http://www.youtube.com/watch?v=J_DV9b0x7v4",
                      fields=('title', 'embed_code'))
        # we load the video data this way to avoid depending on the network
        video_data = CARAMELL_DANSEN_API_DATA.copy()
        video._apply(video_data)
        data = video.serialize()

        # verify that the data we expect is in the serialized version.
        self.assertEqual(data['url'], video.url)
        self.assertEqual(data['title'], video.title)
        self.assertEqual(data['embed_code'], video.embed_code)

        # Verify that the data can be deserialized as a video.
        new_video = Video.deserialize(data)
        self.assertEqual(video.url, new_video.url)
        self.assertEqual(dict(video.items()), dict(new_video.items()))