-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuoter.py
67 lines (52 loc) · 1.76 KB
/
Quoter.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
#from threading import Timer
import requests, datetime, os
def saveKey(key):
with open('api_data.txt', 'w') as f:
f.write(key)
f.close()
def getKey():
if os.path.isfile("api_data.txt"):
with open("api_data.txt", "r") as f:
text = f.read()
f.close()
return text
else:
return ""
def updateBio(bio):
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': 'Bearer ' + api_key,
'X-GitHub-Api-Version': '2022-11-28',
'Content-Type': 'application/x-www-form-urlencoded',
}
data = str('{"bio": "' + bio + '"}').encode('utf-8')
response = requests.patch('https://api.github.com/user', headers=headers, data=data)
return response == 200
def fetchQuote():
request = requests.get('https://programming-quotesapi.vercel.app/api/random')
return request.json()
def styleQuote(quoteR):
author = quoteR['author']
quote = quoteR['quote']
return '`' + quote + '` - ' + author
input = input('Please enter your Github API key. (Leave empty if you want to use cached key)\n')
api_key = ''
if input == "":
key = getKey()
print(key)
if key != "":
api_key = key
else:
print("No api key cached.")
exit()
else:
api_key = input
saveKey(input)
quote = styleQuote(fetchQuote())
updateBio(quote)
print('New quote fetched: ' + quote)
# nextDay = datetime.datetime.now() + datetime.timedelta(days=1)
# dateString = nextDay.strftime('%d-%m-%Y') + " 01-00-00"
# newDate = nextDay.strptime(dateString,'%d-%m-%Y %H-%M-%S')
# delay = (newDate - datetime.datetime.now()).total_seconds()
# Timer(delay, updateBio(styleQuote(fetchQuote()))).start()