Guest User

Untitled

a guest
Jun 25th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | None | 0 0
  1. from urllib.request import urlopen as uReq
  2. from bs4 import BeautifulSoup as soup
  3. import ssl
  4. import os
  5. import sys
  6. import json
  7.  
  8. # To pass "Certificate verified failed" *
  9. context = ssl._create_unverified_context()
  10.  
  11. genre_urls = {
  12.     # "Techhouse": "https://www.beatport.com/genre/tech-house/11/top-100",
  13.     # "Techno": "https://www.beatport.com/genre/techno/6/top-100",
  14.     "House": "https://www.beatport.com/genre/house/5/top-100"
  15. }
  16.  
  17.  
  18. class Downloader:
  19.  
  20.    
  21.     def __init__(self, genre_urls):
  22.  
  23.         self.genre_urls = genre_urls
  24.  
  25.     #    scraping Top 100 from genre_urls  and  
  26.     def wscraping(self):
  27.         #    creating dictionary that will store "track lists"
  28.         self.track_list_top100 = dict()
  29.  
  30.         for genre, urls in genre_urls.items():
  31.             page = uReq(urls, context=context)
  32.             page_html = page.read()
  33.             page.close()
  34.             page_soup = soup(page_html, "html.parser")
  35.             containers = page_soup.findAll("div",{"class":"buk-track-meta-parent"})
  36.  
  37.             for container in containers:
  38.                 strip_artist = container.find("p", {"class":"buk-track-artists"})
  39.                 artist = strip_artist.a.text
  40.                 get_title = container.find("p", {"class":"buk-track-title"})
  41.                 title = get_title.a.span.text
  42.                 remix = container.find("span", {"class":"buk-track-remixed"}).getText()
  43.                 release = container.find("p", {"class":"buk-track-released"}).getText()
  44.                 #    looping thru and storing all values from top 100 in to the dictionary
  45.                 self.track_list_top100[artist] = [title, remix, release]
  46.            
  47.                
  48.  
  49.     def load_from_downloaded(self):
  50.         #  load list of tunes that has been downloaded and store them in to dict
  51.         filename = 'downloaded.json'
  52.         with open (filename, 'r') as f_obj:
  53.             self.downloaded_tunes = json.load(f_obj)
  54.  
  55.    
  56.  
  57.     def new_to_download(self):
  58.         #  compare set of tunes from top100 to the already downloaded and
  59.         #  store new titles in new dict and return his value
  60.         self.tunes_to_download = dict()
  61.         for key in set(self.track_list_top100.keys()) - set(self.downloaded_tunes.keys()):
  62.             self.tunes_to_download[key] = self.track_list_top100[key]
  63.  
  64.         if not self.tunes_to_download:
  65.             return None
  66.         else:
  67.             return self.tunes_to_download
  68.        
  69.  
  70.  
  71.     def append_to_downloaded(self):
  72.  
  73.         filename = 'downloaded.json'
  74.         list_to_download = self.new_to_download()
  75.         if list_to_download:
  76.             with open(filename, 'a') as f_obj:
  77.                 json.dump(list_to_download, f_obj, indent=4)
  78.         else:
  79.             print("Nothing to download")
  80.                
  81.  
  82.     def get_from_youtube(self):
  83.         #  Downloading tunes from "list to download"
  84.         try:
  85.             for artist, [title, remix, release] in self.list_to_download.items():
  86.                 tune = "{} - {} ({}) ".format(artist, title, remix)
  87.                 os.system('youtube-dl --extract-audio -f bestaudio \
  88.                --audio-quality 0 --audio-format mp3 "ytsearch1: {}" \
  89.                --output "~/Documents/test/{}.%(ext)s" '.format(tune, tune))
  90.  
  91.         #  appending downloaded tunes to the list of downloaded
  92.             self.append_to_downloaded()
  93.  
  94.  
  95.         except KeyboardInterrupt:
  96.             sys.exit()
  97.            
  98.      
  99. def main():
  100.    
  101.  
  102.     try:
  103.  
  104.         test = Downloader(genre_urls)
  105.         test.wscraping()
  106.         test.load_from_downloaded()
  107.         test.new_to_download()
  108.         test.get_from_youtube()
  109.  
  110.        
  111.     except KeyboardInterrupt:
  112.         sys.exit()
  113.        
  114.  
  115.  
  116. if __name__ == "__main__":
  117.     main()
Advertisement
Add Comment
Please, Sign In to add comment