Created
December 11, 2016 17:33
-
-
Save 8q/c899183b073f19203ab07545e520f563 to your computer and use it in GitHub Desktop.
Update Name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package update_name; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import twitter4j.*; | |
import twitter4j.conf.Configuration; | |
import twitter4j.conf.ConfigurationBuilder; | |
public class UpdateName { | |
public static void main(String[] args) throws Exception { | |
Configuration conf = new ConfigurationBuilder() | |
.setOAuthConsumerKey("") | |
.setOAuthConsumerSecret("") | |
.setOAuthAccessToken("") | |
.setOAuthAccessTokenSecret("") | |
.build(); | |
TwitterStream twitterStream = new TwitterStreamFactory(conf).getInstance(); | |
Twitter twitter = new TwitterFactory(conf).getInstance(); | |
twitterStream.addListener(new UserStreamAdapter(){ | |
Pattern p = Pattern.compile(String.format("^@%s\\supdate_name\\s(.{1,20})$", twitter.getScreenName())); | |
@Override | |
public void onStatus(Status status){ | |
Matcher m = p.matcher(status.getText()); | |
if(m.find()){ | |
try { | |
twitter.updateProfile(m.group(1), null, null, null); | |
twitter.updateStatus(String.format("%sさんによって %s に変えられました", status.getUser().getName(), m.group(1))); | |
System.out.println(String.format("Update to %s by %s", m.group(1), status.getUser().getName())); | |
} catch (TwitterException te){ | |
System.out.println(te); | |
} | |
} | |
} | |
}); | |
twitterStream.user(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment