Last active
August 10, 2022 20:39
-
-
Save rdenadai/cbcc26751f99bc77e9aa to your computer and use it in GitHub Desktop.
A simple python script that downloads a deezer playlist and bam! download magnetic files from pirate bay or monova and put on transmission torrent client... keep in mind that you need transmission to run this, open it and configure the remote option in preferences!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# DEPENDENCIES | |
# LINUX INSTALLS! | |
# apt-get install transmission firefox python-dev python-pip | |
# PYTHON LIBS NEED IT! | |
# pip install requests beautifulsoup4 transmissionrpc pyopenssl ndg-httpsclient pyasn1 selenium | |
import requests as req | |
import transmissionrpc | |
from selenium import webdriver | |
from bs4 import BeautifulSoup as bs | |
# NEED IT TO BYPASS THE PYTHON 2 OPENSSL ERROR! | |
import urllib3.contrib.pyopenssl | |
urllib3.contrib.pyopenssl.inject_into_urllib3() | |
# IS POSSIBLE TO DOWNLOAD FROM OTHER SOURCES, BUT THEN WE HAVE A DIFFERENT PARSE!! | |
# torrentz_uri = "https://torrentz.eu/any?q=" | |
deezer_api_uri = "https://api.deezer.com/playlist/1471908257" | |
class DefaultDownloader: | |
def __init__(self): | |
self.delay = 20 # seconds | |
self.driver = webdriver.Firefox() | |
def execute_search(self, artist, song_name): | |
torrent_file_uri = self.get_torrent_file_uri( | |
self.get_download_page(artist, song_name) | |
) | |
return torrent_file_uri | |
def get_download_page(self, artist, song_name): | |
return "" | |
def get_torrent_file_uri(self, uri): | |
return "" | |
class PirateBayDownloader(DefaultDownloader): | |
def __init__(self): | |
self.pirate_bay_uri = "https://thepiratebay.mn" | |
def get_download_page(self, artist, song_name): | |
download_page_uri = "" | |
# uri = "%s%s+%s&method=" % (monova_uri, artist, song_name,) | |
uri = "%s/search/%s+%s/0/99/100" % (self.pirate_bay_uri, artist, song_name,) | |
r = req.get(uri, verify=True) | |
if r.status_code == 200: | |
soup = bs(r.text, "lxml") | |
# tds = soup.find_all("td", attrs={'class': 'torrent_name'}) | |
links = soup.find_all("a", attrs={'class': 'detLink'}) | |
if len(links) > 0: | |
# link = tds[0].find("a") | |
link = links[0] | |
if link: | |
download_page_uri = "%s%s" % (self.pirate_bay_uri, link['href'], ) | |
return download_page_uri | |
def get_torrent_file_uri(self, uri): | |
torrent_uri = "" | |
print uri | |
if uri: | |
r = req.get(uri, verify=True) | |
print r.status_code | |
if r.status_code == 200: | |
soup = bs(r.text, "lxml") | |
# link = soup.find_all("a", attrs={'id': 'download-file'}) | |
# LETS GET THE MAGNETIC LINK BUTTON | |
div = soup.find_all("div", attrs={'class': 'download'}) | |
if div: | |
link = div[0].find_all("a") | |
if link: | |
torrent_uri = "%s" % (link[0]["href"], ) | |
return torrent_uri | |
class MonovaDownloader(DefaultDownloader): | |
def __init__(self): | |
self.monova_uri = "https://www.monova.org/search?term=" # &method= | |
def get_download_page(self, artist, song_name): | |
download_page_uri = "" | |
uri = "%s%s+%s&method=" % (self.monova_uri, artist, song_name,) | |
r = req.get(uri, verify=True) | |
if r.status_code == 200: | |
soup = bs(r.text, "lxml") | |
tds = soup.find_all("td", attrs={'class': 'torrent_name'}) | |
if len(tds) > 0: | |
link = tds[0].find("a") | |
if link: | |
download_page_uri = "https:%s%s" % (self.monova_uri, link['href'], ) | |
return download_page_uri | |
def get_torrent_file_uri(self, uri): | |
torrent_uri = "" | |
print uri | |
if uri: | |
self.driver.get(uri) | |
try: | |
WebDriverWait(self.driver, self.delay).until(EC.presence_of_element_located(self.driver.find_element_by_xpath("//a[@id='download-magnet']"))) | |
print "Page is ready!" | |
torrent_uri = "%s" % (self.driver.find_element_by_xpath("//a[@id='download-magnet']").get_attribute('href'), ) | |
except TimeoutException: | |
print "Loading took too much time!" | |
return torrent_uri | |
def get_tracks_from_deezer(tracks): | |
curated_tracks = [] | |
for track in tracks: | |
artist = "+".join(track['artist']['name'].lower().split()) | |
song_name = "+".join(track['title'].lower().split()) | |
curated_tracks.append((artist, song_name)) | |
return curated_tracks | |
if __name__ == "__main__": | |
# Start transmission in you linux env, dont forget to enable in Preferences remote access!!! | |
tc = transmissionrpc.Client(address='localhost') | |
# Load deezer playlist music names! | |
r = req.get(deezer_api_uri) | |
if r.status_code == 200: | |
deezer = r.json() | |
json_ok = False | |
try: | |
deezer['id'] + 1 | |
json_ok = True | |
except: | |
pass | |
# WE HAVE JSON PLAYLIST | |
if json_ok: | |
tracks = get_tracks_from_deezer(deezer['tracks']['data']) | |
# WE HAVE TRACKS! | |
if len(tracks) > 0: | |
with open("not_downloaded_songs.txt", "wb") as fh: | |
pirate_bay_downloader = PirateBayDownloader() | |
monova_downloader = None | |
for track in tracks: | |
artist = track[0] | |
song_name = track[1] | |
filename = "%s-%s.torrent" % (artist, song_name) | |
torrent_file_uri = pirate_bay_downloader.execute_search(artist, song_name) | |
if not torrent_file_uri: | |
# Lets trully start monova downloader class here!! | |
if not monova_downloader: | |
monova_downloader = MonovaDownloader() | |
torrent_file_uri = monova_downloader.execute_search(artist, song_name) | |
if torrent_file_uri: | |
# THIS IS WERE THE MAGIC HAPPENS! | |
tc.add_torrent(torrent_file_uri) | |
else: | |
# WE COULD NOT FIND THIS! SORRY! | |
fh.write("%s - %s \n" % (artist, song_name, )) | |
else: | |
# THIS IS WERE THE MAGIC HAPPENS! | |
tc.add_torrent(torrent_file_uri) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment