Skip to content

Commit

Permalink
Merge pull request #48 from Morpheus636/main
Browse files Browse the repository at this point in the history
Release v0.1.2
  • Loading branch information
Morpheus636 committed Jun 27, 2022
2 parents aad8bf6 + aa85d43 commit d5a6358
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/code_analysis_pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
run: poetry run isort

- name: Black (Code Formatter)
run: poetry run black src/ tests/
run: find . -type f -name "*.py" | xargs poetry run black

- name: Check for changes
id: changes
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/code_analysis_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
run: poetry run isort

- name: Black (Code Formatter)
run: poetry run black src/ tests/
run: find . -type f -name "*.py" | xargs poetry run black

- name: Check for changes
id: changes
Expand Down
10 changes: 8 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Redownload
A python based utility for downloading mp3 files from relisten.
<a href="https://discord.morpheus636.com"><img src="https://img.shields.io/badge/Discord-%237289DA.svg?style=for-the-badge&logo=discord&logoColor=white" /></a><a href="https://github.com/Morpheus636/redownload"><img src="https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white" /></a>
<a href="https://www.python.org"><img src="https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=white"></a>

A python based utility for downloading mp3 files from [Relisten](https://relisten.net).

## Features
- Download music from Relisten and Archive.org automatically
Expand All @@ -21,4 +24,7 @@ To ask a question or get support, you can join my [Discord Server](https://disco
[Forums](https://forums.morpheus636.com)

## Contributing
To contribute to this project, view the Contribution Docs found in [CONTRIBUTING.md](CONTRIBUTING.md)
To contribute to this project, view the Contribution Docs found in [CONTRIBUTING.md](CONTRIBUTING.md)

## Credits
Custom Badges by <a href="https://ileriayo.github.io/markdown-badges/">markdown-badges</a>
38 changes: 26 additions & 12 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,41 @@ Instructions for downloading it and using it are as follows:
- Go to the [Releases](https://github.com/Morpheus636/redownload/releases) page in this repository.
- Select the most recent stable release (or alpha/beta, but you may have problems with them).
- Download the file starting with `redownload-cli` for your operating system.
## Linux
## <img alt="Linux" src="https://cdn-icons-png.flaticon.com/512/6124/6124995.png" width="25" /> Linux
- Open a terminal
- `cd` into the directory containing the `redownload-cli-linux` file
- run `chmod +x ./redownload-cli-linux`
- you can now run the file with `./redownload-cli-linux <archive.org OR relisten.net link>`. This will download all the .mp3's and .flac's
- Run `chmod +x ./redownload-cli-linux`
- You can now run the file with `./redownload-cli-linux <archive.org OR relisten.net link>`. This will download all the .mp3's and .flac's
to `./redownloads/`
- if you need more options, you can run `./redownload-cli-linux --help` and it will print the help to the console.
- If you need more options, you can run `./redownload-cli-linux --help` and it will print the help to the console.

## MacOS
## <img alt="MacOS" src="https://cdn-icons-png.flaticon.com/512/179/179309.png" width="25" /> MacOS
- Open a terminal
- `cd` into the directory containing the `redownload-cli-mac` file.
- run `chmod +x ./redownload-cli-mac`
- you can now run the file with `./redownload-cli-mac <archive.org OR relisten.net link>`. This will download all the .mp3's and .flac's
- Run `chmod +x ./redownload-cli-mac`
- You can now run the file with `./redownload-cli-mac <archive.org OR relisten.net link>`. This will download all the .mp3's and .flac's
to `./redownloads/`
- if you need more options, you can run `./redownload-cli-linux --help` and it will print the help to the console.
- If you need more options, you can run `./redownload-cli-linux --help` and it will print the help to the console.

## Windows
### <img alt="Error Icon" src="https://cdn-icons-png.flaticon.com/512/1304/1304037.png" width="20" /> If you get "Apple can’t check app for malicious software" error,
By default, MacOS prevents apps from running if your system can't check if the software is malicious or not.
**This github repository is not malicious**, so it's safe to say we can override this and run the software anyway.

- Immediately after you get the popup, go to **System Preferences** > **Security and Privacy**
- Near the bottom of the page, next to where it says "redownload-cli-mac was blocked from use because it is not from an identified developer", click the button that says - "**Open Anyway**"
- **Run the app again**. (`./redownload-cli-mac ...`)
- A confirmation popup will appear. **Click "Open" in the new popup**.

## <img alt="Windows" src="https://cdn-icons-png.flaticon.com/512/888/888882.png" width="25" /> Windows
- Open cmd.exe
- `cd` into the directory containing the `redownload-cli-windows.exe` file.
- run the file with `redownload-cli-windows <archive.org OR relisten.net link`. This will download all the .mp3's and .flac's
- Run the file with `redownload-cli-windows <archive.org OR relisten.net link`. This will download all the .mp3's and .flac's
to a subdirectory of your working directory called `redownloads`
- if you need more options, you can run `./redownload-cli-windows --help` and it will print the help to the console.
- If you need more options, you can run `./redownload-cli-windows --help` and it will print the help to the console.

## <img alt="Help!" src="https://emojis.slackmojis.com/emojis/images/1643514968/9949/blob_help.png?1643514968" width="25" /> Help!
If you need help or clarification, join me on [Discord](https://discord.morpheus636.com) or, you can open a [GitHub Issue](https://github.com/Morpheus636/redownload/issues).


If you need help or clarification, join me on [Discord](https://discord.morpheus636.com) or open a [GitHub Issue](https://github.com/Morpheus636/redownload/issues)
## Credits
Uicons by <a href="https://www.flaticon.com/uicons">Flaticon</a>
38 changes: 12 additions & 26 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
name = "redownload"
version = "0.1.0"
description = ""
authors = ["Josh Levin (Morpheus636) <josh@joshinshaker.com>"]
authors = ["Josh Levin <morpheus636@morpheus636.com>"]

[tool.poetry.dependencies]
python = "^3.10, <3.11"
beautifulsoup4 = "^4.10.0"
requests = "^2.27.1"
certifi = "^2021.10.8"
PyYAML = "^6.0"

[tool.poetry.dev-dependencies]
flake8 = "^3.9.2"
Expand Down
6 changes: 4 additions & 2 deletions src/redownload/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import urllib.parse

from . import downloads, exceptions, version, web_parsing
from . import config, downloads, exceptions, version, web_parsing


def redownload(link, filetypes, output_dir):
def redownload(
link, filetypes=config.config["track_formats"], output_dir=config.config["output_dir"]
):
parsed_link = urllib.parse.urlparse(link)
if parsed_link.hostname == "archive.org":
final_page = web_parsing.html_from_url(link)
Expand Down
100 changes: 100 additions & 0 deletions src/redownload/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import os
import platform
import typing

import yaml


# Set the IS_UNIX constant to true or false based on the platform
if platform.system() == "Linux" or platform.system() == "Darwin":
IS_UNIX = True
else:
IS_UNIX = False
import winreg

# Set the LOCATION constant
if not IS_UNIX:
CONFIG_DIR = os.path.join(os.getenv("APPDATA"), "redownload")
LOCATION = os.path.join(CONFIG_DIR, "config.yml")
elif IS_UNIX:
CONFIG_DIR = os.path.join(os.getenv("HOME"), ".config", "redownload")
LOCATION = os.path.join(CONFIG_DIR, "config.yml")

# Define the empty config variable to be populated by load()
config = dict()


def dump(config_dict: dict, location=LOCATION) -> None:
"""Dumps config dict to location using YAML syntax.
:param config_dict: The dictionary to dump.
:param location: The location of the config file. Optional, defaults to LOCATION
:return: None
"""
os.makedirs(os.path.dirname(location), exist_ok=True)
with open(location, "w+") as stream:
yaml.safe_dump(config_dict, stream)


def create_default(location=LOCATION) -> None:
"""Create a config file at the correct location with the default values
:param location: The location of the config file. Optional, defaults to LOCATION
:return: None
"""
# Figure out where the default output dir should be
if not IS_UNIX:
# Get the location of the Downloads folder from the registry
# Yes this is long, windows is stupid. There's nothing I can do about it. Blame Microsoft.
# https://stackoverflow.com/a/48706260/13800487
sub_key = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
downloads_guid = "{374DE290-123F-4565-9164-39C4925E467B}"
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, sub_key) as key:
downloads_location = winreg.QueryValueEx(key, downloads_guid)[0]
# Actually set the output dir
output_dir = os.path.join(downloads_location, "redownloads")
elif IS_UNIX:
output_dir = os.path.join(os.getenv("HOME"), "Downloads", "redownloads")

default_config = {"output_dir": output_dir, "track_formats": [".flac", ".mp3"]}

# Save the default config to the file
dump(default_config, location=location)


def set_key(key: str, value: typing.Any, location=LOCATION, ignore_invalid: bool = False) -> None:
"""Sets specified key to equal specified in the config dict, and writes the changes
to the config file
:param key: The key to set.
:param value: The value to set the key to.
:param location: The location of the config file. Optional, defaults to LOCATION
:param ignore_invalid: If true, the function will add a key to the config file if it doesn't already exist.
Otherwise, will raise a KeyError. Optional, defaults to False.
:return None:
"""
global config
if key not in config.keys() and ignore_invalid is False:
raise KeyError("The config key specified does not exist.")
else:
config[key] = value
dump(config, location=location)


def load(location=LOCATION) -> dict:
"""Load the config file from LOCATION and return it as a dict.
:param location: The location of the config file. Optional, defaults to LOCATION
:return: Config dict
"""
global config
if not os.path.exists(LOCATION):
create_default()
with open(location, "r") as stream:
loaded_config = yaml.safe_load(stream)
config = loaded_config
return loaded_config


# Load config
load()
20 changes: 14 additions & 6 deletions src/redownload_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import redownload


config = redownload.config.config


def main():
parser = argparse.ArgumentParser()
parser.add_argument(
Expand All @@ -22,19 +25,24 @@ def main():
if args.version:
print(f"Build Version: {redownload.version.build_version}")
sys.exit()
# Non-Interactive Mode
elif args.url is not None:
if args.output_dir is not None:
output_dir = args.output_dir
else:
output_dir = os.path.join(os.getcwd(), "redownloads")
else: # Use the default output dir
output_dir = None
# Interactive Mode
else:
url = input("Enter the Relisten or Archive.org URL to download the tracks from: ")
output_dir = input(
"Enter the path to save the tracks to (ENTER for default [./redownlaods]): "
f"Enter the path to save the tracks to (ENTER for default [{config['output_dir']}]): "
)
if not output_dir:
output_dir = os.path.join(os.getcwd(), "redownloads")
redownload.redownload(url, [".flac", ".mp3"], output_dir)
if not output_dir: # Use the default output dir
output_dir = None
if output_dir:
redownload.redownload(url, output_dir=output_dir)
else:
redownload.redownload(url)


if __name__ == "__main__":
Expand Down

0 comments on commit d5a6358

Please sign in to comment.