aboutsummaryrefslogtreecommitdiff
path: root/cogs/twitter.py
diff options
context:
space:
mode:
authorMango0x45 <thomasvoss@live.com>2020-09-12 13:13:29 +0000
committerMango0x45 <thomasvoss@live.com>2020-09-12 13:13:29 +0000
commit0baa36497519711953603db640bf099db19af826 (patch)
treea5e196ee48d6552894c683db2272de16f8d77c41 /cogs/twitter.py
parentfed0c6b1767f3eddbabed9cc28dd0f5615889811 (diff)
downloadsteve-bot-0baa36497519711953603db640bf099db19af826.tar
steve-bot-0baa36497519711953603db640bf099db19af826.tar.gz
steve-bot-0baa36497519711953603db640bf099db19af826.tar.bz2
steve-bot-0baa36497519711953603db640bf099db19af826.tar.lz
steve-bot-0baa36497519711953603db640bf099db19af826.tar.xz
steve-bot-0baa36497519711953603db640bf099db19af826.tar.zst
steve-bot-0baa36497519711953603db640bf099db19af826.zip
Black is neat
Diffstat (limited to 'cogs/twitter.py')
-rwxr-xr-xcogs/twitter.py81
1 files changed, 45 insertions, 36 deletions
diff --git a/cogs/twitter.py b/cogs/twitter.py
index 0688380..d633b8e 100755
--- a/cogs/twitter.py
+++ b/cogs/twitter.py
@@ -7,46 +7,55 @@ 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):
- 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'))
+ def __init__(self):
+ with open("./config.json") as f:
+ self.config = json.load(f)
+
+ def on_error(self, status_code):
+ 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
+ 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'])
+ 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)
- 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)) \ No newline at end of file
+ bot.add_cog(Twitter(bot))