[Python] A simple music downloader
Hey everyone !
So I had this script laying around, I decided to modify it a bit and add a functionality.
It is basically a youtube downloader.
What makes it different from the bunch of others that are on the web ?
Well first it's coded by me and you have the source code Big Grin
Secondly, it's entirely on your side, no need to go on a website full of ads.
Third, you have the choice between pasting the url or searching it.

What can this do ?
Not that many things actually, but it does it's work. You have two choices, either you copy paste the url in the program which will directly download it, or you type a few keywords related to the video and it'll download it. I have to say this isn't that precise but works most of the time.

Some infos related to it :
-Your videos will be downloaded in the same folder as the program, this might change if required, but I didn't feel the urge of doing it.
-The downloads will have the .m4a extension, I'm actually looking for a way to convert them to mp3's.
-The videos are downloaded with the best audio quality

Any advice or feedback is welcome !
(I know it has nothing to do with security, but I thought it might be useful to some of you)

#!/usr/bin/env python3

import requests
from bs4 import BeautifulSoup
import pafy
import os
import urllib3

#here i just set the basic variables, can't recall why i've done it but it's here.
ytb = 'http://www.youtube.com/results?search_query='
a = 0
add_to_query = "http://www.youtube.com/results?search_query="

while 1:
    recherche = input("\nType the name of the music/video you'd like to download from youtube or copy/paste it's url. ")
    #split the words in order to build an url that looks like a youtube search
    if recherche[0:30] == "http://www.youtube.com/watch?v=":
        dl = pafy.new(recherche, basic=True)
        print ("Downloading "+dl.title)
        bestaudio = dl.getbestaudio()
    elif recherche[0:31] == "http://www.youtube.com/watch?v=":
        dl = pafy.new(recherche, basic=True)
        print ("Downloading "+dl.title)
        bestaudio = dl.getbestaudio()
        quer = recherche.split(" ")
        for search in quer:
            add_to_query += "+"
            add_to_query += search
        #here i get the html of the page and parse it in order to find the url of the first result
        page_ytb = requests.get(add_to_query+'/get',verify=True)
        soup = BeautifulSoup(page_ytb.text, "html.parser")
        soup1 = soup.find_all('ol')
        soup2 = soup1[2]
        soup3 = str(soup2)
            #simply downloads it using pafy
            soup5 = soup3[162:173]
            vid_url = "http://www.youtube.com/watch?v="+soup5
            dl = pafy.new(vid_url, basic=True)
            print (dl.title)
            bestaudio = dl.getbestaudio()
    add_to_query = "http://www.youtube.com/results?search_query="

Enjoy !

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Ebook Collection [89 Files] Insider 15 88,553 08-12-2021, 08:02 PM
Last Post: zzeuss
  NSA Python Training Insider 4 29,014 08-12-2021, 02:14 AM
Last Post: hworth
  Having an issue writing a python script with vim FancyBear 4 22,696 01-03-2021, 11:27 PM
Last Post: FancyBear
  Python Data structures and algorithms resources skinnyj0shua 1 17,485 12-23-2020, 12:52 PM
Last Post: enmafia2