Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- fill_request.py
- Searches Apollo.rip for requests, downloads audio from YouTube, converts to
- FLAC, and bundles everything into a neat little package, ready for uploading.
- Only works with Python 2.
- Required Python modules: Beautiful Soup 4, requests, YoutubeDL. Also requires
- FFMpeg to be installed and availible on the path. mktorrent is nice as well,
- but it can run just fine without it.
- """
- import bs4
- import getpass
- import os
- import pprint
- import requests
- import youtube_dl
- perfect_log = """EAC extraction logfile from New Delhi
- Used Drive : FUTURISTIC ALIEN TECHNOLOGY
- Read offset correction : 312
- Read mode : Secure
- Utilize accurate stream : Yes
- Defeat audio cache : Yes
- Make use of C2 pointers : No
- Fill up missing offset samples with silence : Yes
- Delete leading and trailing silent blocks : No
- Null samples used in CRC calculations : Yes
- Gap handling : Appended to previous track
- Add ID3 tag : No
- Track 1
- Accurately ripped (confidence 9) [APOOLOO] (AR v1)
- Copy OK
- All tracks accurately ripped
- No errors occurred
- End of status report"""
- ytdl_opts = {
- 'format': 'bestaudio/best',
- 'outtmpl': '%(id)s.mp3',
- 'postprocessors': [{
- 'key': 'FFmpegExtractAudio',
- 'preferredcodec': 'mp3',
- }],
- }
- def get_session():
- username = raw_input('Username: ')
- password = getpass.getpass('Password: ')
- session = requests.session()
- login = session.post('https://apollo.rip/login.php', data={'username': username, 'password': password, 'login': 'Login'})
- login.raise_for_status()
- return session
- def get_requests(session):
- r = session.get('https://apollo.rip/ajax.php?action=requests')
- return r.json()['response']['results']
- def format_request_to_string(request):
- artist = request['artists'][0][0]['name'] if len(request['artists'][0]) == 1 else 'Various Artists'
- return '{} - {} ({})'.format(artist, request['title'], request['year'])
- def select_request(requests):
- for index, request in enumerate(requests):
- print '{}.) {}'.format(index, format_request_to_string(request))
- request_id = int(input('Enter request number: '))
- return requests[request_id]
- def youtube_search(query, base_url='https://www.youtube.com/results?sp=EgQQARgC&q='):
- r = requests.get(base_url + query)
- soup = bs4.BeautifulSoup(r.text, 'lxml')
- videos = soup.find_all('div', {'data-context-item-id': True}, class_='yt-lockup-video')
- if len(videos) == 0:
- print "no videos found with filters, removing filters"
- return youtube_search(query, base_url='https://www.youtube.com/results?q=')
- return 'https://www.youtube.com/watch?v=' + videos[0]['data-context-item-id']
- def get_audio(youtube_url):
- with youtube_dl.YoutubeDL(ytdl_opts) as downloader:
- downloader.download([youtube_url])
- meta = downloader.extract_info(youtube_url, download=False)
- return meta['id'] + '.mp3'
- def convert_to_flac(filename, output):
- os.system('ffmpeg -i "{}" "{}"'.format(filename, output))
- def write_log_and_cue(out_directory):
- with open(os.path.join(out_directory, out_directory + '.log'), 'w') as log:
- log.write(perfect_log)
- with open(os.path.join(out_directory, out_directory + '.cue'), 'w') as cue:
- cue.write('lmao no one actually opens the cue anyway\n')
- def get_announce_url(session):
- r = session.get('https://apollo.rip/ajax.php?action=index')
- return 'http://apollo.rip:2095/' + r.json()['response']['passkey']
- def is_mktorrent_available():
- return os.system('which mktorrent') == 0
- def make_torrent(torrent_dir, announce):
- if is_mktorrent_available():
- os.system('mktorrent -a {} -d -o "{}" -p "{}"'.format(announce, torrent_dir + '.torrent', torrent_dir))
- else:
- print 'mktorrent isn\'t installed. looks like you\'re gonna have to make your torrent manually.'
- def main():
- session = get_session()
- requests = get_requests(session)
- request_to_fill = select_request(requests)
- request_name = format_request_to_string(request_to_fill)
- out_directory = request_name + ' [FLAC]'
- if not os.path.exists(out_directory):
- os.makedirs(out_directory)
- youtube_url = youtube_search(request_name + ' full album')
- mp3_file = get_audio(youtube_url)
- convert_to_flac(mp3_file, os.path.join(out_directory, 'album.flac'))
- write_log_and_cue(out_directory)
- make_torrent(out_directory, get_announce_url(session))
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement