This article is suitable for developers who want to write Youtube interactive programs, and can quickly understand the usage of Youtube Data API interface Youtube has a large number of videos. How to quickly retrieve the desired videos is very important It is found that google has provided a Youtube developer website with the homepage as follows: https://developers.google.com/youtube/v3 The developer website provides a web access interface, which is called Youtube Data API According to the official introduction, API users can be divided into two categories:
- Uploader
Video uploaders can upload videos through API, manage playlists and subscriptions, update channel settings, etc - Watcher&Searcher
Video viewers can use API to complete the video retrieval function based on keywords, topics, locations, release dates, etc. in addition, it also supports the retrieval function of playlists and channels
This paper mainly introduces the Searcher function used to randomly retrieve youtube videos through API and extract video related information
1. Preparation stage
Before using Youtube Data API, users need to access Google API console through Google account and register to obtain API key
rails c https://console.developers.google.com/ Create project
After the project is created successfully, enter the API console and select the newly registered project to open the API https://console.developers.google.com/apis/enabled
Search youtube in the API list, find YouTube Data API v3 and start it
2. Video retrieval
youtube video has a unique index in the server, namely vid Video retrieval is to retrieve a valid vid
random_string = 'LXU' # Random string api_key = 'Registered acquired API_KEY' api_uri = 'https://www.googleapis.com/youtube/v3/ vid_random_search_url = f'{api_uri}search?key={api_key}&maxResults=10&part=id&type=video&q={random_string}' # maxResults=10 indicates that the maximum number of search results is 10
Through curl, you can retrieve the video's vid
3. Retrieve video information
The video information provided by API mainly includes the following contents:
snippet
Title, description, published at, channeltitle, channelid, categoryid
contentDetails
Duration (video duration), dimension (2D / 3D), definition (hd/sd)
statistics
Viewcount, likecount, dislikecount, favoritecount, commentcount
//The retrieval method is: vid = 'ZQrOIZgGbgQ' video_info_search_url = f'{api_uri}videos?id={vid}&part=snippet,contentDetails,statistics,topicDetails&key={api_key}'
4. Retrieve the category name corresponding to CategoryId
Retrieve the subject name / category name according to the video subject / category CategoryId
topicDetails
relevantTopicIds, topicCategories
//Retrieval method categoryId = '10' video_category_search_url = f'{api_uri}videoCategories?part=snippet&id={categoryId}&key={api_key}'
5. Results
5.1. URI
https://www.googleapis.com/youtube/v3/search?key=***API_KEY***&maxResults=2&part=id&type=video&q=LXU https://www.googleapis.com/youtube/v3/videos?id=ZQrOIZgGbgQ&part=snippet,contentDetails,statistics,topicDetails&key=***API_KEY*** https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&id=28&key=***API_KEY*** https://www.googleapis.com/youtube/v3/videos?id=y0LRXBQDpRI&part=snippet,contentDetails,statistics,topicDetails&key=***API_KEY*** https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&id=10&key=***API_KEY***
5.2 search results
[{ 'vid': 'ZQrOIZgGbgQ', 'randomString': 'LXU', 'snippet': { 'title': 'LXU casa prefabbricata', 'description': 'Il portale per consultare i prezzi case prefabbricate, case prefabbricate in legno, case prefabbricate in cemento armato.\nI progetti, i prezzi, i cantieri e le news sulle case prefabbricate', 'publishedAt': '2017-02-28T06:10:57Z', 'channelTitle': 'Prezzi Case Prefabbricate', 'channelId': 'UClZBymX5oADLQ_5SYGEekcg', 'categoryId': '28', 'categoryName': 'Science & Technology' }, 'contentDetails': { 'duration': 64, 'dimension': '2d', 'definition': 'hd' }, 'statistics': { 'viewCount': '52553', 'likeCount': '144', 'dislikeCount': '19', 'favoriteCount': '0' }, 'topicDetails': { 'relevantTopicIds': ['/m/019_rr', '/m/03glg', '/m/019_rr', '/m/03glg'], 'topicCategories': ['https://en.wikipedia.org/wiki/Hobby', 'https://en.wikipedia.org/wiki/Lifestyle_(sociology)'] } }, { 'vid': 'y0LRXBQDpRI', 'randomString': 'LXU', 'snippet': { 'title': 'LxU - #hot16challenge2 feat. FakeFreshDumbledore [Prod. Call Me G]', 'description': 'Souncloud: https://soundcloud.com/user-370669292/hot16bars2-feat-fakefreshdumbledore\n\n!!!HINWEIS!!! \n\nDieses grandiose Produkt überragender Kompetenz wurde innerhalb von 72h aufgenommen, gefilmt und geschnitten und das sieht/hört man auch.\n\nWir übernehmen keine Verantwortung für eventuelle Verdummung und/oder daraus resultierende Abstumpfung durch den Konsum dieses Meisterwerks!\n\n\n\nHintergrund #hot16challenge2 :\n\nHot16challenge ist eine Initiative für Rapper und Sänger. Die Idee der Aktion ist, einen Vers mit 16 Takten aufzunehmen, ihn zu veröffentlichen und dann vier weitere Künstler zu nominieren, die 72 Stunden Zeit haben, um zu antworten. Hauptziel von Hot16challenge ist es, so viele Künstler wie möglich zu aktivieren und zuerst die Hip-Hop-Industrie und dann hoffentlich die gesamte Musikindustrie zu mobilisieren, um COVID-19 durch Musik zu bekämpfen. Hot16challenge-Teilnehmer und ihre Fans können auf https://www.gofundme.com/f/hot16chall... den Kampf gegen den Coronavirus spenden.\n\nVor sechs Jahren wurde #hot16challenge als Künstler-zu-Künstler-Bewegung viral. Jetzt starten wir eine Fortsetzung, aber mit einer gemeinnützigen Wendung und weltweit. \n\n\n\n\n\nNominierungen:\n\nLyrisLost:\nInstagram: https://www.instagram.com/lyrislost/\nYoutube: https://www.youtube.com/channel/UC0OZQYa84UxpPCnKN9HO51w\n\n\nLUV: SOLU :\nInstagram: https://www.instagram.com/luv_solu/\nYoutube: https://www.youtube.com/channel/UCOSGrBIYQbANrY-gsxCGkGA\n\n\nEminem:\nInstagram: https://www.instagram.com/eminem/\nYoutube: https://www.youtube.com/user/EminemMusic\n\n\nHelene Fischer:\nInstagram: https://www.instagram.com/helenefischer/\nYoutube: https://www.youtube.com/user/helenefischer\n\n\n\n\n\nLEUTE HINTER DIESEM MEISTERWERK :\n\n\n\nBeat: https://www.youtube.com/watch?v=T0Tkd6m6kpI \n\nCall me G: https://www.youtube.com/channel/UCiFS_E_63VjpSxzbIP9iRZQ\n\n\n\n\n\nVideoschnitt mit meiner Regie:\nTius\nhttps://www.instagram.com/teasingtius/\n\n\n"Kamera": \nAlina\nhttps://www.instagram.com/alina_marie2606/ \n\n\n\nIdee:\n\nLxU \nInstagram: https://www.instagram.com/l__x__u/\nYoutube: https://www.youtube.com/channel/UCUx30nt2CwLLDlwcjOkmmRw\nGmail: luckasxunknow@gmail.com\n\n\n\nKUSS AN DIE EHRENMÄNNER IM VIDEO\n\n\n\nText:\n\nDas ist 16bars 2; Netflix teenage moms geil\nTeenage-Mutant-Ninja-Turtles, Hirnis sind mein Staatsfeind\nIch bin einfach nur geil, mache Wäsche wieder weiß\nBro sie nenn mich weißer Riese, dikker Stichwort Weißer Hai\n\nDiese Haiforscherbitches suchen keine Orca Snitch ey\nSie sind auf der Suche nach dem großen Weißen, in mein Slips ey\nBin so hart Lauch, vegane Hoes würden an meinem Meat nagen\nDu bist so nen Spast gönnst dir ne Polin in nem Mietwagen\n\nSchreibe alle Texte in der Hotboxmatrix\nGeheimratsecken sind auf dieser Überbossbasis \nAlle sagen immer ich wär von Medusa gefickt\nDoch ich bin nur stoned lookalike von *picklerick*\n\nIch penetrier deine Mum mit meinem scheiß Enderstab\nBaller ihn rein wie ein Kolben und sie schreit wie ein Schaf\nDeine Mum möcht Hertha/härter bangen, ya sie steckt in einer Krise\nDoch das geht nicht denn die ist ne Randerscheinung, KÖLNER ZIEGE\n\nHook (Fake Fresh Dumbledore):\n16bars, Hook bin der Hellste nennt mich Lampenlord\nIch werde zwar alt doch bleib für immer\nFresh Dumbledore\nBaller weiter deine Mum doch nicht mehr mit dem Enderstab\nScheiß einfach MC man und gönn ihr jetzt mit Elderstab! \n\n\n\n\n\nDANKE FÜR DIE NOMINIERUNG AN NIKLAS AKA LUCKYPUNCH.\n\n\n!!! EHRE AN ALLE DIE MIR NEN FEEDBACK IN DEN KOMMENTAREN HINTERLASSEN !!!', 'publishedAt': '2020-05-23T17:00:12Z', 'channelTitle': 'Luckas x Unknow', 'channelId': 'UCUx30nt2CwLLDlwcjOkmmRw', 'categoryId': '10', 'categoryName': 'Music' }, 'contentDetails': { 'duration': 156, 'dimension': '2d', 'definition': 'hd' }, 'statistics': { 'viewCount': '190', 'likeCount': '21', 'dislikeCount': '0', 'favoriteCount': '0', 'commentCount': '4' }, 'topicDetails': { 'relevantTopicIds': ['/m/0glt670', '/m/04rlf', '/m/04rlf', '/m/0glt670'], 'topicCategories': ['https://en.wikipedia.org/wiki/Hip_hop_music', 'https://en.wikipedia.org/wiki/Music'] } }]