Пример #1
0
def scribe(video_id: str, youtube_api_key: str):

    if not youtube_api_key:
        log.error(
            'You need to provide an API key either by --youtube-api0key or by setting YOUTUBE_API_KEY'
        )
        sys.exit(1)

    api = Api(api_key=youtube_api_key)
    video_metadata = api.get_video_by_id(video_id=video_id).items[0]

    #log.debug(json.dumps(video_metadata.to_dict(), sort_keys=True, indent=2))

    title = video_metadata.snippet.title
    preview_image_path = get_preview_image(
        img_url=video_metadata.snippet.thumbnails.default.url,
        video_id=video_id)
    description = video_metadata.snippet.description
    date = datetime.datetime.strptime(video_metadata.snippet.publishedAt,
                                      "%Y-%m-%dT%H:%M:%S%z")
    captions = YouTubeTranscriptApi.get_transcript(video_id)

    print(
        gen_markdown_page(video_id=video_id,
                          title=title,
                          image_path=preview_image_path,
                          description=description,
                          date=date,
                          captions=captions))
Пример #2
0
class YTSearch():
    def __init__(self, api_key=API_KEY):
        self.key = api_key
        self.api = Api(api_key=self.key)

    def check_video_eligibility(self, ids, expected_type):

        like_max = {"yt_id": "", "youtube_rating": 0.0}
        for id in ids:
            video = list(
                self.api.get_video_by_id(video_id=id).to_dict().items())
            try:
                vid = video[5][1][0]
                duration = isodate.parse_duration(
                    vid['contentDetails']['duration']).total_seconds()
                definition = vid['contentDetails']['definition']
                like_count = float(vid["statistics"]["likeCount"])
                dislike_count = float(vid["statistics"]["dislikeCount"])
                duration_min = Duration[expected_type][0]
                duration_max = Duration[expected_type][1]
                like_percentage = like_count / (like_count + dislike_count)
                channel_title = vid["snippet"]["channelTitle"]
                if like_percentage > like_max["youtube_rating"] and (duration_min < duration <= duration_max) and \
                        channel_title != 'YouTube Movies':
                    like_max["yt_id"] = id
                    like_max["yt_duration"] = duration
                    like_max["yt_likes"] = like_count
                    like_max["youtube_rating"] = like_percentage
                    like_max["yt_definition"] = definition
            except Exception as e:
                print("ignoring the error")
                continue
        return like_max

    def obsolete_get_stats(self,
                           key_word,
                           expected_type="movie",
                           max_results=10):
        videos = list(
            self.api.search_by_keywords(q=key_word,
                                        search_type=["video"]).items)
        vids = [vid.to_dict()["id"]['videoId'] for vid in videos]
        ret = self.check_video_eligibility(vids, expected_type)
        print(ret)

    def get_youtube_stats(self,
                          key_word,
                          expected_type="movie",
                          max_results=10):
        get_mov_stats = YoutubeSearch(key_word,
                                      max_results=max_results).to_dict()
        ids = [d['id'] for d in get_mov_stats]
        ret = self.check_video_eligibility(ids, expected_type)
        for mov in get_mov_stats:
            if mov["id"] == ret["yt_id"]:
                ret["youtube_link"] = "https://youtube.com" + mov["link"]
        return (ret)
Пример #3
0
def process_play_list_step(message):
    try:
        chat_id = message.chat.id
        text = message.text
        if ":" not in text:
            bot.reply_to(message, 'Wrong message format')
            return

        data = str(text).split(':')
        plaid = data[0]
        title = data[1]

        if len(plaid) <= 5 or len(title) <= 5:
            bot.reply_to(message, 'Wrong message format')
            return

        api = Api(api_key=youtube_api_key)
        playlist_item_by_playlist = api.get_playlist_items(playlist_id=plaid,
                                                           count=1000)
        videos = playlist_item_by_playlist.items
        if len(videos) <= 0:
            bot.send_message(message.chat.id,
                             "No Vidoes found for playlist = " + str(title))
            return

        bot.send_message(message.chat.id,
                         "Found " + str(len(videos)) + " videos from youtube")
        real_video_count = len(videos)
        count = 0
        lessons = []
        for video in videos:
            video_by_id = api.get_video_by_id(
                video_id=video.snippet.resourceId.videoId,
                parts=('snippet', 'contentDetails', 'statistics'))
            if len(video_by_id.items) <= 0:
                real_video_count = real_video_count - 1
                continue
            count = count + 1
            item = video_by_id.items[0]
            title = title + " " + formatLessonName(
                count) + " " + item.snippet.title
            time_val = isodate.parse_duration(item.contentDetails.duration)
            code = getYoutubeEmbedCode(video.snippet.resourceId.videoId)
            lesson = Lesson(title, code, time_val)
            lessons.append(lesson)

        bot.send_message(
            message.chat.id,
            "Total avalibale vidoe count = " + str(real_video_count))
        if len(lessons) <= 0:
            bot.send_message(message.chat.id, "No lesson found!")
        loginAndUpdate(lessons, message, plaid)

    except Exception as e:
        bot.reply_to(message, str(e))
Пример #4
0
 def get_youtube_statistics(video_ids):
     api_key = frappe.db.get_single_value("Video Settings", "api_key")
     api = Api(api_key=api_key)
     try:
         video = api.get_video_by_id(video_id=video_ids)
         video_stats = video.items
         return video_stats
     except Exception:
         title = "Failed to Update YouTube Statistics"
         frappe.log_error(title + "\n\n" + frappe.get_traceback(),
                          title=title)
Пример #5
0
    def set_youtube_statistics(self):
        api_key = frappe.db.get_single_value("Video Settings", "api_key")
        api = Api(api_key=api_key)

        try:
            video = api.get_video_by_id(video_id=self.youtube_video_id)
            video_stats = video.items[0].to_dict().get('statistics')

            self.like_count = video_stats.get('likeCount')
            self.view_count = video_stats.get('viewCount')
            self.dislike_count = video_stats.get('dislikeCount')
            self.comment_count = video_stats.get('commentCount')

        except Exception:
            title = "Failed to Update YouTube Statistics for Video: {0}".format(
                self.name)
            frappe.log_error(title + "\n\n" + frappe.get_traceback(),
                             title=title)
Пример #6
0
    # Then create another nested folder. Example: output/output-2020-09-14T173551
    outputFolderPath = join(
        'output', 'output-' + strftime('%Y-%m-%dT%H%M%S', localtime()))
    mkdir(outputFolderPath)

    # Remember to comment out the individual playlist keys from MyConfigurations if you don't want to loop through them all
    for key in playlistIDs:
        playlistVideoItems = api.get_playlist_items(
            playlist_id=playlistIDs[key], count=None).items

        currentTimestamp = strftime(' %Y-%m-%dT%H%M%S', localtime())
        outputFileName = key + currentTimestamp + '.out'
        outputFilePath = join(outputFolderPath, outputFileName)

        with open(outputFilePath, 'w', encoding='utf8') as outfile:
            for index, playlistVideo in enumerate(playlistVideoItems):

                try:
                    video = api.get_video_by_id(
                        video_id=playlistVideo.contentDetails.videoId).items[0]

                    if not isVideoBlocked(countryCode, video):
                        outfile.write(video.snippet.title + '\n')
                        if displayUploader:
                            outfile.write(f'{video.snippet.channelTitle}\n\n')

                except Exception as e:
                    print(
                        f'\n\nException occurred on "{playlistVideo.snippet.title}" within playlist {key} at index {index}: {e}'
                    )
Пример #7
0
class EdumaBot():
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.api = Api(api_key=apikey)
        self.db = DBHelper()

    def login(self):
        self.driver.get(url)

        time.sleep(1)
        login_btn = self.driver.find_element_by_xpath('//*[@id="wp-submit"]')
        email_field = self.driver.find_element_by_xpath(
            '//*[@id="user_login"]')
        pass_field = self.driver.find_element_by_xpath('//*[@id="user_pass"]')

        email_field.send_keys(username)
        time.sleep(1)
        pass_field.send_keys(password)
        login_btn.click()
        self.driver.find_element_by_xpath(
            '//*[@id="toplevel_page_learn_press"]').click()
        self.driver.find_element_by_xpath(
            '//*[@id="toplevel_page_learn_press"]/ul/li[3]/a').click()
        self.driver.find_element_by_xpath(
            '//*[@id="wpbody-content"]/div[3]/a').click()

    def start(self):
        self.login()
        courses = []
        # resume_lesons = self.db.get_resume_lessons()
        #
        # print(resume_lesons.rowcount)
        #
        # if resume_lesons.rowcount != -1:
        #     for ressume_lesson in resume_lesons:
        #         index = ressume_lesson[4]
        #         total = ressume_lesson[3]
        #         title = ressume_lesson[2]
        #         playlist_id = ressume_lesson[1]
        #
        #         if int(index) == int(total):
        #             continue
        #
        #         print("found lesons that is not added index = {} ,playlist = {},count = {} ".format(index, playlist_id,
        #                                                                                             total))
        #         courses.append(Course(title=str(title), playlistId=str(playlist_id), start=str(index + 1)))

        f = open("playlist", "r+")
        for i in f:
            k = i.split('\n')
            for j in k:
                vals = j.split("[|]")
                if len(vals) == 2:
                    courses.append(
                        Course(title=str(vals[1]),
                               playlistId=str(vals[0]),
                               start=0))

        if len(courses) == 0:
            print("no lesson found on file =  " + f.name)
            return

        print("course found on file " + str(len(courses)))

        for course in courses:

            finsihed = self.db.getFinishedPlayLists(
                playlist_id=course.playlistId)
            print(finsihed)
            if finsihed.rowcount != -1:
                print("course already exist titile =  " + course.title)
                continue

            playlist_item_by_playlist = self.api.get_playlist_items(
                playlist_id=course.playlistId, count=1000)
            videos = playlist_item_by_playlist.items
            print("number of vidoes in playlist = " + str(len(videos)))
            if len(videos) <= 0:
                print("no videos found for  playlist = " +
                      str(course.playlistId) + "\n")
                continue
            count = 1
            real_video_count = len(videos)
            lesson_list = []

            temp_counter = 0
            for video in videos:

                temp_counter = temp_counter + 1
                if temp_counter <= -1 and course.playlistId == "":
                    continue

                count = temp_counter

                video_by_id = self.api.get_video_by_id(
                    video_id=video.snippet.resourceId.videoId,
                    parts=('snippet', 'contentDetails', 'statistics'))
                if len(video_by_id.items) <= 0:
                    print("missed or private vidoe for  = " +
                          str(course.playlistId) + "\n")
                    real_video_count = real_video_count - 1
                    continue

                item = video_by_id.items[0]
                title = course.title + " " + self.formatLessonName(
                    count) + " " + item.snippet.title

                time_val = isodate.parse_duration(item.contentDetails.duration)
                code = self.getYoutubeEmbedCode(
                    video.snippet.resourceId.videoId)
                count = count + 1
                lesson = Lesson(title, code, time)

                title_field = self.driver.find_element_by_xpath(
                    '//*[@id="title"]')
                media_field = self.driver.find_element_by_xpath(
                    '//*[@id="_lp_lesson_video_intro"]')
                date_field = self.driver.find_element_by_xpath(
                    '//*[@id="_lp_duration"]')
                publish_button = self.driver.find_element_by_xpath(
                    '//*[@id="publish"]')
                select = Select(
                    self.driver.find_element_by_id('_lp_duration_select'))

                time_array = str(time_val).split(":")

                print(time_array[0] + " = hour")
                print(time_array[1] + " = minties")

                final_time = 1
                if time_array[0] != "0" and time_array[0] != "00":
                    select.select_by_value('hour')
                    final_time = time_array[0]
                elif time_array[1] != "0" and time_array[1] != "00":
                    select.select_by_value('minute')
                    final_time = time_array[1]
                else:
                    select.select_by_value('minute')

                title_field.clear()
                title_field.send_keys(lesson.title)
                time.sleep(1)
                media_field.clear()
                media_field.send_keys(lesson.code)
                time.sleep(1)
                date_field.clear()
                date_field.send_keys(final_time)
                time.sleep(2)
                self.driver.execute_script("arguments[0].click();",
                                           publish_button)
                self.driver.implicitly_wait(10)
                time.sleep(2)
                self.driver.find_element_by_xpath(
                    '//*[@id="wpbody-content"]/div[3]/a').click()
                self.driver.implicitly_wait(10)
                #self.db.update_lessons_resume(playlist_id=course.playlistId, index=str(temp_counter))

            # suffucly addded the playlsit
        # self.db.add_finished_playlist(course.playlistId)
        # self.db.delete_course_from_resume(playlist_id=course.playlistId)

    def formatLessonName(self, count):
        countStr = ""
        if count <= 9:
            countStr = "00" + str(count)
        elif count <= 99 and count >= 10:
            countStr = "0" + str(count)
        else:
            countStr = str(count)
        return countStr

    def getYoutubeEmbedCode(self, videoId):
        embedder = Embedder()
        code = embedder("https://www.youtube.com/watch?v=" + str(videoId),
                        width=video_width,
                        height=video_height)
        return code