A Python package for defending against harassment on Twitter.
Python 3.9+
Install directly from github
#if you are using python3.9, for example
python3.9 -m pip install --user git+https://github.com/corruptbear/twitter_guard
To use the package, create apifree.yaml
, white_list.yaml
and block_list.yaml
in the folder where your own script resides.
touch apifree.yaml && touch white_list.yaml && touch block_list.yaml
apifree.yaml
Before first time use, set latest_cursor
field as ""
.
filtering_rule: your_custom_filtering_rule
latest_cursor: ""
login:
email: your_actual_stuff_here
password: your_actual_stuff_here
phonenumber: your_actual_stuff_here
screenname: your_actual_stuff_here
put known friends in white_list.yaml
line by line
id1_of_your_friend: name1_of_your_friend
id2_of_your_friend: name2_of_your_friend
put known friends in block_list.yaml
line by line
id1_of_your_enemy: name1_of_your_enemy
id2_of_your_enemy: name2_of_your_enemy
import os
from twitter_guard.apifree_bot import TwitterBot
#specify the paths of configs and cookies
pwd = os.path.dirname(os.path.realpath(__file__))
COOKIE_PATH = os.path.join(pwd, "sl_cookies.pkl")
#COOKIE_PATH = os.path.join(pwd,'twitter.com_cookies.txt')#alternatively using Netscape cookie file
CONFIG_PATH = os.path.join(pwd, "apifree.yaml")#optional, if the cookie is not valid, then you will need the credentials in the config file
WHITE_LIST_PATH = os.path.join(pwd, "white_list.yaml")#optional
BLOCK_LIST_PATH = os.path.join(pwd, "block_list.yaml")#optional
#create the bot
bot = TwitterBot(
cookie_path=COOKIE_PATH,#absolutely mandatory
config_path=CONFIG_PATH,
white_list_path=WHITE_LIST_PATH,
block_list_path=BLOCK_LIST_PATH,
)
try:
# use a small query to test the validity of cookies
bot.get_badge_count()
except:
#in order to refresh cookies, you have to supply valid credentials in the config
bot.refresh_cookies()
#examing recent interactions, and block users according to the filtering_rule defined in apifree.yaml
bot.check_notifications(block=True)
logic expression describing bad accounts
- logic operators:
not
and
or
- arithmatic operators:
+
-
*
/
- comparison operators:
>
<
>=
<=
==
!=
- keywords:
followers_count
following_count
tweet_count
media_count
favourites_count
days
Example
"(followers_count <= 5 and following_count <= 5) or (days <= 180)"
"(followers_count <= 10 and following_count <= 10) or (days <= 360) or ((followers_count/(tweet_count + 1) > 20) and tweet_count < 100)"