Exemplo n.º 1
0
def get_video_data(video_id: str) -> str:
    """Returns the video data as a dictionary with 'title', 'description', and published 'date'."""
    YT_API_KEY = os.getenv("YT_API_KEY")
    api = pyyoutube.Api(api_key=YT_API_KEY)
    data: dict = api.get_video_by_id(video_id=video_id,
                                     parts="snippet",
                                     return_json=True)
    snippet: dict = data["items"][0]["snippet"]
    return {
        "date": snippet["publishedAt"][:-1],
        "title": snippet["title"],
        "description": snippet["description"].split("\n", 1)[0],
    }
Exemplo n.º 2
0
 def testGetProfile(self):
     with open(f'{self.base_path}modeldata/user_profile.json',
               'rb') as json_file:
         user_info = json_file.read()
     responses.add(responses.GET,
                   self.api.USER_INFO_URL,
                   body=user_info,
                   status=200)
     profile = self.api.get_profile()
     self.assertEqual(profile.given_name, 'kun')
     api = pyyoutube.Api(access_token='access_token')
     profile_json = api.get_profile(return_json=True)
     self.assertEqual(profile_json['given_name'], 'kun')
Exemplo n.º 3
0
    def get_video_info():
        api = pyyoutube.Api(api_key=API_KEY)

        playlist_item_res = api.get_playlist_items(
            playlist_id="PLb_KV6iCoxINp2vwdz9ZHJO7aPF_WrEat", count=1
        )

        videos = []
        for item in playlist_item_res.items:
            video_id = item.contentDetails.videoId
            video_res = api.get_video_by_id(video_id=video_id)
            videos = video_res.items
        return videos
def main():
    api = pyyoutube.Api(api_key=os.environ['YOUTUBE_API_KEY'])
    playlist_items_by_playlist = api.get_playlist_items(
        playlist_id="FLIoOgOl_PcQzRHhS_rSCP6Q",
        # Push it to the limit!
        count=None)
    with open('exported_youtube_playlist.html', 'w') as file:
        file.write(f"<html><body>")
        for video in playlist_items_by_playlist.items:
            file.write(
                f"<a href=\"https://www.youtube.com/watch?v={video.snippet.resourceId.videoId}\" target=\"_blank\">https://www.youtube.com/watch?v={video.snippet.resourceId.videoId}</a><br>\n"
            )
        file.write(f"</body></html>")
    exit()
Exemplo n.º 5
0
    def testOAuth(self) -> None:
        url, statue = self.api.get_authorization_url()
        self.assertEqual(statue, "PyYouTube")

        redirect_response = (
            "https://localhost/?state=PyYouTube&code=code"
            "&scope=profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile#"
        )

        with self.assertRaises(pyyoutube.PyYouTubeException):
            self.api.refresh_token()

        with self.assertRaises(pyyoutube.PyYouTubeException):
            api = pyyoutube.Api(client_id="xx", client_secret="xx")
            api.generate_access_token(authorization_response="str")

        with responses.RequestsMock() as m:
            m.add("POST",
                  self.api.EXCHANGE_ACCESS_TOKEN_URL,
                  json=self.ACCESS_TOKEN_INFO)
            token = self.api.generate_access_token(
                authorization_response=redirect_response, )
            self.assertEqual(token.access_token, "access_token")
            token_origin = self.api.generate_access_token(
                authorization_response=redirect_response, return_json=True)
            self.assertEqual(token_origin["access_token"], "access_token")

            refresh_token = self.api.refresh_token()
            self.assertEqual(refresh_token.access_token, "access_token")
            refresh_token_origin = self.api.refresh_token(return_json=True)
            self.assertEqual(refresh_token_origin["refresh_token"],
                             "refresh_token")

            api = pyyoutube.Api(client_id="xx", client_secret="xx")
            refresh_token = api.refresh_token(refresh_token="refresh_token")
            self.assertEqual(refresh_token.refresh_token, "refresh_token")
Exemplo n.º 6
0
def get_videos(channel_name):
    api = pyyoutube.Api(api_key=API_KEY)
    channel = api.get_channel_info(channel_name=channel_name)

    playlist_id = channel[0].contentDetails.relatedPlaylists.uploads

    playlist_items, _ = api.get_playlist_item(playlist_id=playlist_id,
                                              count=10,
                                              limit=6)

    videos = []
    for item in playlist_items:
        video_id = item.contentDetails.videoId
        video_info = api.get_video_by_id(video_id=video_id)
        videos.append(video_info[0])
    return videos
def get_all_videos_id_by_channel(channel_id, limit=50, count=50):
    api = pyyoutube.Api(api_key=API_KEY)
    res = api.search(channel_id=channel_id, limit=limit, count=count)
    nextPage = res.nextPageToken
    videos_id = []

    while nextPage:
        nextPage = res.nextPageToken
        for item in res.items:
            if item.id.videoId:
                videos_id.append(item.id.videoId)

        res = api.search(
            channel_id=channel_id,
            limit=limit,
            count=count,
            page_token=res.nextPageToken,
        )

    return videos_id
Exemplo n.º 8
0
def index(request):
    api = pyyoutube.Api(api_key='AIzaSyC1MdrF1pr0_GSXutgMdqDNsPhr8DujyeI')
    if request.method == "GET":
        return HttpResponse('API loaded in api/')
    if request.method == "POST":
        json_data = json.loads(request.body.decode('utf-8'))
        score = 0

        comment_list = api.get_comment_threads(video_id=json_data.link,
                                               limit=100,
                                               count=None)
        list_comment = []
        for i in comment_list.items:
            list_comment.append(i.snippet.topLevelComment.snippet.textDisplay)

        score += loader(list_comment)
        result = ""
        if score > 0:
            result = "Positive"
        else:
            result = "Negative"
        send_data = {"sent_id": json_data.link, "prediction": result}
        return JsonResponse(send_data)
Exemplo n.º 9
0
import pyyoutube
import json

api = pyyoutube.Api(api_key="AIzaSyC1MdrF1pr0_GSXutgMdqDNsPhr8DujyeI")
# print(api.get_comment_threads(video_id="rvIZjhj8M50",return_json=True))
comment_list = api.get_comment_threads(video_id="rvIZjhj8M50",
                                       return_json=False,
                                       limit=100,
                                       count=None)
for i in comment_list.items:
    print(i.snippet.topLevelComment.snippet.textDisplay)
Exemplo n.º 10
0
 def setUp(self) -> None:
     self.api = pyyoutube.Api(api_key="api key")
     self.api_with_token = pyyoutube.Api(access_token="token")
Exemplo n.º 11
0
def main():
    parser = generate_parser()
    args = parser.parse_args()
    if args.path is not None and args.folders is None:
        parser.error("When using --path, you should specify a folder structure"
                     " using --folders. Use -h for help")
    elif args.folders is not None and len(args.playlists) != 1:
        parser.error("When using --folders, multiple playlists aren't allowed."
                     " Use -h for help")

    api = yt.Api(api_key=cfg.YT_API_KEY)
    playlists = api.get_playlist_by_id(playlist_id=args.playlists,
                                       parts=["snippet"]).items

    paths = map(lambda playlist: u.get_base_path(args, playlist), playlists)
    for playlist, path in zip(playlists, paths):
        os.makedirs(path, exist_ok=args.force)
        with open(os.path.join(path, cfg.INDEX), "w") as f:
            frontmatter = cfg.FRONTMATTER["series"].format(
                date=playlist.snippet.publishedAt,
                description=args.description
                or playlist.snippet.description.split("\n")[0],
                title=args.title or playlist.snippet.title,
                playlist_id=args.playlists,
            )
            f.write(frontmatter)

        path_chapter = os.path.join(path, cfg.DIR_CHAPTER)
        os.makedirs(path_chapter, exist_ok=args.force)
        with open(os.path.join(path_chapter, cfg.INDEX), "w") as f:
            f.write(cfg.FRONTMATTER["chapter"])

        playlist_items = api.get_playlist_items(playlist_id=args.playlists,
                                                parts=["snippet"],
                                                count=None).items

        snippets = [playlist.snippet for playlist in playlist_items]
        snippets_grouped = groupby(enumerate(snippets),
                                   lambda ix: ix[0] // cfg.YT_MAX_RESULTS)

        videos = [
            api.get_video_by_id(
                video_id=[s.resourceId.videoId for _, s in snippets]).items
            for _, snippets in snippets_grouped
        ]
        videos = chain.from_iterable(videos)

        snippet_frontmatter = [(
            snippet,
            cfg.FRONTMATTER["video"].format(
                date=snippet.publishedAt,
                title=snippet.title,
                description=snippet.description,
                weight=snippet.position,
                video_id=snippet.resourceId.videoId,
                video_duration=video.contentDetails.get_video_seconds_duration(
                ),
            ),
        ) for snippet, video in zip(snippets, videos)]
        for snippet, frontmatter in snippet_frontmatter:
            path = os.path.join(
                path_chapter,
                "{}_{}.md".format(snippet.position,
                                  u.sanitize_title(snippet.title)),
            )
            with open(
                    path,
                    "w",
            ) as f:
                f.write(frontmatter)
 def setUp(self) -> None:
     self.base_path = 'testdata/'
     self.api = pyyoutube.Api(api_key='test')
Exemplo n.º 13
0
def main():
    args = parse_command_line_arguments()

    api = pyyoutube.Api(api_key=config.YT_API_KEY)
    playlists = api.get_playlist_by_id(
        playlist_id=args.playlist, parts=["snippet"]
    ).items

    paths = map(lambda playlist: utils.get_base_path(args, playlist), playlists)
    for playlist, path in zip(playlists, paths):
        os.makedirs(path, exist_ok=args.force)
        with open(os.path.join(path, config.INDEX), "w") as f:
            frontmatter = config.FRONTMATTER["series"].format(
                date=playlist.snippet.publishedAt,
                description=args.description
                or playlist.snippet.description.split("\n")[0],
                title=args.title or playlist.snippet.title,
                playlist_id=args.playlist,
            )
            f.write(frontmatter)

        path_chapter = os.path.join(path, config.DIR_CHAPTER)
        os.makedirs(path_chapter, exist_ok=args.force)
        with open(os.path.join(path_chapter, config.INDEX), "w") as f:
            f.write(config.FRONTMATTER["chapter"])

        playlist_items = api.get_playlist_items(
            playlist_id=args.playlist, parts=["snippet"], count=None
        ).items

        snippets = [playlist.snippet for playlist in playlist_items]
        snippets_grouped = groupby(
            enumerate(snippets), lambda ix: ix[0] // config.YT_MAX_RESULTS
        )

        videos = [
            api.get_video_by_id(
                video_id=[s.resourceId.videoId for _, s in snippets]
            ).items
            for _, snippets in snippets_grouped
        ]
        videos = chain.from_iterable(videos)

        snippet_frontmatter = []
        for snippet, video in zip(snippets, videos):
            index = snippet.position + 1
            frontmatter = config.FRONTMATTER["video"].format(
                    date=snippet.publishedAt,
                    title=snippet.title,
                    description=snippet.description.split('\n', 1)[0],
                    # Add 1 so the weight starts at 1, not 0.
                    weight=index,
                    video_id=snippet.resourceId.videoId,
                    video_duration=video.contentDetails.get_video_seconds_duration(),
                )
            path = os.path.join(
                path_chapter,
                "{}_{}.md".format(index, utils.sanitize_title(snippet.title)),
            )
            with open(path, "w",) as f:
                f.write(frontmatter)
Exemplo n.º 14
0
from IndianMedia.constants import Channels, MongoConsts, Creds
from IndianMedia.pymongoconn import DBConnection

try:
    __file__
except:
    __file__ = os.path.abspath(
        os.path.join(".", "..", "Analytics", "IndianMedia", Creds.KEY_FILE))

connection = DBConnection()

f = os.path.abspath(os.path.join(os.path.dirname(__file__), Creds.KEY_FILE))
key = open(f, "r").read().strip("\n")

api = pyyoutube.Api(api_key=key)

#p = pl.items[0]


def GetChannelVideoInfo(channelId):
    pl = api.get_playlists(channel_id=channelId, count=200)

    #all_videos = []
    for j, i in enumerate(pl.items):
        logging.info(f"-- Loading - {j} Playlist")

        pljs = json.loads(i.to_json())

        videos = api.get_playlist_items(playlist_id=i.id, count=200)
Exemplo n.º 15
0
import pyyoutube  #run cmd as admin while instaling 'pip install --upgrade python-youtube'
from datetime import datetime

d = datetime.now()

print(d.strftime('%Y-%m-%dT{}+{}').format("00:00:00", "00:00"))
api = pyyoutube.Api(api_key='AIzaSyBXmobEX1fX31VQk55p6YxJ5qQ5Q7fHYDc')

res = api.get_activities_by_channel(channel_id='UCc4Rz_T9Sb1w5rqqo9pL1Og',
                                    return_json=True,
                                    after=d.strftime('%Y-%m-%dT{}+{}').format(
                                        "00:00:00", "00:00"))
if len(res['items']) != 0:
    print("Last video:::https://www.youtube.com/watch?v={}".format(
        res['items'][0]['contentDetails']['upload']['videoId']))
else:
    print("No video")  #
Exemplo n.º 16
0
 def setUp(self) -> None:
     self.api_with_token = pyyoutube.Api(access_token="access token")
Exemplo n.º 17
0
 def setUp(self) -> None:
     self.api = pyyoutube.Api(api_key="api key")
Exemplo n.º 18
0
 def setUp(self) -> None:
     self.api = pyyoutube.Api(client_id="xx", client_secret="xx")
Exemplo n.º 19
0
def getNewEpisodesInPlaylist(playlistId, above, existingVideoIDs):
    print("==> Fetching youtube video metadata for playlist " + playlistId)
    api = pyyoutube.Api(api_key=API_KEY)
    list = api.get_playlist_items(playlist_id=playlistId, count=None)
    episodes = []

    YT_API_BASE_URL = "https://www.googleapis.com/youtube/v3/videos"
    YT_API_QS = "?part=snippet%2CliveStreamingDetails&key=" + API_KEY

    for item in list.items:
        print("----> Processing metadata for video " +
              item.snippet.resourceId.videoId)
        videoId = item.snippet.resourceId.videoId
        response = requests.get(YT_API_BASE_URL + YT_API_QS + "&id=" + videoId)
        videoList = json.loads(response.text)
        videoInfo = videoList["items"][0]["snippet"]
        liveInfo = {}
        try:
            liveInfo = videoList["items"][0]["liveStreamingDetails"]
        except:
            liveInfo["scheduledStartTime"] = None

        episodeDate = fuzzyDate(liveInfo["scheduledStartTime"]) or fuzzyDate(
            videoInfo["publishedAt"]) or datetime.time()

        video = {
            "videoId": videoId,
            "title": videoInfo["title"],
            "description": videoInfo["description"],
            "imageUrl": videoInfo["thumbnails"]["medium"]["url"],
            "date": episodeDate,
        }
        episodes.append(video)

    print("==> Comparing Youtube Videos with Episode List")
    episodes = sorted(episodes, key=itemgetter('date'), reverse=False)

    updateEpisodes = []
    new = above
    for episode in episodes:
        if getEpisodeNumberFromId(episode["videoId"], existingEpisodes) == 0:
            new += 1
            episode["episode"] = str(new)
        else:
            episode["episode"] = str(
                getEpisodeNumberFromId(episode["videoId"], existingEpisodes))

        print("---> episode: " + episode["episode"] + " - " +
              episode["title"] + ":")

        if (not episode["date"]) or (episode["episode"] == 0):
            video["draft"] = "true"

        if int(episode["episode"]) in range(
                above - 4, above + 1
        ):  # Do the last 5 episodes all the time to make sure we update them
            updateEpisodes.append(episode)
            print("----> Updating as #" + episode["episode"] + "...")
        elif int(episode["episode"]) > above:  # create new episodes
            updateEpisodes.append(episode)
            print("----> Saving as #" + episode["episode"] + "...")
        elif episode["videoId"] not in existingVideoIDs:
            updateEpisodes.append(episode)
            print("----> Adding Youtube Video to #" + episode["episode"] +
                  "...")
        else:
            print("----> Skipping episode #" + episode["episode"] +
                  " (already have it)...")
    return updateEpisodes
Exemplo n.º 20
0
import os
import pyyoutube
import time
import datetime
import requests
import json
import random

api = pyyoutube.Api(
    api_key=os.getenv('YOUTUBE_API_TOKEN'))  # initializes a new api object
last_check = datetime.datetime.timestamp(datetime.datetime.now() -
                                         datetime.timedelta(hours=1))
print('YouTube mailman starts')

# gets the list of channels to check
channelIDs = json.loads(os.getenv('CHANNELS_LIST'))
for channel in channelIDs['channelIDs']:
    try:
        results = api.search(parts='snippet', channel_id=channel, order='date')
    except pyyoutube.PyYouTubeException:
        print('An error occurred while getting videos')
        break

    # get latest video and compare
    video_upload_time = time.mktime(
        datetime.datetime.strptime(results.items[0].snippet.publishedAt,
                                   '%Y-%m-%dT%H:%M:%SZ').timetuple())

    if video_upload_time > last_check:  # new video since last check
        # get a pattern for message
        patterns = json.loads(os.getenv('MESSAGE_PATTERNS'))
Exemplo n.º 21
0
    async def play(self, ctx, *, query: str):
        """Play a song or playlist by providing the name/artist or through an URL."""

        # If given, cut the audio filter away from the query into it's own.
        audioFilter = None
        if query.endswith(']') and '[' in query:
            audioFilter = re.search(r'\[.*?\]', query)[0][1:-1]
            query = query.replace(f' [{audioFilter}]', '')

        # Start typing incidicator.
        await ctx.channel.trigger_typing()

        # First, let's handle Spotify links.
        if 'spotify.com' in query and ('playlist' in query
                                       or 'track' in query):
            spotifyCredentials = SpotifyClientCredentials(
                client_id=self.bot.config.spotify_client_id,
                client_secret=self.bot.config.spotify_client_secret)
            spotify = spotipy.Spotify(
                client_credentials_manager=spotifyCredentials)

            # It's a playlist link...
            if 'playlist' in query:

                # Now, let's get all the results from Spotify properly into an array...
                playlist = []
                result = spotify.playlist_items(query)
                while result:
                    playlist.extend(result['items'])
                    result = spotify.next(result)

                # Now let's add them to the local playlist...
                for track in playlist:
                    entry = self.spotify_to_entry(track['track'], audioFilter)
                    self.bot.memory['music'][ctx.guild.id]['playlist'].append(
                        entry)

                # Inform we're done adding a playlist.
                message = await language.get(self, ctx,
                                             'music.queued_playlist')
                await ctx.send(message.format(len(playlist)))

            # It's a track link...
            elif 'track' in query:
                entry = self.spotify_to_entry(spotify.track(query),
                                              audioFilter)
                self.bot.memory['music'][ctx.guild.id]['playlist'].append(
                    entry)
                await ctx.send(
                    (await
                     language.get(self, ctx,
                                  'music.queued')).format(entry['title']))

        # Secondly, let's handle YouTube links.
        elif 'youtube.com' in query or 'youtu.be' in query:

            # It's a playlist link...
            if 'playlist?list=' in query:

                # Extract the ID from the url.
                url = requests.utils.urlparse(query).query
                params = dict(x.split('=') for x in url.split('&'))

                # Now get the videos information through YouTube API.
                api = pyyoutube.Api(api_key=self.bot.config.youtube_api_key)
                result = api.get_playlist_items(
                    playlist_id=params['list'],
                    parts="id,snippet",
                    count=None,
                )

                # Loop to add the songs.
                for item in result.items:
                    self.bot.memory['music'][ctx.guild.id]['playlist'].append({
                        'query':
                        f'ytsearch:{item.snippet.title}',
                        'title':
                        item.snippet.title,
                        'duration':
                        0,
                        'audiofilter':
                        audioFilter
                    })

                # Inform succes...
                message = await language.get(self, ctx,
                                             'music.queued_playlist')
                await ctx.send(message.format(len(result.items)))

            # It's a track link...
            else:
                entry = self.get_from_youtube(query, audioFilter)
                self.bot.memory['music'][ctx.guild.id]['playlist'].append(
                    entry)
                await ctx.send(
                    (await
                     language.get(self, ctx,
                                  'music.queued')).format(entry['title']))

        # If still a hyperlink, then it's not supported.
        elif query.startswith('http://') or query.startswith('https://'):
            return await ctx.send(await language.get(self, ctx,
                                                     'music.not_supported'))

        # Finally, search for the song on YouTube...
        else:
            entry = self.get_from_youtube(f'ytsearch:{query}', audioFilter)
            self.bot.memory['music'][ctx.guild.id]['playlist'].append(entry)
            await ctx.send(
                (await language.get(self, ctx,
                                    'music.queued')).format(entry['title']))

        # Now let's see if we need to start playing directly, as in, nothing is playing...
        if not self.bot.memory['music'][ctx.guild.id]['playing']:
            self.start_play(
                ctx, self.bot.memory['music'][ctx.guild.id]['playingIndex'])
Exemplo n.º 22
0
 def setUp(self) -> None:
     self.api = pyyoutube.Api(access_token="Authorize token")
Exemplo n.º 23
0
 def testInitApi(self) -> None:
     with self.assertRaises(pyyoutube.PyYouTubeException):
         pyyoutube.Api()
Exemplo n.º 24
0
from pprint import pprint
import pyyoutube

from yourtube.config import config

# Initialize the api once during the loading phase of the program
# Just use it by importing `api` anywhere in this program
# There's an excellent tutorial on how to do this in the pyyoutube Docs:
# https://python-youtube.readthedocs.io/en/latest/getting_started.html
api = pyyoutube.Api(api_key=config["youtube"]["api_key"])


def get_videos_of_subscription(name):
    """Return all videos of a specific subscription."""
    # Get general information of this channel
    channels = api.get_channel_info(channel_name=name)
    channel = channels.items[0]

    # Looks like one has to look at the related playlists to get to the channel's uploads
    playlist_id = channel.contentDetails.relatedPlaylists.uploads

    all_uploads = []
    result = None
    while result is None or len(result.items) > 0:
        print("Playlist by id")
        result = api.get_playlist_by_id(playlist_id=playlist_id)
        pprint(playlist_id)
        pprint(result)
        pprint(result.items)
        all_uploads += result.items
Exemplo n.º 25
0
 def setUp(self):
     self.base_path = 'testdata/'
     self.api = pyyoutube.Api(client_id='xx',
                              client_secret='xx',
                              api_key='xx')
Exemplo n.º 26
0
import pyyoutube
from time import *
#webscraping libary

from selenium import webdriver

# variables
api = pyyoutube.Api(api_key='AIzaSyC52css0A3UeTZme1tHj0BRWT1JET-eUuM')
api.get_authorization_url()
id = 'UCC7fJamx8-ycCtcCHyjWDnA'
url = 'https://www.youtube.com/'
driverPath = '/Users/kavinselvaraj/Own_Projects/automation/youtube-alert/chromedriver'


def WebScraping():
    channel_by_id = api.get_channel_info(channel_id=id)
    data = channel_by_id.items[0].to_dict()
    print(data)
    sleep(3)


WebScraping()