Created
October 20, 2021 07:29
-
-
Save Pixelsuft/b492ba787be72b2380c5be39e64c425c to your computer and use it in GitHub Desktop.
Newgrounds Music Downloader
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
import os | |
import sys | |
import requests | |
def parse_song(song_id: int) -> bytes: | |
url = f'https://www.newgrounds.com/audio/listen/{song_id}' | |
resp = requests.get(url) | |
cur_line = '' | |
for line in resp.text.split('\n'): | |
if not 'var embed_controller = new embedController'.lower() in line.lower(): | |
continue | |
cur_line = line.strip() | |
if not cur_line: | |
return None | |
mp3_url = cur_line.split('"')[3].replace('\\/', '/').split('?')[0] | |
print(f'Info: Downloading "{mp3_url}"...') | |
mp3_resp = requests.get(mp3_url) | |
return mp3_resp.content | |
def main() -> None: | |
if len(sys.argv) <= 1: | |
print(f'Usage: "{sys.executable}" "{__file__}" "id"') | |
sys.exit(0) | |
song_id = int(sys.argv[1]) | |
result = parse_song(song_id) | |
if not result: | |
print('Error: Can\'t download the song') | |
sys.exit(0) | |
f = open(f'{song_id}.mp3', 'wb') | |
f.write(result) | |
f.close() | |
print(f'Info: OK!') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment