Skip to content

Commit

Permalink
Add before and after dates
Browse files Browse the repository at this point in the history
  • Loading branch information
landaire committed Oct 21, 2016
1 parent 6bc4aa2 commit 84c0a11
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
23 changes: 23 additions & 0 deletions detweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@
import sys
import csv
import re
import moment

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

DATE_FORMAT = 'YYYY-M-D'
TWEET_TIMESTAMP_FORMAT = 'YYYY-MM-DD'


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('--before', help='match tweets before this date (YYYY-M-D)')
parser.add_argument('--after', help='match tweets after this date (YYYY-M-D)')
parser.add_argument('pattern', help='regular expressions to match', nargs='+')

args = parser.parse_args()
Expand All @@ -42,11 +48,28 @@ def main():

exit(1)

before_date = None
after_date = None

if args.before is not None:
before_date = moment.date(args.before, DATE_FORMAT)
if args.after is not None:
after_date = moment.date(args.after, DATE_FORMAT)

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

matching_tweets = []
for row in reader:
# slice the date here so that we don't bother parsing the time
tweet_date = moment.date(row['timestamp'][:len(TWEET_TIMESTAMP_FORMAT)], TWEET_TIMESTAMP_FORMAT)

if before_date is not None and tweet_date > before_date:
continue

if after_date is not None and tweet_date < after_date:
continue

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

Expand Down
11 changes: 11 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
arrow==0.8.0
moment==0.5.1
oauthlib==2.0.0
python-dateutil==2.5.3
pytz==2016.7
requests==2.11.1
requests-oauthlib==0.7.0
six==1.10.0
times==0.7
tweepy==3.5.0
wheel==0.24.0

0 comments on commit 84c0a11

Please sign in to comment.