Skip to content

Instantly share code, notes, and snippets.

@willianrschuck
Created July 1, 2019 01:47
Show Gist options
  • Save willianrschuck/71693c77f2ab8708729fd315f9089b83 to your computer and use it in GitHub Desktop.
Save willianrschuck/71693c77f2ab8708729fd315f9089b83 to your computer and use it in GitHub Desktop.
import sys
import requests
from urllib.parse import urlparse
from urllib.parse import parse_qs
if len(sys.argv) == 1:
print('url não foi informada')
exit()
url = sys.argv[1]
r = requests.get(url)
o = urlparse(url) # Cria um objeto com as informações da URL
query = parse_qs(o.query) # Separa as informações passadas pelo método GET
# Tenta baixar a imagem com a maior qualidade possível
url = 'https://i.ytimg.com/vi/' + query['v'][0] + '/maxresdefault.jpg'
r = requests.get(url)
# Caso a imagem não seja encontrada, tenta baixar em uma qualidade inferior
if int(r.headers['content-length']) < 2048:
url = 'https://i.ytimg.com/vi/' + query['v'][0] + '/hqdefault.jpg'
r = requests.get(url)
arq = open('thumb_'+ query['v'][0] +'.jpg', 'wb')
arq.write(r.content)
arq.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment