Skip to content

Instantly share code, notes, and snippets.

@orekyuu
Created January 10, 2015 07:10
Show Gist options
  • Save orekyuu/3c303bc20c0d653f16da to your computer and use it in GitHub Desktop.
Save orekyuu/3c303bc20c0d653f16da to your computer and use it in GitHub Desktop.
UpdateNamePlugin
package net.orekyuu.updatename;
import net.orekyuu.javatter.api.models.UserModel;
import net.orekyuu.javatter.api.plugin.OnLoad;
import net.orekyuu.javatter.api.twitter.ClientUser;
import net.orekyuu.javatter.api.twitter.ClientUserRegister;
import twitter4j.Status;
import twitter4j.TwitterException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UpdateNamePlugin {
@OnLoad
public void load() {
ClientUserRegister.getInstance().registeredUserList()
.forEach(clientUser -> clientUser.getStream().addOnStatus(status -> updateName(status, clientUser)));
}
private void updateName(Status status, ClientUser clientUser) {
String text = status.getText();
String name = clientUser.getName();
String updateName = getUpdateName(text, name);
if (updateName != null) {
try {
UserModel build = UserModel.Builder.build(clientUser.getTwitter().getId(), clientUser);
clientUser.getTwitter().updateProfile(updateName, build.getWebSite(), build.getLocation(), build.getDescription());
String response = "." + updateName + "になりました。";
clientUser.getTwitter().updateStatus(response);
} catch (TwitterException e) {
e.printStackTrace();
}
}
}
public String getUpdateName(String text, String user) {
Pattern pattern = Pattern.compile("^\\s*@" + user + "\\s+update_name\\s+(\\S{1,20})\\s*$");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment