I use my own piece of code which uses a HttpURLConnection
and a twitter search url. I then use a regular expression to pull out the last 20 matching tweets... Luckily as I'm deleting the tweets I can simply search again until I can't find anymore tweets. I'm including the code although it's in Java but the same would apply for any language. First I use a class to actually search for tweets and record their details:
public class ReadSearch{
private String startURL = "https://twitter.com/search?f=realtime&q=from%3A";
private String middleURL = "%20%40";
private String endURL = "&src=typd";
public ArrayList<Tweet> getTweets(String user, String troll) {
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
String expr = "small.class=\"time\".*?href=\"/"
+ "([^/]+)"
+ ".*?status/"
+ "([^\"]+)"
+ ".*?title=\""
+ "([^\"]+)";
Pattern patt = Pattern.compile(expr, Pattern.DOTALL | Pattern.UNIX_LINES);
try {
Matcher m = patt.matcher(getData(startURL+user+middleURL+troll+endURL));
while (m.find()) {
if(user.equals(m.group(1).trim())){
Tweet tw = new Tweet();
tw.setUser(m.group(1).trim());
tw.setTweetid(Long.parseLong(m.group(2).trim()));
tw.setDate(m.group(3).trim());
tweets.add(tw);
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception " + e);
}
return tweets;
}
private StringBuilder getData(String dataurl) throws MalformedURLException, IOException{
URL url = new URL(dataurl);
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");
StringBuilder sb = new StringBuilder(16384);
BufferedReader br = new BufferedReader(new InputStreamReader(httpcon.getInputStream(), "ISO-8859-1"));
String line;
while ((line = br.readLine()) != null){
sb.append(line);
sb.append('\n');
}
httpcon.disconnect();
br.close();
return sb;
}
public static void main(String [] args){
//testing
ReadSearch rs = new ReadSearch();
ArrayList<Tweet> tweets = rs.getTweets("Tony_Kennah", "PickLuckier");
for(Tweet t : tweets){
System.out.println("TWEET: " + t.toString());
}
}
}
We then need the Tweet class itself so we can group Tweets up and do things with them, it's just a bean like this:
public class Tweet{
private String user;
private long tweetid;
private String date;
public String getUser(){
return user;
}
public void setUser(String user){
this.user = user;
}
public long getTweetid(){
return tweetid;
}
public void setTweetid(long tweetid){
this.tweetid = tweetid;
}
public String getDate(){
return date;
}
public void setDate(String date){
this.date = date;
}
public String toString(){
return this.tweetid + " " + this.user + " " + this.date;
}
}
... and so that was all just standard java. To make use of the above code I use the Twitter4J API and do this:
public class DeleteTweets
{
public static void main(String args[]) throws Exception
{
Twitter twitter = TwitterFactory.getSingleton();
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
String [] people = { "PickLuckier" };
for(String s : people){
do{
ReadSearch rs = new ReadSearch();
tweets = rs.getTweets(twitter.getScreenName(), s);
for(Tweet tw : tweets){
twitter.destroyStatus(tw.getTweetid());
}
} while(tweets.size()!=0);
}
}
}
That's it. I don't use comments but I hope it's easy to see what's going on and that this helps you out.