-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetQuote.py
143 lines (115 loc) · 4.02 KB
/
GetQuote.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
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
import requests
import json
"""Documentation: https://github.com/lukePeavey/quotable
"""
class __QuoteStructure:
"""
Returns everything as string
content = returns content
author = returns author
authorSulg = returns authorSlug
id = returns id
length = returns length
tags = return tags
pointer = returns http pointer
request send and check helper function.
"""
def __init__(self):
self.URL = 'https://api.quotable.io/random'
def content(self):
return self.raw_quote["content"]
def author(self):
return self.raw_quote["author"]
def authorSlug(self):
return self.raw_quote["authorSlug"]
def id(self):
return self.raw_quote["_id"]
def length(self):
return self.raw_quote["length"]
def tags(self):
return self.raw_quote["tags"]
def pointer(self):
return self.pointerobject
def _sendRequestAndCheck(self, load=None):
if load == None: # for the Random() method without load.
pp = requests.get(self.URL)
else: # fetch with the load argument.
pp = requests.get(self.URL, load)
# check for status codes.
if pp.status_code == 200:
return pp # all fine
elif pp.status_code == 404:
raise Exception("Invalid input or quote not found")
else:
raise Exception("Side Down")
class Random(__QuoteStructure):
"""
Gets a Random quote from https://api.quotable.io/random
"""
def __init__(self):
self.URL = 'https://api.quotable.io/random'
self.fetchedURL = None
self.raw_quote = json.loads(self.__helper())
def __helper(self):
raw_data = self._sendRequestAndCheck()
self.fetchedURL = raw_data.url
return raw_data.text
class RandomTag(__QuoteStructure):
"""
Gets a random quote using tags and function.
Tag-> List of strings
Function-> "and", "or"
Note: Give a one string list for single Tag
"""
def __init__(self, tagslist, function=None):
self.URL = 'https://api.quotable.io/random'
self.raw_quote = ''
self.pointerobject = None
if len(tagslist) == 1 and function == None:
self.raw_quote = self.__helperSINGLE(tagslist[0])
elif len(tagslist) != 1 and function == "and":
self.raw_quote = self.__helperAND(tagslist)
elif len(tagslist) != 1 and function == "or":
self.raw_quote = self.__helperOR(tagslist)
else:
raise Exception("Invalid Input Arguments")
def __helperAND(self, tagslist):
strformat = ",".join(tagslist)
payload = {"tags": strformat}
raw = self._sendRequestAndCheck(payload)
self.pointerobject = raw
return json.loads(raw.text)
def __helperOR(self, tagslist):
strformat = "|".join(tagslist)
payload = {"tags": strformat}
raw = self._sendRequestAndCheck(payload)
self.pointerobject = raw
return json.loads(raw.text)
def __helperSINGLE(self, singletag):
payload = {"tags": singletag}
raw = self._sendRequestAndCheck(payload)
self.pointerobject = raw
return json.loads(raw.text)
class RandomLen(__QuoteStructure):
"""
Gets a random quote using length(int)
minlength, maxlength
"""
def __init__(self, minlength=None, maxlength=None):
self.URL = 'https://api.quotable.io/random'
self.raw_quote = ''
self.pointerobject = None
if maxlength == None and minlength == None:
raise Exception(
"No length specified. Please spepecify minlength or maxlength")
else:
self.raw_quote = self.__helper(minlength, maxlength)
def __helper(self, min, max):
payload = {"minLength": min, "maxLength": max}
raw = self._sendRequestAndCheck(payload)
self.pointerobject = raw
return json.loads(raw.text)
if __name__ == "__main__":
r = RandomLen(50, 100)
print(r.pointer().url)
print(r.content())