コード例 #1
0
    def handle(self, *args, **options):
        youtube_url_video = options['youtube_url_video']

        # Get code from youtube url
        video_code = ""
        try:
            video_code = get_video_code(youtube_url_video)
        except Exception:
            msg = "ERROR: Invalid URL video {:s}".format(youtube_url_video)
            self.stdout.write(self.style.ERROR(msg))
            return

        # Check if the talk is already on the database
        if Talk.objects.filter(code=video_code).exists():
            msg = "ERROR: Talk {:s} is already present on the database".format(
                video_code)
            self.stdout.write(self.style.NOTICE(msg))

            # Call to update command instead
            management.call_command("update_talk", youtube_url_video)
            return

        msg = "Creating talk code:{:s}".format(video_code)
        self.stdout.write(msg)

        # Fetch channel data from Youtube API
        video_json_data = fetch_video_data(settings.YOUTUBE_API_KEY,
                                           video_code)

        # If no data is received do nothing
        if video_json_data is None:
            msg = "ERROR: Youtube Data API does not return anything for video {:s}".format(
                video_code)
            self.stdout.write(self.style.ERROR(msg))
            return

        talk_obj = talk.create_talk(video_json_data,
                                    playlist=options['playlist'])

        msg = "Talk id:{:d} - title:{:s} created successfully".format(
            talk_obj.id, talk_obj.title)
        self.stdout.write(self.style.SUCCESS(msg))
コード例 #2
0
    def handle(self, *args, **options):
        youtube_url_video = options['youtube_url_video']

        # Get code from youtube url
        video_code = ""
        try:
            video_code = get_video_code(youtube_url_video)
        except Exception:
            msg = "ERROR: Invalid URL video {:s}".format(youtube_url_video)
            self.stdout.write(self.style.ERROR(msg))
            return

        # Check if the talk is already on the database
        if not Talk.objects.filter(code=video_code).exists():
            msg = "ERROR: Talk {:s} is not present on the database".format(
                video_code)
            self.stdout.write(self.style.NOTICE(msg))

            # Call to create command instead
            management.call_command("create_video", youtube_url_video)
            return

        # Get the talk from the database
        talk_obj = Talk.objects.get(code=video_code)

        # Check if it is outdated
        delta = timezone.now() - talk_obj.updated
        if delta.total_seconds() <= settings.UPDATE_THRESHOLD:
            msg = "Talk code:{:s} have been updated in the last 24h seconds:{:f}".format(
                talk_obj.code, delta.total_seconds())
            self.stdout.write(msg)
            return

        msg = "Updating talk code:{:s}".format(talk_obj.code)
        self.stdout.write(msg)

        # Fetch video data from Youtube API
        try:
            video_json_data = fetch_video_data(settings.YOUTUBE_API_KEY,
                                               talk_obj.code)
        except Exception:
            msg = "ERROR: Youtube Data API returns 404 Not Found for {:s}".format(
                talk_obj.code)
            self.stdout.write(self.style.ERROR(msg))
            return

        # If no data is received un-publish the Talk
        if video_json_data is None:
            msg = "ERROR: Youtube Data API does not return anything for video {:s}".format(
                talk_obj.code)
            self.stdout.write(self.style.ERROR(msg))
            talk_obj.published = False
            talk_obj.updated = timezone.now()
            talk_obj.save()
            return

        # if uploadStatus on youtube is failed we un-publish the video
        if "status" in video_json_data:
            status = video_json_data['status']
            if "uploadStatus" in status:
                if status['uploadStatus'] == "failed":
                    msg = "ERROR: Youtube statusUpload is failed unpublish video {:s}".format(
                        talk_obj.code)
                    self.stdout.write(self.style.ERROR(msg))
                    talk.published = False
                    talk_obj.updated = timezone.now()
                    talk.save()
                    return

        talk.update_talk(talk_obj,
                         video_json_data,
                         playlist=options["playlist"])

        msg = "Talk id:{:d} - title:{:s} updated successfully".format(
            talk_obj.id, talk_obj.title)
        self.stdout.write(self.style.SUCCESS(msg))
コード例 #3
0
    def test_video_code_fail(self):
        url = "invalid_url"
        with self.assertRaises(Exception) as context:
            video.get_video_code(url)

        self.assertTrue('Invalid url "invalid_url"' in str(context.exception))
コード例 #4
0
 def test_video_code(self):
     url = "https://www.youtube.com/watch?v=5UG57xQL_RE"
     expected_code = "5UG57xQL_RE"
     video_code = video.get_video_code(url)
     self.assertEqual(expected_code, video_code)
コード例 #5
0
    def _get_output_path(self):
        self.video_code = video.get_video_code(self.youtube_url)
        output_path = "/opt/pipeline/data/youtube/talks/{:s}.json".format(
            self.video_code)

        return output_path