6

I am very new to python, I'm wondering how I would just get the maxres thumbnail to print, instead of literally everything about the video. Sorry if the answer is a bit obvious, I am a bit new to python and programming in general.

from apiclient.discovery import build

DEVELOPER_KEY = 'xxxxxx'
youtube = build('youtube', 'v3', developerKey=DEVELOPER_KEY)

ids = '6Gw-RyTRMns'
results = youtube.videos().list(id=ids, part='snippet').execute()
for result in results.get('items', []):
    print(results)    

I'm also wondering if there is a way to grab a URL to a picture from a description of a given video and have it saved to a folder.

yevvy
  • 63
  • 1
  • 3

4 Answers4

10

This has been made easy thanks to YouTube. They use links like the one below to retreive thumbnails at different resolutions. Assuming all you need is the URL, just format a string like this:

http://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg

You can find a similar question here: How do I get a YouTube video thumbnail from the YouTube API?

You can find more information on the Api here: https://developers.google.com/youtube/v3/

To iterate over a channel you can find another similar question here: python: get all youtube video urls of a channel

To download the images you could use something like this.

import urllib
urllib.urlretrieve("http://img.youtube.com/vi/ytvideo/0.jpg")
Community
  • 1
  • 1
James
  • 773
  • 1
  • 6
  • 15
  • how would I go about doing the second part of my question? – yevvy Oct 28 '16 at 03:19
  • In that question did you mean to get links directly out of a videos description? – James Oct 28 '16 at 03:20
  • If so I would suggest using regex, you can find an example of this here: maxresdefault.jpg – James Oct 28 '16 at 03:25
  • http://stackoverflow.com/questions/6883049/regex-to-find-urls-in-string-in-python – James Oct 28 '16 at 03:26
  • @yevvy I've updated the answer with a link you could use to implement this – James Oct 28 '16 at 03:29
  • I'm still not really sure on what exactly to do (in my first question). My main goal is to try to scrape the thumbnails of a large number, if not all of the videos off of a channel. Is there a way to get the id of every video on a channel, input it into a script, and have it save all of the pictures into a folder besides going through and saving all of the images myself? – yevvy Oct 28 '16 at 03:42
  • @yevvy This may be what you're looking for: http://stackoverflow.com/questions/15512239/python-get-all-youtube-video-urls-of-a-channel – James Oct 28 '16 at 03:47
  • yes but then how would I get the ID of all of them and what would I put it into to get a list of every link to the thumbnail of every video – yevvy Oct 28 '16 at 04:16
  • @yevvy After you create a function using the API to iterate over the videos, you will be given a list of metadata from the API, you can access this with something like `id = api_result['video_id']` add this to a list `listname.append(id)` then iterate over that list using the code I have provided to download the thumbnails. I highly suggest that you read Youtubes very details Api reference that directly answers you question here https://developers.google.com/youtube/v3/guides/implementation/videos#videos-retrieve-uploads – James Oct 28 '16 at 04:37
3

For those reading this answer now, the updated url method is:

http://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69
2

Here is a simple code. The regular expression matches all the shared and embedded URLs

import re
url = # Any Youtube URL
exp = "^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*"
s = re.findall(exp,url)[0][-1]
thumbnail = f"https://i.ytimg.com/vi/{s}/maxresdefault.jpg"
print(thumbnail)
Viraj Mane
  • 29
  • 2
0

Add os.chdir to manipulate directories to store thumnails.

import re
import requests
import os

#urls to id
url = "YouTube URL"
exp = "^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*"
s = re.findall(exp,url)[0][-1]
thumbnail = f"https://i.ytimg.com/vi/{s}/maxresdefault.jpg"

#image scraping
def imagedown(url, folder):
    try:
        os.mkdir(os.path.join(os.getcwd(), folder))
    except:
        pass
    os.chdir(os.path.join(os.getcwd(), folder))

    name = url
    link = url
    with open(name.replace(' ', '-').replace('/', '') + '.jpg', 'wb') as f:
        im = requests.get(link)
        f.write(im.content)
        print('Writing: ', name)

imagedown(thumbnail, 'image')
Ryan
  • 5
  • 3