-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
57 lines (45 loc) · 1.46 KB
/
bot.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
import os
import tweepy
import requests
import json
from dotenv import load_dotenv
import time
load_dotenv() #loads all the .env variables at the start.
def get_quote():
pp = requests.get("https://api.quotable.io/random")
if pp.status_code == 200:
return json.loads(pp.text) # all fine
elif pp.status_code == 404:
raise Exception("Invalid input or quote not found")
else:
raise Exception("Side Down")
# raw_quote = get_quote()
# print(type(raw_quote["tags"]))
def __make_quote_helper():
raw_quote = get_quote()
tags = " "
quote = str(raw_quote["content"]) + " - " + str(raw_quote["author"]) + ". Tags- " + tags.join(raw_quote["tags"])
return quote
def make_quote():
quote = __make_quote_helper()
if len(quote)>=250 and quote<=0:
make_quote()
else:
return quote
def authorization():
try:
client = tweepy.Client(
# OAuth 1.0a
consumer_key=os.getenv("API_KEY"),
consumer_secret=os.getenv("API_SECRET"),
access_token=os.getenv("ACCESS_TOKEN"),
access_token_secret=os.getenv("ACCESS_TOKEN_SECRET"),
# OAuth 2.0
bearer_token=os.getenv("BEARER_TOKEN")
)
return client
except Exception as e:
print("client authorization Failed: ", e)
return 1
def post_to_twitter(client, quote):
client.create_tweet(text=quote)