Rearranged functions

This commit is contained in:
Ali Parlakci
2018-07-11 18:56:39 +03:00
parent 45191e2c60
commit d9dc3132f6

249
script.py
View File

@@ -210,28 +210,113 @@ def checkConflicts():
print("No redditor name given") print("No redditor name given")
quit() quit()
def postFromLog(fileName): class PromptUser():
"""Analyze a log file and return a list of dictionaries containing @staticmethod
submissions def chooseFrom(choices):
""" print()
if Path.is_file(Path(fileName)): choicesByIndex = list(str(x) for x in range(len(choices)+1))
content = jsonFile(fileName).read() for i in range(len(choices)):
else: print("{indent}[{order}] {mode}".format(
print("File not found") indent=" "*4,order=i+1,mode=choices[i]
quit() ))
print(" "*4+"[0] exit\n")
choice = input("> ")
while not choice.lower() in choices+choicesByIndex:
print("Invalid input\n")
programModeIndex = input("> ")
try: if choice == "0":
del content["HEADER"] quit()
except KeyError: elif choice in choicesByIndex:
pass return choices[int(choice)-1]
else:
return choice
def __init__(self):
print("select program mode:")
programModes = [
"search","subreddit","multireddit",
"submitted","upvoted","saved","log"
]
programMode = self.chooseFrom(programModes)
posts = [] if programMode == "search":
GLOBAL.arguments.search = input("\nquery: ")
GLOBAL.arguments.subreddit = input("\nsubreddit: ")
for post in content: print("\nselect sort type:")
if not content[post][-1]['postType'] == None: sortTypes = [
posts.append(content[post][-1]) "relevance","top","new"
]
sortType = self.chooseFrom(sortTypes)
GLOBAL.arguments.sort = sortType
return posts print("\nselect time filter:")
timeFilters = [
"hour","day","week","month","year","all"
]
timeFilter = self.chooseFrom(timeFilters)
GLOBAL.arguments.time = timeFilter
if programMode == "subreddit":
GLOBAL.arguments.subreddit = input("\nsubreddit: ")
if " " in GLOBAL.arguments.subreddit:
GLOBAL.arguments.subreddit = "+".join(GLOBAL.arguments.subreddit.split())
print("\nselect sort type:")
sortTypes = [
"hot","top","new","rising","controversial"
]
sortType = self.chooseFrom(sortTypes)
GLOBAL.arguments.sort = sortType
if sortType in ["top","controversial"]:
print("\nselect time filter:")
timeFilters = [
"hour","day","week","month","year","all"
]
timeFilter = self.chooseFrom(timeFilters)
GLOBAL.arguments.time = timeFilter
else:
GLOBAL.arguments.time = "all"
elif programMode == "multiredit":
GLOBAL.arguments.user = input("\nredditor: ")
GLOBAL.arguments.subreddit = input("\nmultireddit: ")
print("\nselect sort type:")
sortTypes = [
"hot","top","new","rising","controversial"
]
sortType = self.chooseFrom(sortTypes)
GLOBAL.arguments.sort = sortType
if sortType in ["top","controversial"]:
print("\nselect time filter:")
timeFilters = [
"hour","day","week","month","year","all"
]
timeFilter = self.chooseFrom(timeFilters)
GLOBAL.arguments.time = timeFilter
else:
GLOBAL.arguments.time = "all"
elif programMode == "submitted":
GLOBAL.arguments.submitted = True
GLOBAL.arguments.user = input("\nredditor: ")
elif programMode == "upvoted":
GLOBAL.arguments.upvoted = True
GLOBAL.arguments.user = input("\nredditor: ")
elif programMode == "saved":
GLOBAL.arguments.saved = True
GLOBAL.arguments.user = input("\nredditor: ")
elif programMode == "log":
GLOBAL.arguments.log = input("\nlog file directory:")
GLOBAL.arguments.limit = int(input("\nlimit: "))
def prepareAttributes(): def prepareAttributes():
ATTRIBUTES = {} ATTRIBUTES = {}
@@ -299,115 +384,28 @@ def prepareAttributes():
return ATTRIBUTES return ATTRIBUTES
class PromptUser(): def postFromLog(fileName):
@staticmethod """Analyze a log file and return a list of dictionaries containing
def chooseFrom(choices): submissions
print() """
choicesByIndex = list(str(x) for x in range(len(choices)+1)) if Path.is_file(Path(fileName)):
for i in range(len(choices)): content = jsonFile(fileName).read()
print("{indent}[{order}] {mode}".format( else:
indent=" "*4,order=i+1,mode=choices[i] print("File not found")
)) quit()
print(" "*4+"[0] exit\n")
choice = input("> ")
while not choice.lower() in choices+choicesByIndex:
print("Invalid input\n")
programModeIndex = input("> ")
if choice == "0": try:
quit() del content["HEADER"]
elif choice in choicesByIndex: except KeyError:
return choices[int(choice)-1] pass
else:
return choice
def __init__(self):
print("Select program mode:")
programModes = [
"search","subreddit","multireddit",
"submitted","upvoted","saved","log"
]
programMode = self.chooseFrom(programModes)
if programMode == "search": posts = []
GLOBAL.arguments.search = input("\nquery: ")
GLOBAL.arguments.subreddit = input("\nsubreddit: ")
print("\nSelect sort type:") for post in content:
sortTypes = [ if not content[post][-1]['postType'] == None:
"relevance","top","new" posts.append(content[post][-1])
]
sortType = self.chooseFrom(sortTypes)
GLOBAL.arguments.sort = sortType
print("\nSelect time filter:") return posts
timeFilters = [
"hour","day","week","month","year","all"
]
timeFilter = self.chooseFrom(timeFilters)
GLOBAL.arguments.time = timeFilter
if programMode == "subreddit":
GLOBAL.arguments.subreddit = input("\nsubreddit: ")
if " " in GLOBAL.arguments.subreddit:
GLOBAL.arguments.subreddit = "+".join(GLOBAL.arguments.subreddit.split())
print("\nSelect sort type:")
sortTypes = [
"hot","top","new","rising","controversial"
]
sortType = self.chooseFrom(sortTypes)
GLOBAL.arguments.sort = sortType
if sortType in ["top","controversial"]:
print("\nSelect time filter:")
timeFilters = [
"hour","day","week","month","year","all"
]
timeFilter = self.chooseFrom(timeFilters)
GLOBAL.arguments.time = timeFilter
else:
GLOBAL.arguments.time = "all"
elif programMode == "multiredit":
GLOBAL.arguments.user = input("\nredditor: ")
GLOBAL.arguments.subreddit = input("\nmultireddit: ")
print("\nSelect sort type:")
sortTypes = [
"hot","top","new","rising","controversial"
]
sortType = self.chooseFrom(sortTypes)
GLOBAL.arguments.sort = sortType
if sortType in ["top","controversial"]:
print("\nSelect time filter:")
timeFilters = [
"hour","day","week","month","year","all"
]
timeFilter = self.chooseFrom(timeFilters)
GLOBAL.arguments.time = timeFilter
else:
GLOBAL.arguments.time = "all"
elif programMode == "submitted":
GLOBAL.arguments.submitted = True
GLOBAL.arguments.user = input("\nredditor: ")
elif programMode == "upvoted":
GLOBAL.arguments.upvoted = True
GLOBAL.arguments.user = input("\nredditor: ")
elif programMode == "saved":
GLOBAL.arguments.saved = True
GLOBAL.arguments.user = input("\nredditor: ")
elif programMode == "log":
GLOBAL.arguments.log = input("\nlog file directory:")
GLOBAL.arguments.limit = int(input("\nlimit: "))
def postExists(POST): def postExists(POST):
"""Figure out a file's name and checks if the file already exists""" """Figure out a file's name and checks if the file already exists"""
@@ -595,9 +593,10 @@ def main():
if len(sys.argv) == 1: if len(sys.argv) == 1:
PromptUser() PromptUser()
GLOBAL.config = getConfig(Path(PurePath(__file__).parent / 'config.json')) else:
checkConflicts()
checkConflicts() GLOBAL.config = getConfig(Path(PurePath(__file__).parent / 'config.json'))
print(sys.argv) print(sys.argv)