2020-06-02 00:05:02 +12:00
|
|
|
import sys
|
2021-02-06 21:35:50 +13:00
|
|
|
from pathlib import Path
|
|
|
|
|
2021-02-07 14:05:18 +13:00
|
|
|
from bulkredditdownloader.errors import InvalidSortingType, ProgramModeError, RedditorNameError, SearchModeError
|
|
|
|
from bulkredditdownloader.parser import LinkDesigner
|
2021-02-07 01:29:13 +13:00
|
|
|
import argparse
|
2021-02-06 21:35:50 +13:00
|
|
|
|
2020-06-02 00:05:02 +12:00
|
|
|
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2020-06-02 00:05:02 +12:00
|
|
|
class ProgramMode:
|
|
|
|
|
2021-02-07 01:29:13 +13:00
|
|
|
def __init__(self, arguments: argparse.Namespace):
|
2020-06-02 00:05:02 +12:00
|
|
|
self.arguments = arguments
|
|
|
|
|
2021-02-07 01:29:13 +13:00
|
|
|
def generate(self) -> dict:
|
2020-06-02 00:05:02 +12:00
|
|
|
try:
|
|
|
|
self._validateProgramMode()
|
|
|
|
except ProgramModeError:
|
|
|
|
self._promptUser()
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode = {}
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
if self.arguments.user is not None:
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["user"] = self.arguments.user
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
if self.arguments.search is not None:
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["search"] = self.arguments.search
|
2020-06-02 00:05:02 +12:00
|
|
|
if self.arguments.sort == "hot" or \
|
2021-02-25 22:32:06 +13:00
|
|
|
self.arguments.sort == "controversial" or \
|
|
|
|
self.arguments.sort == "rising":
|
2020-06-02 00:05:02 +12:00
|
|
|
self.arguments.sort = "relevance"
|
|
|
|
|
|
|
|
if self.arguments.sort is not None:
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["sort"] = self.arguments.sort
|
2020-06-02 00:05:02 +12:00
|
|
|
else:
|
|
|
|
if self.arguments.submitted:
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["sort"] = "new"
|
2020-06-02 00:05:02 +12:00
|
|
|
else:
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["sort"] = "hot"
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
if self.arguments.time is not None:
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["time"] = self.arguments.time
|
2020-06-02 00:05:02 +12:00
|
|
|
else:
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["time"] = "all"
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
if self.arguments.link is not None:
|
|
|
|
self.arguments.link = self.arguments.link.strip("\"")
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode = LinkDesigner(self.arguments.link)
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
if self.arguments.search is not None:
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["search"] = self.arguments.search
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
if self.arguments.sort is not None:
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["sort"] = self.arguments.sort
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
if self.arguments.time is not None:
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["time"] = self.arguments.time
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
elif self.arguments.subreddit is not None:
|
2021-02-25 22:32:06 +13:00
|
|
|
if isinstance(self.arguments.subreddit, list):
|
2020-06-02 00:05:02 +12:00
|
|
|
self.arguments.subreddit = "+".join(self.arguments.subreddit)
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["subreddit"] = self.arguments.subreddit
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
elif self.arguments.multireddit is not None:
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["multireddit"] = self.arguments.multireddit
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
elif self.arguments.saved is True:
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["saved"] = True
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
elif self.arguments.upvoted is True:
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["upvoted"] = True
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
elif self.arguments.submitted is not None:
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["submitted"] = True
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
if self.arguments.sort == "rising":
|
|
|
|
raise InvalidSortingType("Invalid sorting type has given")
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
program_mode["limit"] = self.arguments.limit
|
2020-06-02 00:05:02 +12:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
return program_mode
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
@staticmethod
|
2021-02-07 01:29:13 +13:00
|
|
|
def _chooseFrom(choices: list[str]):
|
2020-06-02 00:05:02 +12:00
|
|
|
print()
|
2021-02-06 21:35:50 +13:00
|
|
|
choices_by_index = list(str(x) for x in range(len(choices) + 1))
|
2020-06-02 00:05:02 +12:00
|
|
|
for i in range(len(choices)):
|
2021-02-06 21:35:50 +13:00
|
|
|
print("{indent}[{order}] {mode}".format(indent=" " * 4, order=i + 1, mode=choices[i]))
|
2021-02-25 22:32:06 +13:00
|
|
|
print(" " * 4 + "[0] exit\n")
|
2020-06-02 00:05:02 +12:00
|
|
|
choice = input("> ")
|
2021-02-06 21:35:50 +13:00
|
|
|
while not choice.lower() in choices + choices_by_index + ["exit"]:
|
2020-06-02 00:05:02 +12:00
|
|
|
print("Invalid input\n")
|
|
|
|
input("> ")
|
|
|
|
|
|
|
|
if choice == "0" or choice == "exit":
|
|
|
|
sys.exit()
|
2021-02-06 21:35:50 +13:00
|
|
|
elif choice in choices_by_index:
|
2021-02-25 22:32:06 +13:00
|
|
|
return choices[int(choice) - 1]
|
2020-06-02 00:05:02 +12:00
|
|
|
else:
|
|
|
|
return choice
|
|
|
|
|
|
|
|
def _promptUser(self):
|
|
|
|
print("select program mode:")
|
2021-02-06 21:35:50 +13:00
|
|
|
program_modes = ["search", "subreddit", "multireddit", "submitted", "upvoted", "saved", "log"]
|
|
|
|
program_mode = self._chooseFrom(program_modes)
|
2020-06-02 00:05:02 +12:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
if program_mode == "search":
|
2020-06-02 00:05:02 +12:00
|
|
|
self.arguments.search = input("\nquery: ")
|
|
|
|
self.arguments.subreddit = input("\nsubreddit: ")
|
|
|
|
|
|
|
|
print("\nselect sort type:")
|
2021-02-06 21:35:50 +13:00
|
|
|
sort_types = ["relevance", "top", "new"]
|
|
|
|
sort_type = self._chooseFrom(sort_types)
|
|
|
|
self.arguments.sort = sort_type
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
print("\nselect time filter:")
|
2021-02-06 21:35:50 +13:00
|
|
|
time_filters = ["hour", "day", "week", "month", "year", "all"]
|
|
|
|
time_filter = self._chooseFrom(time_filters)
|
|
|
|
self.arguments.time = time_filter
|
2020-06-02 00:05:02 +12:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
if program_mode == "subreddit":
|
|
|
|
subreddit_input = input("(type frontpage for all subscribed subreddits,\n"
|
|
|
|
" use plus to seperate multi subreddits:"
|
|
|
|
" pics+funny+me_irl etc.)\n\n"
|
|
|
|
"subreddit: ")
|
|
|
|
self.arguments.subreddit = subreddit_input
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
if " " in self.arguments.subreddit:
|
2021-02-25 22:32:06 +13:00
|
|
|
self.arguments.subreddit = "+".join(
|
|
|
|
self.arguments.subreddit.split())
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
# DELETE THE PLUS (+) AT THE END
|
2021-02-06 21:35:50 +13:00
|
|
|
if not subreddit_input.lower() == "frontpage" and self.arguments.subreddit[-1] == "+":
|
2020-06-02 00:05:02 +12:00
|
|
|
self.arguments.subreddit = self.arguments.subreddit[:-1]
|
|
|
|
|
|
|
|
print("\nselect sort type:")
|
2021-02-06 21:35:50 +13:00
|
|
|
sort_types = ["hot", "top", "new", "rising", "controversial"]
|
|
|
|
sort_type = self._chooseFrom(sort_types)
|
|
|
|
self.arguments.sort = sort_type
|
2020-06-02 00:05:02 +12:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
if sort_type in ["top", "controversial"]:
|
2020-06-02 00:05:02 +12:00
|
|
|
print("\nselect time filter:")
|
2021-02-06 21:35:50 +13:00
|
|
|
time_filters = ["hour", "day", "week", "month", "year", "all"]
|
|
|
|
time_filter = self._chooseFrom(time_filters)
|
|
|
|
self.arguments.time = time_filter
|
2020-06-02 00:05:02 +12:00
|
|
|
else:
|
|
|
|
self.arguments.time = "all"
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
elif program_mode == "multireddit":
|
2020-06-02 00:05:02 +12:00
|
|
|
self.arguments.user = input("\nmultireddit owner: ")
|
|
|
|
self.arguments.multireddit = input("\nmultireddit: ")
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2020-06-02 00:05:02 +12:00
|
|
|
print("\nselect sort type:")
|
2021-02-06 21:35:50 +13:00
|
|
|
sort_types = ["hot", "top", "new", "rising", "controversial"]
|
|
|
|
sort_type = self._chooseFrom(sort_types)
|
|
|
|
self.arguments.sort = sort_type
|
2020-06-02 00:05:02 +12:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
if sort_type in ["top", "controversial"]:
|
2020-06-02 00:05:02 +12:00
|
|
|
print("\nselect time filter:")
|
2021-02-06 21:35:50 +13:00
|
|
|
time_filters = ["hour", "day", "week", "month", "year", "all"]
|
|
|
|
time_filter = self._chooseFrom(time_filters)
|
|
|
|
self.arguments.time = time_filter
|
2020-06-02 00:05:02 +12:00
|
|
|
else:
|
|
|
|
self.arguments.time = "all"
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
elif program_mode == "submitted":
|
2020-06-02 00:05:02 +12:00
|
|
|
self.arguments.submitted = True
|
|
|
|
self.arguments.user = input("\nredditor: ")
|
|
|
|
|
|
|
|
print("\nselect sort type:")
|
2021-02-06 21:35:50 +13:00
|
|
|
sort_types = ["hot", "top", "new", "controversial"]
|
|
|
|
sort_type = self._chooseFrom(sort_types)
|
|
|
|
self.arguments.sort = sort_type
|
2020-06-02 00:05:02 +12:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
if sort_type == "top":
|
2020-06-02 00:05:02 +12:00
|
|
|
print("\nselect time filter:")
|
2021-02-06 21:35:50 +13:00
|
|
|
time_filters = ["hour", "day", "week", "month", "year", "all"]
|
|
|
|
time_filter = self._chooseFrom(time_filters)
|
|
|
|
self.arguments.time = time_filter
|
2020-06-02 00:05:02 +12:00
|
|
|
else:
|
|
|
|
self.arguments.time = "all"
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
elif program_mode == "upvoted":
|
2020-06-02 00:05:02 +12:00
|
|
|
self.arguments.upvoted = True
|
|
|
|
self.arguments.user = input("\nredditor: ")
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
elif program_mode == "saved":
|
2020-06-02 00:05:02 +12:00
|
|
|
self.arguments.saved = True
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
elif program_mode == "log":
|
2020-06-02 00:05:02 +12:00
|
|
|
while True:
|
|
|
|
self.arguments.log = input("\nlog file directory:")
|
|
|
|
if Path(self.arguments.log).is_file():
|
2021-02-25 22:32:06 +13:00
|
|
|
break
|
2020-06-02 00:05:02 +12:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
self.arguments.limit = int(input("\nlimit (0 for none): "))
|
|
|
|
if self.arguments.limit == 0:
|
|
|
|
self.arguments.limit = None
|
|
|
|
break
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _validateProgramMode(self):
|
|
|
|
"""Check if command-line self.arguments are given correcly,
|
|
|
|
if not, raise errors
|
|
|
|
"""
|
|
|
|
if self.arguments.user is None:
|
|
|
|
user = 0
|
|
|
|
else:
|
|
|
|
user = 1
|
|
|
|
|
|
|
|
search = 1 if self.arguments.search else 0
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
modes = ["saved", "subreddit", "submitted", "log", "link", "upvoted", "multireddit"]
|
|
|
|
|
|
|
|
values = {x: 0 if getattr(self.arguments, x) is None or
|
|
|
|
getattr(self.arguments, x) is False
|
|
|
|
else 1
|
|
|
|
for x in modes
|
|
|
|
}
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
|
|
if not sum(values[x] for x in values) == 1:
|
|
|
|
raise ProgramModeError("Invalid program mode")
|
2021-02-25 22:32:06 +13:00
|
|
|
|
|
|
|
if search + values["saved"] == 2:
|
2020-06-02 00:05:02 +12:00
|
|
|
raise SearchModeError("You cannot search in your saved posts")
|
|
|
|
|
2021-02-25 22:32:06 +13:00
|
|
|
if search + values["submitted"] == 2:
|
2020-06-02 00:05:02 +12:00
|
|
|
raise SearchModeError("You cannot search in submitted posts")
|
|
|
|
|
2021-02-25 22:32:06 +13:00
|
|
|
if search + values["upvoted"] == 2:
|
2020-06-02 00:05:02 +12:00
|
|
|
raise SearchModeError("You cannot search in upvoted posts")
|
|
|
|
|
2021-02-25 22:32:06 +13:00
|
|
|
if search + values["log"] == 2:
|
2020-06-02 00:05:02 +12:00
|
|
|
raise SearchModeError("You cannot search in log files")
|
|
|
|
|
2021-02-25 22:32:06 +13:00
|
|
|
if values["upvoted"] + values["submitted"] == 1 and user == 0:
|
2020-06-02 00:05:02 +12:00
|
|
|
raise RedditorNameError("No redditor name given")
|