Skip to content

Instantly share code, notes, and snippets.

@naiquevin
Created January 15, 2014 17:40
Show Gist options
  • Save naiquevin/8440758 to your computer and use it in GitHub Desktop.
Save naiquevin/8440758 to your computer and use it in GitHub Desktop.
Script to delete spam DMs sent from your account by some malicious apps on twitter
"""Script to delete spam DMs sent from your account by some malicious
apps on twitter
Usage: This script needs to be run in two steps
1. $ python twdm.py auth (for getting oauth token and secret)
This step will prompt you to enter pin and then print the oauth
token and secret.
2. $ python twdm.py delete <oauth_token> <oauth_token_secret>
This step will identify the spam messages (those that start with
'http') and delete them. The 'sent_messages' API returns only few
recent DMs so you might need to run this script several times.
Note: This will only delete the messages. Make sure you change your
twitter password and revoke access to the app.
"""
import sys
from pprint import pprint
from twython import Twython
APP_KEY = '<your-app-key>'
APP_SECRET = '<your-app-secret>'
def auth():
twitter = Twython(APP_KEY, APP_SECRET, oauth_version=1.0)
auth = twitter.get_authentication_tokens()
OAUTH_TOKEN = auth['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']
print('Visit the following link where you will get a pin. Paste it here.')
print(auth['auth_url'])
oauth_verifier = raw_input('Enter pin> ')
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN,
OAUTH_TOKEN_SECRET)
final_step = twitter.get_authorized_tokens(oauth_verifier)
oauth_token = final_step['oauth_token']
oauth_token_secret = final_step['oauth_token_secret']
print(oauth_token)
print(oauth_token_secret)
def delete_DMs(oauth_token, oauth_token_secret):
twitter = Twython(APP_KEY, APP_SECRET, oauth_token, oauth_token_secret)
spams = [(s['id'], s['text'])
for s in twitter.get_sent_messages()
if s['text'].startswith('http')]
pprint(spams)
raw_input('Proceed with deleting? [Enter/Ctrl-c]')
for spam in spams:
twitter.destroy_direct_message(id=spam[0])
def main():
args = sys.argv[1:]
if args[0] == 'auth':
auth()
elif args[0] == 'delete':
oauth_token, oauth_token_secret = args[1:]
delete_DMs(oauth_token, oauth_token_secret)
return 0
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment