mirror of
https://github.com/metafy-social/python-scripts.git
synced 2024-11-27 14:01:12 +00:00
Adding Twitter Bot program
This commit is contained in:
parent
618f2fd966
commit
ff6a82c8dc
15
scripts/twitter_streamer_bot/README.md
Normal file
15
scripts/twitter_streamer_bot/README.md
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
# TWITTER BOT
|
||||||
|
|
||||||
|
This is a Twitter bot, it will like and retweet your tweet, containing some keyword and will print the url of tweet.
|
||||||
|
<br>
|
||||||
|
Here I used the twython library to use Twitter API, this library can be used to all the things mentioned in Twitter API document.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
Check [requirements.txt](requirements.txt), for twython check [here](https://twython.readthedocs.io/en/latest/usage/install.html)
|
||||||
|
Generate your unique Twitter keys and token from [here](https://developer.twitter.com/en) by creating a [new app](https://developer.twitter.com/en/apps).
|
||||||
|
|
||||||
|
## How to run the script
|
||||||
|
Update the keys and tokens in code.
|
||||||
|
Run the python file and enter the keyword you want to track, now any new tweet will automatically liked and retweeted.
|
||||||
|
It will print the link of tweet<br>
|
29
scripts/twitter_streamer_bot/code.py
Normal file
29
scripts/twitter_streamer_bot/code.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
from twython import Twython
|
||||||
|
from twython import TwythonStreamer
|
||||||
|
import configparser
|
||||||
|
|
||||||
|
|
||||||
|
class MyStreamer(TwythonStreamer):
|
||||||
|
# Overwriting function
|
||||||
|
def on_success(self, data):
|
||||||
|
if 'text' in data:
|
||||||
|
username = data['user']['screen_name']
|
||||||
|
tweet_id = data['id']
|
||||||
|
# Liking the tweet found
|
||||||
|
st.create_favorite(id=tweet_id)
|
||||||
|
# Retweeting the tweet with msg
|
||||||
|
st.update_status(status=f'Nice Tweet @{username}', in_reply_to_status_id=tweet_id)
|
||||||
|
print(f"https://twitter.com/{username}/status/{str(tweet_id)}")
|
||||||
|
|
||||||
|
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read('config.ini')
|
||||||
|
api_key = config['keys']['api_key']
|
||||||
|
api_secret_key = config['keys']['api_secret_key']
|
||||||
|
access_token = config['keys']['access_token']
|
||||||
|
access_secret_token = config['keys']['access_secret_token']
|
||||||
|
|
||||||
|
api = MyStreamer(api_key, api_secret_key, access_token, access_secret_token)
|
||||||
|
st = Twython(api_key, api_secret_key, access_token, access_secret_token)
|
||||||
|
keyword = input("Enter keyword to track: ")
|
||||||
|
api.statuses.filter(track=keyword)
|
5
scripts/twitter_streamer_bot/config.ini
Normal file
5
scripts/twitter_streamer_bot/config.ini
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
[keys]
|
||||||
|
api_key=
|
||||||
|
api_secret_key=
|
||||||
|
access_token=
|
||||||
|
access_secret_token=
|
2
scripts/twitter_streamer_bot/requirements.txt
Normal file
2
scripts/twitter_streamer_bot/requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
configparser
|
||||||
|
twython
|
Loading…
Reference in New Issue
Block a user