Get YouTube video id in Python - python

I am working on a project for my MSc class and I have users that have declared songs they like in youtube, creating a large data-set. I have found a way to get the video id of a song by searching with you-tube-api, but it seems that the key they provide has an access limit. The code is bellow:
api_key = 'xxxxxxxx'
yt = YouTubeDataAPI(api_key)
searches = yt.search(q=song,
max_results=1)
yt.get_video_metadata_gen(searches[0]['video_id'])
Do you know any alternative way to do it without a limit?
I guess there should be an option to provide keys without limitations for research purposes or to universities, but I am not able to find sth like this.

Related

Web Scraping with an Incrementing ID and Login

I’m trying to create a web scraper in python for a website called Canvas LMS to find a course link. The course link is formatted like this: [schoolname].instructure.com/courses/[id] I need to figure out how to have a bot log in with my api key and check all IDs from 1 to 10,000 for courses that does not contain the phrase “Unauthorized” or “Page Not Found” in the title so I can check them manually. However, I cannot figure out how to do any of this, as there is no guide (to my knowledge) that says how to do any of this. Tips would be appreciated.
I was working with a similar problem a few weeks back and found a solution.
Firstly, there is extensive Canvas LMS documentation available here
Secondly, find the relevant tags which can be identified and then copied to a download set. I forked a repository here and ran as a Docker container on my local machine.
I would also recommend viewing the data in a Jupyter Notebook. As a starter (and I do not advocate nested 'for loops' but you get the idea) try these:
from canvasapi import Canvas
canvas = Canvas("your-course-url", "api-token")
#Get courses and print
courses = canvas.get_courses()
for course in courses:
print(f"{course}")
#Get modules and print
modules = course.get_modules()
for module in modules:
print(f"{module}")
#Get modules items and print
module_items = course.get_module_items()
for item in module_items:
print(f"{item}")
Good luck

How to use Youtube API v3 to filter out videos by category?

I'm trying to build a Youtube query in python that filters out, for instance, category 39 - Horror.
I'm looking to something similar to this:
searchResults = YouTube.Search.list('id,snippet', {
maxResults: "10",
q: "The ring",
type: "video",
videoCategoryId: "-10"
});
Or any kind of workaround.
I even contemplated searching without filter and then try to exclude the ones from that category, but the information provided by the query does not include the category of the video. And it's pretty ugly as I really want 10 results, and I may need to make more queries in order to fill the list.
All I've found googling is how to get videos from the specified category.
Can you help me on how to achieve this?
By using the YouTube Data API, you can check here the existing videoCategories.
In this demo available in the Google API Explorer, you can retrieve the videoCategories.
Once you have selected the videoCategoryId, use the following search request:
https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=10&q=The+ring&regionCode=US&type=video&videoCategoryId=1&fields=items(id(channelId%2CvideoId)%2Csnippet(channelId%2CchannelTitle%2Cdescription%2Ctitle))%2CnextPageToken%2CpageInfo%2CprevPageToken%2CregionCode&key={YOUR_API_KEY}
Here, I'm using the search.list request for get videos "from the US region", with videoCategoryId 1 = (Film & Animation) and retrieving the title of the YouTube video, its videoId, the channel title and channel_id from the channel who uploaded the video.
Here is the demo.
It seems that the videoCategoryId is not supported in all regions (this might be the reason I couldn't retrieve videos with videoCategoryId=39 "which is the Horror category in the US").
For more information, read this answer.

YouTube API v3 - get video title by url in Python

I'd like to know how can I simply get the title or other information about a video using Youtube API, in case the only thing I know is the url of the video (so basically the video ID).
What other info can I get about a video? eg: Length, Category, Uploader name, Country of origin, ... ???
Can somebody provide me a usable code snippet and the library to use for this data collecting?
Thanks for the help in advance.
There are plenty of examples and documentation about what you can get using the YouTube API's Python bindings.
Here are Python code samples as provided by YouTube:
https://developers.google.com/youtube/v3/code_samples/python#create_and_manage_youtube_video_caption_tracks
And the code samples can also be downloaded from their GitHub repository:
https://github.com/youtube/api-samples/tree/master/python
Try below code:
payload = {'id': search_result["id"]["videoId"], 'part': 'contentDetails,statistics,snippet', 'key': DEVELOPER_KEY}
l = requests.Session().get('https://www.googleapis.com/youtube/v3/videos', params=payload)
resp_dict = json.loads(l.content)
print "Title: ",resp_dict['items'][0]['snippet']['title']
Try using this API: https://pypi.org/project/python-youtube/
To grab the title, you can do something like this:
from pyyoutube import Api
playlistVideoItems = api.get_playlist_items(playlist_id='PLOU2XLYxmsIKpaV8h0AGE05so0fAwwfTw').items
print(playlistVideoItems[0].snippet.title)
Note that the above is somewhat untested code. I copied the relevant bits and pieces from what I currently have, but I did not test this exact set lines of code. And of course, I'm not actually using that playlist ID for my purposes.
In regards to what other types of information can be gathered, I would recommend reading the documentation or running in a debugger. As of this writing, I am trying to figure out how to obtain the video uploader name, but I don't really need this data although it would be nice to have. I will update this answer if I figure it out.

Gracenote/Cddb official genre list

I am working with mp3s and metadata. I have used Python to edit and add metadata to each file, but I cannot seem to get genres to work. I have used Pygn, a gracenote module that seems to be doing its job well. The problem is with the gracenote data itself. When I request data for, say daft punk, pygn.search(clientID=clientid, userID=userid, artist="Daft Punk", album="Random Access Memories")
I am returned a JSON as expected. The problem is that the genre key gives me a text anwser, and a number such as 45720. I was wondering if maybe there is some Dewey decimal-like system in place here. Do you know what system this is? Do you have an official gracenote genre list?
Gracenote does not provide genre list. But you can get the top level genre list of 25 genres through Rhythm API. Check out the fieldvalues API call on Gracenote Developer Program website.

Get spotify currently playing track

EDIT : Let's try to clarify all this.
I'm writing a python script, and I want it to tell me the song that Spotify is currently playing.
I've tried looking for libraries that could help me but didn't find any that are still maintained and working.
I've also looked through Spotify's web API, but it does not provide any way to get that information.
The only potential solution I found would be to grab the title of my Spotify (desktop app) window. But I didn't manage to do that so far.
So basically, what I'm asking is whether anyone knows :
How to apply the method I'm already trying to use (get the window's title from a program), either in pure python or using an intermediary shell script.
OR
Any other way to extract that information from Spotify's desktop app or web client.
Original post :
I'm fiddling with the idea of a python status bar for a linux environment, nothing fancy, just a script tailored to my own usage. What I'm trying to do right now is to display the currently playing track from spotify (namely, the artist and title).
There does not seem to be anything like that in their official web API. I haven't found any third party library that would do that either. Most libraries I found are either deprecated since spotify released their current API, or they are based on said API which does not do what I want.
I've also read a bunch of similar question in here, most of which had no answers, or a deprecated solution.
I thought about grabbing the window title, since it does diplay the information I need. But not only does that seem really convoluted, I also have difficulties making this happen. I was trying to get it by running a combination of the linux commands xdotools and xprop inside my script.
It's worth mentionning that since I'm already using the psutil lib for other informations, I already have access to spotify's PID.
Any idea how I could do that ?
And in case my method was the only one you can think of, any idea how to actually make it work ?
Your help will be appreciated.
The Spotify client on Linux implements a D-Bus interface called MPRIS - Media Player Remote Interfacing Specification.
http://specifications.freedesktop.org/mpris-spec/latest/index.html
You could access the title (and other metadata) from python like this:
import dbus
session_bus = dbus.SessionBus()
spotify_bus = session_bus.get_object("org.mpris.MediaPlayer2.spotify",
"/org/mpris/MediaPlayer2")
spotify_properties = dbus.Interface(spotify_bus,
"org.freedesktop.DBus.Properties")
metadata = spotify_properties.Get("org.mpris.MediaPlayer2.Player", "Metadata")
# The property Metadata behaves like a python dict
for key, value in metadata.items():
print(key, value)
# To just print the title
print(metadata['xesam:title'])
For windows:
The library can be found at github: https://github.com/XanderMJ/spotilib. Keep in mind that this is still work in progress.
Just copy the file and place it in your Python/Lib directory.
import spotilib
spotilib.artist() #returns the artist of the current playing song
spotilib.song() #returns the song title of the current playing song
spotilib.artist() returns only the first artist. I started working on an other library spotimeta.py to solve this issue. However, this is not working at 100% yet.
import spotimeta
spotimeta.artists() #returns a list of all the collaborating artists of the track
If an error occurs, spotimeta.artists() will return only the first artist (found with spotilib.artist())

Categories

Resources