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
|
import json
import discord
import requests
import tweepy
from discord.ext import commands, tasks
class StreamListener(tweepy.StreamListener):
def __init__(self):
with open("./config.json") as f:
self.config = json.load(f)
def on_error(self, status_code: int) -> bool:
if status_code == 420:
print("Rate limit reached. ")
# returning False in on_error disconnects the stream
return False
def on_data(self, data):
data = json.loads(data)
try:
tweetUser = data["tweet"]["user"]["screen_name"]
tweetID = data["tweet"]["id_str"]
except:
tweetUser = data["user"]["screen_name"]
tweetID = data["id_str"]
tweetLink = f"https://twitter.com/{tweetUser}/status/{tweetID}"
body = {"content": tweetLink}
global config
r = requests.post(
self.config["574267523869179904"]["tweetWebhook"],
headers={"Content-Type": "application/json"},
data=json.dumps(body),
) # config['574267523869179904']['tweetWebhook'], data=json.dumps(body))
print(r.status_code)
print(r.text)
# print(json.dumps(data, indent='\t'))
class Twitter(commands.Cog):
def __init__(self, bot):
self.bot = bot
auth = tweepy.OAuthHandler(
self.bot.config["twitter"]["consumer_key"],
self.bot.config["twitter"]["consumer_secret"],
)
auth.set_access_token(
self.bot.config["twitter"]["access_token"],
self.bot.config["twitter"]["access_token_secret"],
)
api = tweepy.API(auth)
myStreamListener = StreamListener()
stream = tweepy.Stream(auth=api.auth, listener=myStreamListener)
stream.filter(follow=["1287799985040437254"], is_async=True)
def setup(bot):
bot.add_cog(Twitter(bot))
|