-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
83 lines (81 loc) · 3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import sys
import json
import webbrowser
from core.news_manager import News_Manager
from core.prefManager import PrefManager
from core.help_command import Help_Command
def commandHandler():
command = input(">>>")
if(command != None and len(command) > 0):
commandArray = command.split(" ")
if(commandArray[0] == "preview"):
if(len(commandArray) >= 3):
try:
story_number = int(commandArray[len(commandArray) - 1]) - 1
sourceName = " ".join(commandArray[1:len(commandArray)-1])
news.previewStory(sourceName, story_number)
except ValueError:
print("Error: invalid article number.")
else:
print("Too few arguments. Please use the format: " +
"preview source_name story_number")
elif(commandArray[0] == "open"):
if(len(commandArray) >= 3):
try:
story_number = int(commandArray[len(commandArray) - 1]) - 1
sourceName = " ".join(commandArray[1:len(commandArray)-1])
url = news.getStoryURL(sourceName, story_number)
if(not url is None):
webbrowser.open(url)
else:
print("Could not find a URL for that story.")
except ValueError:
print("Error: invalid article number.")
else:
print("Too few arguments. Please use the format: " +
"open source_name story_number")
elif(commandArray[0] == "add"):
if(len(commandArray) > 1):
prefManager.addSource(" ".join(commandArray[1:]))
else:
print("Too few arguments. Please use the format: " +
"add source")
elif(commandArray[0] == "remove"):
if(len(commandArray) > 1):
prefManager.remove_source(" ".join(commandArray[1:]))
else:
print("Too few arguments. Please use the format: " +
"remove source")
elif(commandArray[0] == "sps"):
if(len(commandArray) > 1 and len(commandArray) < 3):
try:
prefManager.set_stories_per_source(int(commandArray[1]))
except ValueError:
print("Error: Invalid number of stories. Please enter an integer greater than 0.")
else:
print("Invalid command. Please use the format sps storyNumber")
elif(commandArray[0] == "random"):
news.random_story()
elif(commandArray[0] == "refresh"):
#Can repost stories without calling API again by passing -local switch.
news.refresh(commandArray)
elif(commandArray[0] == "help"):
Help_Command.help_handler(commandArray)
#Refactor validation code from command handler to specific command handlers for readability.
#Refactor API key into raab_requests
#Generate preferences file if one is not found. Stops you propogating news sources via preferences to others.
#Write batch file to fire on startup, close after x time.
elif(commandArray[0] == "exit"):
sys.exit()
else:
print("Unknown command")
commandHandler()
else:
print("Unknown command")
commandHandler()
prefManager = PrefManager()
print("Welcome to the news! Thanks to newsapi.org for their lovely API!")
print("Please use the 'help' command for a full list and explanation of all commands!")
print("Loading news...")
news = News_Manager(prefManager)
commandHandler()