-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from balbi-uff/feature/workable_by_terminal
Feature/workable by terminal
- Loading branch information
Showing
8 changed files
with
1,501 additions
and
31 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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
**-workspace | ||
*.pytest** | ||
**/test_cases/* | ||
**/.idea/* |
This file was deleted.
Oops, something went wrong.
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,136 @@ | ||
import sys | ||
|
||
from scrapper.scrapper_methods import download_files_from_thread | ||
|
||
MINIMUM_ARGUMENTS_THRESHOLD = 1 | ||
|
||
|
||
def get_arguments_from_command_line(): | ||
""" | ||
Gets arguments from command line and returns them as a list. | ||
""" | ||
return sys.argv | ||
|
||
|
||
def is_manual_mode_trigger(argument): | ||
return argument.startswith("-m") | ||
|
||
|
||
def has_minimum_argument_thresold(arguments): | ||
return len(arguments) >= MINIMUM_ARGUMENTS_THRESHOLD | ||
|
||
|
||
def manual_resolution_setting_simple(arguments): | ||
""" | ||
Checks if resolution is set in manual mode - simple. | ||
""" | ||
return "--resolution" in arguments | ||
|
||
|
||
def manual_resolution_setting_full_config(arguments): | ||
""" | ||
Checks if resolution is set in manual mode - full config and not in simple. | ||
""" | ||
return ("--max-res" in arguments or "--min-res" in arguments) and not manual_resolution_setting_simple(arguments) | ||
|
||
|
||
def get_resolution_from_arguments_standart(res_arguments, resolution_trigger_str): | ||
""" | ||
Abstract method for getting resolution from arguments. | ||
""" | ||
resolution_trigger_index = res_arguments.index(resolution_trigger_str) | ||
x_resolution = int(res_arguments[resolution_trigger_index + 1]) | ||
y_resolution = int(res_arguments[resolution_trigger_index + 2]) | ||
return x_resolution, y_resolution | ||
|
||
|
||
def get_resolution_from_arguments_simple(res_arguments): | ||
""" | ||
Gets resolution from arguments. | ||
""" | ||
return get_resolution_from_arguments_standart(res_arguments, "--resolution") | ||
|
||
|
||
def define_resolution_full_config(res_args, resolution_trigger_str): | ||
# Obtain minimum resolution | ||
if resolution_trigger_str in res_args: | ||
x, y = get_resolution_from_arguments_standart(res_args, resolution_trigger_str) | ||
else: | ||
x, y = None, None | ||
return x, y | ||
|
||
|
||
def get_resolution_from_arguments_full_config(res_arguments): | ||
""" | ||
Gets resolution from arguments. | ||
""" | ||
|
||
# Obtain minimum resolution | ||
min_x_res, min_y_res = define_resolution_full_config(res_arguments, "--min-res") | ||
|
||
# Obtain maximum resolution | ||
max_x_res, max_y_res = define_resolution_full_config(res_arguments, "--max-res") | ||
|
||
return min_x_res, min_y_res, max_x_res, max_y_res | ||
|
||
|
||
def manual_mode_download(arguments): | ||
""" | ||
Manual mode is triggered when -m is passed as argument. | ||
Expected arguments: | ||
# Full command example: | ||
$ python FchanScrapper.py -m thread_link download_path --resolution 1920 1080 | ||
[0] [1] [2] [3] [4] [5] [6] | ||
$ python FchanScrapper.py -m thread_link download_path --min-res 1920 1080 --max-res 1920 1080 | ||
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] | ||
""" | ||
|
||
manual_mode_arguments = arguments[2:] | ||
if manual_resolution_setting_simple(manual_mode_arguments): | ||
x_resolution, y_resolution = get_resolution_from_arguments_simple(manual_mode_arguments) | ||
print("Resolution limits set to: " + str(x_resolution) + "x" + str(y_resolution)) | ||
|
||
return download_files_from_thread(manual_mode_arguments[0], manual_mode_arguments[1], | ||
min_x_res=x_resolution, min_y_res=y_resolution, | ||
max_x_res=None, max_y_res=None | ||
) | ||
if manual_resolution_setting_full_config(manual_mode_arguments): | ||
[min_x, min_y, max_x, max_y] = get_resolution_from_arguments_full_config(manual_mode_arguments) | ||
print("minimum accepted resolution: " + str(min_x) + "x" + str(min_y)) | ||
print("maximum accepted resolution: " + str(max_x) + "x" + str(max_y)) | ||
|
||
return download_files_from_thread(manual_mode_arguments[0], manual_mode_arguments[1], | ||
min_x_res=min_x, min_y_res=min_y, | ||
max_x_res=max_x, max_y_res=max_y | ||
) | ||
else: | ||
raise Exception("Resolution not set. Please set resolution with --resolution <resolution-x> <resolution-y> or " | ||
"type -h for help.") | ||
|
||
|
||
if __name__ == '__main__': | ||
command_line_arguments = get_arguments_from_command_line() | ||
try: | ||
if is_manual_mode_trigger(command_line_arguments[1]): | ||
manual_mode_download(command_line_arguments) | ||
elif has_minimum_argument_thresold(command_line_arguments): | ||
thread_link = command_line_arguments[1] | ||
|
||
if len(command_line_arguments) == 2: | ||
download_path = None | ||
else: | ||
download_path = command_line_arguments[2] | ||
download_files_from_thread(thread_link, download_path) | ||
else: | ||
raise Exception("Not enough arguments.") | ||
sys.exit(0) | ||
except IndexError as e: | ||
print("Please insert arguments correctly while on manual mode.") | ||
|
||
except Exception as e: | ||
print(e) | ||
|
||
sys.exit(1) |
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,31 +1,55 @@ | ||
import shutil | ||
|
||
import pytest | ||
import sys, os | ||
from shutil import rmtree | ||
|
||
projects_test_directory = sys.path[0] | ||
projects_root = projects_test_directory + "/.." | ||
temp_dir_name = "$temp" | ||
temp_dir_path = projects_test_directory + "/test_cases/" + temp_dir_name | ||
|
||
|
||
def remove_recursive(path): | ||
if os.path.isdir(path) and not os.path.islink(path): | ||
shutil.rmtree(path) | ||
elif os.path.exists(path): | ||
os.remove(path) | ||
|
||
|
||
@pytest.fixture() | ||
def setup_and_teardown_at_temp_dir(): | ||
# setup | ||
os.chdir(projects_test_directory + "/test_cases") | ||
os.mkdir(temp_dir_name) | ||
os.chdir(temp_dir_name) | ||
os.mkdir(temp_dir_path) | ||
os.chdir(temp_dir_path) | ||
|
||
# test | ||
yield | ||
|
||
# teardown | ||
os.chdir("..") | ||
rmtree(f"{temp_dir_name}") | ||
remove_recursive(f"{temp_dir_path}") | ||
|
||
|
||
@pytest.fixture() | ||
def setup_at_battleship_thread_dir(): | ||
def setup_for_thread_name_at_battleship_thread_dir(): | ||
# setup | ||
os.chdir(projects_test_directory + "/test_cases/battleship") | ||
|
||
# test | ||
yield | ||
|
||
|
||
@pytest.fixture() | ||
def setup_and_teardown_at_battleship_thread_dir(): | ||
# setup | ||
os.chdir(projects_root) | ||
# create temp directory | ||
os.mkdir(temp_dir_path) | ||
|
||
# test | ||
yield | ||
|
||
# teardown | ||
os.chdir("..") | ||
remove_recursive(temp_dir_path) |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.