-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
169 lines (154 loc) · 4.94 KB
/
Copy pathclient.py
File metadata and controls
169 lines (154 loc) · 4.94 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- Coding with utf-8 -*- #
# -*- Developed by Harryjin -*- #
import requests
# Information to search
def get_information():
cat_choice = ('pol', 'art', 'tech', 'trivia', '*')
reg_choice = ('uk', 'eu', 'w', '*')
category = '*'
region = '*'
while True:
temp = input("Choices of the category: \n1) Politics \n2) Arts \n3) New Technology \n4) Trivia \n5) All \nEnter your choice: ")
try:
category = cat_choice[int(temp)-1]
break
except Exception as e:
print("Invalid Input! Please try again!")
while True:
temp = input("Choices of the region: \n1) United Kingdom \n2) Europe \n3) World \n4) All \nEnter your choice: ")
try:
region = reg_choice[int(temp)-1]
break
except Exception as e:
print("Invalid Input! Please try again!")
return category, region
# Information to post
def get_information_post():
cat_choice = ('pol', 'art', 'tech', 'trivia')
reg_choice = ('uk', 'eu', 'w')
category = ''
region = ''
while True:
temp = input("Choices of the category: \n1) Politics \n2) Arts \n3) New Technology \n4) Trivia \nEnter your choice: ")
try:
category = cat_choice[int(temp)-1]
break
except Exception as e:
print("Invalid Input! Please try again!")
while True:
temp = input("Choices of the region: \n1) United Kingdom \n2) Europe \n3) World \nEnter your choice: ")
try:
region = reg_choice[int(temp)-1]
break
except Exception as e:
print("Invalid Input! Please try again!")
return category, region
'''
User Login
Param: login url
Return: user token or error message
'''
def login(url):
username = input("Enter your username: ")
password = input("Enter your password: ")
data = {"username": username, "password": password}
response = requests.post(url=f"{url}/api/login/", data=data)
if response.status_code == 200:
print(response.json()['msg'])
token = response.json()['token']
print(f'Your token is: {token}')
return token
else:
print("Login failed!")
print(response.content)
return ''
'''
User Logout
Param: URL and user token
Return: Response message
'''
def logout(url, token):
response = requests.post(f"{url}/api/logout/", data={'token': token})
if response.status_code == 200:
print("Logout successful!")
else:
print("Logout failed!")
'''
Post new story / news
Param: URL and user token
Return: Response message
'''
def post_story(url, token):
title = input("Enter the story title: ")
category, region = get_information_post()
details = input("Enter the story details: ")
response = requests.post(f"{url}/api/stories/", data={"title": title, "category": category, "region": region, "details": details, 'token': token})
if response.status_code == 201:
print("Story posted successfully!")
else:
print("Failed to post story!")
print(response.content)
'''
List news / stories
Param: URL
Return: Stories / Response message
'''
def get_stories(url):
category, region = get_information()
date = input("Enter the date you want to search (* for all): ")
response = requests.get(f"{url}/api/stories/", data={"story_cat": category, "story_reg": region, "story_date": date})
if response.status_code == 200:
stories = response.json()
for story in stories:
print(story)
else:
print("Failed to get stories!")
print(response.content)
'''
List news agencies
Param: URL
Return: News Agencies / Response message
'''
def list_agencies(url):
response = requests.get(f"{url}/api/directory/")
if response.status_code == 200:
agencies = response.json()
for agency in agencies:
print(agency)
else:
print("Failed to list agencies!")
print(response.content)
'''
Delete stories
Param: URL and user token
Return: Response message
'''
def delete_story(url, story_key, token):
response = requests.delete(f"{url}/api/stories/{story_key}/", data={'token': token})
if response.status_code == 200:
print("Story deleted successfully!")
else:
print("Failed to delete story!")
print(response.content)
def main():
url = "https://example.com/"
token = ''
while True:
command = input("Enter a command: ")
if command == "login":
token = login(url)
elif command == "logout":
logout(url, token)
elif command == "post":
post_story(url, token)
elif command == "news":
get_stories(url)
elif command == "list":
list_agencies(url)
elif command == "delete":
story_key = input("Enter the story key: ")
delete_story(url, story_key, token)
else:
print("Unknown command!")
if __name__ == "__main__":
main()