twitter 取得テスト
/**
* Copyright tepe ( http://wonderfl.net/user/tepe )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/tFBw
*/
// forked from moyashipan's 形態素解析もマルコフ連鎖もしないとこうなる
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.TextEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.StyleSheet;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.ui.Keyboard;
/**
* 指定したTwitterIDの発言を取得して適当に並び替えます
*
* TwitterIDを入力して[Search]を押してね
* protectだったりすると見れないよ
* reply先のIDをクリックするとその人の発言で生成するよ
*
* http://apiwiki.twitter.com/Twitter-Search-API-Method%3A-search
* @author Shinichiro Sugimoto
*/
import flash.display.Graphics;
public class Main extends Sprite
{
private static const TWITTER_USER_LINK:String = '<a href="http://twitter.com/$1" target="_blank">http://twitter.com/$1</a>';
private static const TWITTER_REPLY_LINK:String = '@<a href="event:$1" target="_blank">$1</a>';
private static const TWITTER_SEARCH_INFO_URL:String = "http://search.twitter.com/search.atom?q=${q}";
private static const SEARCH_PARAMS:Object = { rpp:20 };//取得数
//private var DEFAULT_USER_ID:String = "kis";
private var textField:TextField;
//private var input:TextField;
private var button:TextField;
private var linkButton:TextField;
private var userID:String = "kis";
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var css:StyleSheet = new StyleSheet();
css.setStyle("a", { color:"#2276BB", textDecoration:"underline" } );
/*
// INPUT ID
input = new TextField();
input.x = input.y = 5;
input.width = 120;
input.height = 20;
input.border = true;
input.borderColor = 0x999999;
input.type = TextFieldType.INPUT;
//input.restrict = "[0-9a-zA-Z_]";
input.text = DEFAULT_USER_ID;
input.background = true;
addChild(input);
input.addEventListener(Event.CHANGE, onInputChange, false, 0, true);
input.addEventListener(KeyboardEvent.KEY_UP, onKUp, false, 0, true);
*/
// SEARCH BUTTON
button = new TextField();
button.background = true;
button.backgroundColor = 0x000000;
button.textColor = 0xffffff;
button.text = " Search";
button.selectable = false;
button.x = 127;
button.y = 5;
button.border = true;
button.borderColor = 0x999999;
button.width = 40;
button.height = 20;
addChild(button);
button.addEventListener(MouseEvent.CLICK, onBClick, false, 0, true);
/*
// LINK
linkButton = new TextField();
linkButton.y = 5;
linkButton.x = 170;
linkButton.width = 230;
linkButton.height = 20;
linkButton.htmlText = getHtmlText();
addChild(linkButton);
linkButton.styleSheet = css;
*/
// RESULT
textField = new TextField();
textField.width = 465 - 10;
textField.height = 465 - 40;
textField.x = 5;
textField.y = 35;
textField.wordWrap = true;
addChild(textField);
//textField.addEventListener(TextEvent.LINK, onReplyeeLink, false, 0, true);
textField.styleSheet = css;
//最初にすぐ表示
onBClick();
}
private function onReplyeeLink(e:TextEvent):void
{
userID = e.text;
linkButton.htmlText = getHtmlText();
onBClick();
}
private function onKUp(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.ENTER || e.keyCode == Keyboard.NUMPAD_ENTER) {
onBClick();
}
}
private function onInputChange(e:Event):void
{
linkButton.htmlText = getHtmlText();
}
private function getHtmlText():String
{
return TWITTER_USER_LINK.replace(/\$1/g, userID);//文字列置き換え
}
//
//データ要求
private function onBClick(e:* = null):void
{
/*
if (!button.mouseEnabled) return;
button.mouseEnabled = false;
button.textColor = 0x808080;
*/
//url指定
var twitterUrl:String = TWITTER_SEARCH_INFO_URL.replace("${q}", "from%3A"+ userID);
for (var key:String in SEARCH_PARAMS) {
twitterUrl += "&" + key + "=" + SEARCH_PARAMS[key];
}
var loader:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest(twitterUrl);
loader.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
loader.load(req);
}
private function onComplete(e:Event):void
{
button.mouseEnabled = true;
button.textColor = 0xffffff;
var loader:URLLoader = e.target as URLLoader;
if (loader != null && loader.data != null) {
default xml namespace = new Namespace("http://www.w3.org/2005/Atom");
var xml:XML = new XML(loader.data);
if (xml == null) {
showMessage("Not Found");
return;
}
if (xml.children().length() < 1) {
showMessage("Not Found\n\n" + xml.toString());
return;
}
var entries:XMLList = xml..entry;
if (entries.length() < 1) {
showMessage("Not Found\n\n" + xml.toString());
return;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
textField.htmlText = String(xml);
return;
}
}
private function showMessage(message:String):void
{
message = message.replace(/</g, "<");
message = message.replace(/>/g, ">");
textField.htmlText = message;
}
}
}