-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPythonTwitteratiScript.py
More file actions
64 lines (47 loc) · 1.92 KB
/
PythonTwitteratiScript.py
File metadata and controls
64 lines (47 loc) · 1.92 KB
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
Update the below details in the .env file and add personal credentials...
# CONSUMER_KEY=YOURKEYGOESHERE
# CONSUMER_SECRET=YOURSECRETGOESHERE
# ACCESS_TOKEN=YOURACCESSTOKENGOESHERE
# ACCESS_SECRET=ACCESSSECRETGOESHERE
import tweepy
from decouple import config
auth = tweepy.OAuthHandler(config("CONSUMER_KEY"), config("CONSUMER_SECRET"))
auth.set_access_token(config("ACCESS_TOKEN"), config("ACCESS_SECRET"))
# API instance
api = tweepy.API(auth)
api.update_status("This is a test tweet using tweepy.")
BG_API_KEY="Your API Key"
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
username = status.user.screen_name
status_id = status.id
if ‘media’ in status.entities:
for image in status.entities['media']:
tweet_image(image['media_url'], username, status_id)
my_stream_listener = MyStreamListener()
stream = tweepy.Stream(auth, my_stream_listener)
stream.filter(track=['@saral_gyaan'])
import requests
from io import BytesIO
from PIL import Image
def tweet_image(url, username, status_id):
filename = 'temp.png'
response = requests.get(url, stream=True)
if response.status_code == 200:
i = Image.open(BytesIO(request.content))
i.save(filename)
remove_bg(filename)
api.update_with_media('no-bg.png', status=f'@{username}, Here is the picture without the background', in_reply_to_status_id=status_id)
else:
print("unable to download image")
def remove_bg(filename):
response = requests.post(
'https://api.remove.bg/v1.0/removebg',
files={'image_file': open(filename, 'rb')},
data={'size': 'auto'},
headers={'X-Api-Key': config('BG_API_KEY')},)
if response.status_code == requests.codes.ok:
with open('no-bg.png', 'wb') as out:
out.write(response.content)
else:
print("Error:", response.status_code, response.text)