forked from benjaminjackson/shame-eraser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
erase_json.rb
161 lines (132 loc) · 5.04 KB
/
erase_json.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require 'date'
require 'csv'
require 'json'
require 'rubygems'
require "bundler/setup"
require 'twitter'
require 'chronic'
$: << 'lib'
require 'lib_trollop'
require 'config'
p = Trollop::Parser.new do
version "shame-eraser 0.1 (c) 2012 Benjamin Jackson"
banner <<-EOS
shame-eraser 0.1 (c) 2012 Benjamin Jackson
Shame Eraser is a small Ruby script for erasing your shame on Twitter (i.e., your oldest tweets).
Usage:
erase.rb [options] <foldername>
You can run it without options and just follow the prompts, or specify them on the command line as follows:
EOS
opt :"start-date", "Tweets before this date will not be deleted", :short => 's', :type => Date
opt :"end-date", "Tweets after this date will not be deleted", :short => 'e', :type => Date
opt :number, "Number of tweets to be deleted, starting from the first", :short => 'n', :type => Integer
end
@opts = Trollop::with_standard_exception_handling p do
raise Trollop::HelpNeeded if ARGV.empty? # show help screen
p.parse ARGV
end
TWEET_ARCHIVE_DIR = ARGV.shift
if @opts[:number].nil?
puts "\nHow many of your early tweets would you like to delete?"
puts "Enter a number, or 'date' to specify a date range."
puts "For example, '50' will delete your first 50 tweets."
@opts[:number] = gets.chomp.strip
while @opts[:number].to_i <= 0 && !@opts[:number].to_s.include?("date")
puts "Please specify a number greater than 0, or 'date'."
@opts[:number] = gets.chomp.strip
end
@opts[:number] = "date" if @opts[:number].to_s.include?("date")
@opts[:number] = @opts[:number].to_i unless @opts[:number] == "date"
end
if @opts[:number] == "date"
[:"start-date", :"end-date"].each do |key|
if @opts[key].nil?
while !@opts[key].is_a?(Date)
begin
puts "\nWhat's the #{key == :"start-date" ? "start" : "end"} date? Example: 'a year ago', 'January 2009'. Tweets #{key == :"start-date" ? "before" : "after"} this date will not be deleted."
@opts[key] = Date::parse(Chronic::parse(gets.chomp.strip).strftime("%Y-%m-%d"))
puts "Great! That looks like #{@opts[key].to_s}."
rescue
puts "Error parsing date. Please try again."
end
end
end
end
end
@total_deleted = 0
def delete_tweets_in_file filepath
lines = CSV.read(filepath)
id_index = lines.first.index("tweet_id")
date_index = lines.first.index("timestamp")
# reverse so it's earliest tweets first
lines[1..-1].reverse.each_with_index do |row, index|
date = Date.parse(row[date_index])
if (@opts[:number] != "date" && @total_deleted < @opts[:number]) ||
(@opts[:number] == "date" && (@opts[:"start-date"]...@opts[:"end-date"]).include?(date))
id = row[id_index]
puts "Deleting tweet with ID #{id}"
begin
Twitter.status_destroy(id)
@total_deleted += 1
rescue Twitter::Error::NotFound
puts "Tweet not found. Perhaps you already deleted this one?"
end
end
if @opts[:number] != "date" && @total_deleted == @opts[:number]
puts "Finished deleting #{@opts[:number]} tweets"
exit
end
end
end
def delete_tweets_in_json_file filepath
file = open(filepath)
json = file.read
tweets = JSON.parse(json)
#puts tweets
id_list = Array.new
date_list = Array.new
tweets.each { |element|
id_list.push(element["tweet"]["id"])
date_list.push(element["tweet"]["created_at"])
}
# reverse so it's earliest tweets first
id_list = id_list.reverse()
date_list = date_list.reverse()
i = 0
id_list.each do |id|
date = Date.parse(date_list[i])
if (@opts[:number] != "date" && @total_deleted < @opts[:number]) ||
(@opts[:number] == "date" && (@opts[:"start-date"]...@opts[:"end-date"]).include?(date))
puts "Deleting tweet with ID #{id}"
begin
#puts "Should try to destroy tweet with ID #{id} at date #{date}"
Twitter.status_destroy(id)
@total_deleted += 1
rescue Twitter::Error::NotFound
puts "Tweet not found. Perhaps you already deleted this one?"
end
end
if @opts[:number] != "date" && @total_deleted == @opts[:number]
puts "Finished deleting #{@opts[:number]} tweets"
exit
end
i = i+1
end
end
if @opts[:number] == "date"
puts "\nThis action will PERMANENTLY delete your tweets for @#{Twitter.user.username} between #{@opts[:"start-date"]} and #{@opts[:"end-date"]}. Are you absolutely certain beyond a shadow of a doubt that you want to do this? There is no undo. (Y/n)"
else
tweets_phrase = @opts[:number] > 1 ? "your first #{@opts[:number]} tweets" : "your first tweet"
puts "\nThis action will PERMANENTLY delete #{tweets_phrase} for @#{Twitter.user.username}. Are you absolutely certain beyond a shadow of a doubt that you want to do this? There is no undo. (Y/n)"
end
confirmation = gets.chomp.strip
if confirmation == "y"
puts "Is that a 'yes'? Just making sure."
confirmation = gets.chomp.strip
end
if confirmation == "Y" || confirmation == 'yes'
puts "Deleting tweets..."
delete_tweets_in_json_file(File.join(TWEET_ARCHIVE_DIR, "tweets.js"))
else
puts "Canceling"
end