Skip to content

Instantly share code, notes, and snippets.

@Taehun
Last active February 5, 2018 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Taehun/4411d7a8dd6052db9b1f10264a0f6895 to your computer and use it in GitHub Desktop.
Save Taehun/4411d7a8dd6052db9b1f10264a0f6895 to your computer and use it in GitHub Desktop.
유튜브에서 최신가요 mp3 파일로 가져오기
#!/usr/bin/env python
# $ sudo apt install youtube-dl ffmpeg
import bs4
import requests
import os
# 아래 URL 수정. 최신가요가 올라오는 유튜브 채널로 URL로 변경
channel_url = 'https://www.youtube.com/channel/UC8inZoPlovl-QsPUbKFv5JQ/videos'
base_url = 'https://www.youtube.com/watch?v='
response = requests.get(channel_url)
html = response.text
soup = bs4.BeautifulSoup(html, "html.parser")
latest_music_id = soup.find(class_='yt-lockup-video').get('data-context-item-id')
music_id_fname = "~/music_id"
if os.path.exists(music_id_fname) == False:
with open(music_id_fname, 'w') as f:
f.write("")
pre_music_id = ""
with open(music_id_fname, 'r') as f:
pre_music_id = f.read()
if pre_music_id != latest_music_id:
with open(music_id_fname, 'w') as f:
f.write(latest_music_id)
os.system("youtube-dl -x --audio-format mp3 --output new_music.mp3 " + base_url + latest_music_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment