-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Updated TODO list * Added tests for the webserver * refactored webserver to use infohashes * tests * Added cli flags for server mode * Removed unused import * Added port env var * Added 404 response * Updated README
- Loading branch information
1 parent
32cfddd
commit 07b7153
Showing
15 changed files
with
248 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,3 +133,4 @@ cython_debug/ | |
|
||
.env | ||
tests/support/torrents/example.torrent | ||
scratchpad.md |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +0,0 @@ | ||
## TODO: | ||
|
||
- reintroduce `PTH` as an alternate source for RED | ||
- Update docker file | ||
- add `ignore-ops` and `ignore-red` flags (might need clearer name) | ||
- Add ability to call with single torrent files (ie: not a dirscan) | ||
- Add webserver to allow for calls to be sent to the container | ||
- Add direct injection perhaps? | ||
- Optionally read secrets from ENV vars OR Specify secrets file location | ||
- See if I can store metadata in the torrent file to indicate the torrent was generated by this app (and therefor can be ignored) | ||
- Alternately, see if I can screen out torrenets that have already been generated via infohash | ||
- Add a linter | ||
- add build artifacts to release | ||
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
bencoder | ||
colorama | ||
requests | ||
flask | ||
pytest | ||
requests-mock | ||
ruff |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import os | ||
from flask import Flask, request | ||
|
||
from src.parser import is_valid_infohash | ||
from src.torrent import generate_new_torrent_from_file | ||
from src.errors import TorrentAlreadyExistsError, TorrentNotFoundError | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
@app.route("/api/webhook", methods=["POST"]) | ||
def webhook(): | ||
config = app.config | ||
request_form = request.form.to_dict() | ||
infohash = request_form.get("infohash") | ||
# NOTE: always ensure safety checks are done before this filepath is ever used | ||
filepath = f"{config['input_dir']}/{infohash}.torrent" | ||
|
||
if infohash is None: | ||
return http_error("Request must include an 'infohash' parameter", 400) | ||
if not is_valid_infohash(infohash): | ||
return http_error("Invalid infohash", 400) | ||
if not os.path.exists(filepath): | ||
return http_error(f"No torrent found at {filepath}", 404) | ||
|
||
try: | ||
_, new_filepath = generate_new_torrent_from_file( | ||
filepath, | ||
config["output_dir"], | ||
config["red_api"], | ||
config["ops_api"], | ||
) | ||
|
||
return http_success(new_filepath, 201) | ||
except TorrentAlreadyExistsError as e: | ||
return http_error(str(e), 409) | ||
except TorrentNotFoundError as e: | ||
return http_error(str(e), 404) | ||
except Exception as e: | ||
return http_error(str(e), 500) | ||
|
||
|
||
@app.errorhandler(404) | ||
def page_not_found(_e): | ||
return http_error("Not found", 404) | ||
|
||
|
||
def http_success(message, code): | ||
return {"status": "success", "message": message}, code | ||
|
||
|
||
def http_error(message, code): | ||
return {"status": "error", "message": message}, code | ||
|
||
|
||
def run_webserver(input_dir, output_dir, red_api, ops_api, host="0.0.0.0", port=9713): | ||
app.config.update( | ||
{ | ||
"input_dir": input_dir, | ||
"output_dir": output_dir, | ||
"red_api": red_api, | ||
"ops_api": ops_api, | ||
} | ||
) | ||
|
||
app.run(debug=False, host=host, port=port) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
import os | ||
import shutil | ||
|
||
|
||
def get_torrent_path(name): | ||
return f"tests/support/torrents/{name}.torrent" | ||
|
||
|
||
class SetupTeardown: | ||
TORRENT_SUCCESS_RESPONSE = {"status": "success", "response": {"torrent": {"filePath": "foo", "id": 123}}} | ||
TORRENT_KNOWN_BAD_RESPONSE = {"status": "failure", "error": "bad hash parameter"} | ||
TORRENT_UNKNOWN_BAD_RESPONSE = {"status": "failure", "error": "unknown error"} | ||
ANNOUNCE_SUCCESS_RESPONSE = {"status": "success", "response": {"passkey": "bar"}} | ||
|
||
def setup_method(self): | ||
for f in os.listdir("/tmp"): | ||
if f.endswith(".torrent"): | ||
os.remove(os.path.join("/tmp", f)) | ||
|
||
os.makedirs("/tmp/input", exist_ok=True) | ||
os.makedirs("/tmp/output", exist_ok=True) | ||
|
||
def teardown_method(self): | ||
pass | ||
shutil.rmtree("/tmp/input", ignore_errors=True) | ||
shutil.rmtree("/tmp/output", ignore_errors=True) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.