Skip to content

Commit

Permalink
Add pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
landaire committed Oct 21, 2016
1 parent 5732f49 commit 6bc4aa2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ ENV/
# Rope project settings
.ropeproject
*.iml
credentials
63 changes: 60 additions & 3 deletions detweet.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,75 @@
#!/usr/bin/env python3

from __future__ import print_function
import argparse
import tweepy
import os
import sys
import csv
import re

ENVIRON_CONSUMER_KEY = 'CONSUMER_KEY'
ENVIRON_CONSUMER_SECRET = 'CONSUMER_SECRET'
ENVIRON_ACCESS_TOKEN = 'ACCESS_TOKEN'
ENVIRON_ACCESS_TOKEN_SECRET = 'ACCESS_TOKEN_SECRET'


def main():
parser = argparse.ArgumentParser(description='Delete tweets en masse')
parser.add_argument('--csv', help='the path to your tweets.csv file located in your twitter archive', required=True)
parser.add_argument('--dry', type=bool, default=False, help='do a dry run')
parser.add_argument('keyword', help='keywords to delete', nargs='+')
parser.add_argument('pattern', help='regular expressions to match', nargs='+')

args = parser.parse_args()

print(args.keyword)
print(args.csv)
# The consumer keys can be found on your application's Details
# page located at https://dev.twitter.com/apps (under "OAuth settings")
consumer_key = os.getenv(ENVIRON_CONSUMER_KEY)
consumer_secret = os.getenv(ENVIRON_CONSUMER_SECRET)

# The access tokens can be found on your applications's Details
# page located at https://dev.twitter.com/apps (located
# under "Your access token")
access_token = os.getenv(ENVIRON_ACCESS_TOKEN)
access_token_secret = os.getenv(ENVIRON_ACCESS_TOKEN_SECRET)

required_environ_vars = [consumer_key, consumer_secret, access_token, access_token_secret]
if None in required_environ_vars:
required_environ_var_keys = [ENVIRON_ACCESS_TOKEN, ENVIRON_ACCESS_TOKEN_SECRET, ENVIRON_CONSUMER_KEY,
ENVIRON_CONSUMER_SECRET]
eprint('Missing a required environment variable. Make sure the following are set: {}'.format(
', '.join(required_environ_var_keys)))

exit(1)

with open(args.csv) as csv_file:
reader = csv.DictReader(csv_file)

matching_tweets = []
for row in reader:
if tweet_matches_patterns(row['text'], args.pattern):
matching_tweets.append(row)

for tweet in matching_tweets:
print(tweet['text'])


def eprint(*args, **kwargs):
"""Helper function that prints to stderr"""
print(*args, file=sys.stderr, **kwargs)


def tweet_matches_patterns(tweet, patterns):
"""Checks if a tweet matches a pattern
:param tweet: tweet to search for the given pattern
:param patterns: patterns to check against
"""

for pattern in patterns:
if re.search(pattern, tweet, re.IGNORECASE) is not None:
return True

return False


if __name__ == '__main__':
Expand Down

0 comments on commit 6bc4aa2

Please sign in to comment.