Skip to content

Instantly share code, notes, and snippets.

@tateisu
Created September 25, 2012 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tateisu/3783381 to your computer and use it in GitHub Desktop.
Save tateisu/3783381 to your computer and use it in GitHub Desktop.
AndroidのTwitterクライアントから共有されたツイート本文のパース
// twicca プラグインとして起動された場合
if( "jp.r246.twicca.ACTION_SHOW_TWEET".equals(intent.getAction()) ){
item.type_extra.put(TE_SCREEN_NAME,intent.getStringExtra("user_screen_name"));
item.type_extra.put(TE_STATUS_TEXT,item.body);
item.type_extra.put(TE_STATUS_ID,intent.getStringExtra( "id" ));
break;
}
Matcher m;
// twiccaからツイートを共有された場合
Pattern reTwicca= Pattern.compile(
"([a-z0-9_]{1,20})[\\x0d\\x0a]+(.+?)[\\x0d\\x0a]+([^\\x0d\\x0a]+から)[\\x0d\\x0a]+http://twitter\\.com/([a-z0-9_]{1,20})/status/(\\d+)"
,Pattern.CASE_INSENSITIVE|Pattern.DOTALL
);
m = reTwicca.matcher(item.body);
if(m.matches() ){
item.type_extra.put(TE_STATUS_TEXT,m.group(2));
item.type_extra.put(TE_SCREEN_NAME,m.group(4));
item.type_extra.put(TE_STATUS_ID,m.group(5));
break;
}
// Twitter公式からツイートを共有された場合
Pattern reTwitterOfficial = Pattern.compile(
" \\(@[a-z0-9_]{1,20}\\) (?:がツイートをあなたに共有しました|has shared a Tweet with you):[\\x0d\\x0a]+\"([a-z0-9_]{1,20}): (.+?)\"[\\x0d\\x0a]+--http://twitter\\.com/([a-z0-9_]{1,20})/status/(\\d+)\\Z"
,Pattern.CASE_INSENSITIVE|Pattern.DOTALL
);
m = reTwitterOfficial.matcher(item.body);
if(m.find() ){
item.type_extra.put(TE_STATUS_TEXT,m.group(2));
item.type_extra.put(TE_SCREEN_NAME,m.group(3));
item.type_extra.put(TE_STATUS_ID,m.group(4));
break;
}
// TweetDeckからツイートを共有された場合
Pattern reTweetDeck = Pattern.compile(
"\\A([a-z0-9_]{1,20}): (.+?)[\\x0d\\x0a]+Original Tweet: http://twitter\\.com/([a-z0-9_]{1,20})/status/(\\d+)[\\x0d\\x0a]+Sent via TweetDeck \\(www\\.tweetdeck\\.com\\)\\Z"
,Pattern.CASE_INSENSITIVE|Pattern.DOTALL
);
m = reTweetDeck.matcher(item.body);
if(m.find() ){
item.type_extra.put(TE_STATUS_TEXT,m.group(2));
item.type_extra.put(TE_SCREEN_NAME,m.group(3));
item.type_extra.put(TE_STATUS_ID,m.group(4));
break;
}
// Seesmic からツイートを共有された場合
Pattern reSeesmic= Pattern.compile(
" \\(([a-z0-9_]{1,20})\\):[\\x0d\\x0a ]+(.+?)[\\x0d\\x0a]+http://twitter\\.com/([a-z0-9_]{1,20})/status/(\\d+)[\\x0d\\x0a]+\\(.+?http://www\\.seesmic\\.com\\)\\Z"
,Pattern.CASE_INSENSITIVE|Pattern.DOTALL
);
m = reSeesmic.matcher(item.body);
if(m.find() ){
item.type_extra.put(TE_STATUS_TEXT,m.group(2));
item.type_extra.put(TE_SCREEN_NAME,m.group(3));
item.type_extra.put(TE_STATUS_ID,m.group(4));
break;
}
// Twidroydからツイートを共有された場合
Pattern reTwidroyd= Pattern.compile(
"\\A@([a-z0-9_]{1,20}):[\\x0d\\x0a ]+(.+?)[\\x0d\\x0a]+--[\\x0d\\x0a]+shared via Twidroyd http://twidroyd\\.com\\Z"
,Pattern.CASE_INSENSITIVE|Pattern.DOTALL
);
Pattern reStatusNumber = Pattern.compile("(\\d+)$");
m = reTwidroyd.matcher(item.body);
if(m.find() ){
String url = item.extra.get("uri");
if( url != null ){
item.type_extra.put(TE_SCREEN_NAME,m.group(1));
item.type_extra.put(TE_STATUS_TEXT,m.group(2));
m = reStatusNumber.matcher(url);
if( ! m.find() ){
log.e("missing status number in %s",url);
item.type_extra.clear();
}else{
item.type_extra.put(TE_STATUS_ID,m.group(1));
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment