Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from urllib.request import urlopen as uReq
- from bs4 import BeautifulSoup as soup
- import ssl
- import os
- import sys
- import json
- # To pass "Certificate verified failed" *
- context = ssl._create_unverified_context()
- genre_urls = {
- # "Techhouse": "https://www.beatport.com/genre/tech-house/11/top-100",
- # "Techno": "https://www.beatport.com/genre/techno/6/top-100",
- "House": "https://www.beatport.com/genre/house/5/top-100"
- }
- class Downloader:
- def __init__(self, genre_urls):
- self.genre_urls = genre_urls
- # scraping Top 100 from genre_urls and
- def wscraping(self):
- # creating dictionary that will store "track lists"
- self.track_list_top100 = dict()
- for genre, urls in genre_urls.items():
- page = uReq(urls, context=context)
- page_html = page.read()
- page.close()
- page_soup = soup(page_html, "html.parser")
- containers = page_soup.findAll("div",{"class":"buk-track-meta-parent"})
- for container in containers:
- strip_artist = container.find("p", {"class":"buk-track-artists"})
- artist = strip_artist.a.text
- get_title = container.find("p", {"class":"buk-track-title"})
- title = get_title.a.span.text
- remix = container.find("span", {"class":"buk-track-remixed"}).getText()
- release = container.find("p", {"class":"buk-track-released"}).getText()
- # looping thru and storing all values from top 100 in to the dictionary
- self.track_list_top100[artist] = [title, remix, release]
- def load_from_downloaded(self):
- # load list of tunes that has been downloaded and store them in to dict
- filename = 'downloaded.json'
- with open (filename, 'r') as f_obj:
- self.downloaded_tunes = json.load(f_obj)
- def new_to_download(self):
- # compare set of tunes from top100 to the already downloaded and
- # store new titles in new dict and return his value
- self.tunes_to_download = dict()
- for key in set(self.track_list_top100.keys()) - set(self.downloaded_tunes.keys()):
- self.tunes_to_download[key] = self.track_list_top100[key]
- if not self.tunes_to_download:
- return None
- else:
- return self.tunes_to_download
- def append_to_downloaded(self):
- filename = 'downloaded.json'
- list_to_download = self.new_to_download()
- if list_to_download:
- with open(filename, 'a') as f_obj:
- json.dump(list_to_download, f_obj, indent=4)
- else:
- print("Nothing to download")
- def get_from_youtube(self):
- # Downloading tunes from "list to download"
- try:
- for artist, [title, remix, release] in self.list_to_download.items():
- tune = "{} - {} ({}) ".format(artist, title, remix)
- os.system('youtube-dl --extract-audio -f bestaudio \
- --audio-quality 0 --audio-format mp3 "ytsearch1: {}" \
- --output "~/Documents/test/{}.%(ext)s" '.format(tune, tune))
- # appending downloaded tunes to the list of downloaded
- self.append_to_downloaded()
- except KeyboardInterrupt:
- sys.exit()
- def main():
- try:
- test = Downloader(genre_urls)
- test.wscraping()
- test.load_from_downloaded()
- test.new_to_download()
- test.get_from_youtube()
- except KeyboardInterrupt:
- sys.exit()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment