flash on 2012-2-11
/**
* Copyright tepe ( http://wonderfl.net/user/tepe )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/tBID
*/
package {
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.text.*;
public class twit1 extends Sprite {
private var _resultStage:Sprite = new Sprite();
private var maxID:String;//これより古いIDを取得
private var sinceID:String;//これより新しいIDを取得
private var tlTop:Number = 25;
private var tlBottom:Number;
private var titleArea:Sprite = new Sprite();
private var keyword:String;
private var serchWord:TextField = new TextField();
public function twit1():void {
//背景
titleArea.graphics.beginFill(0x39CCFF);
titleArea.graphics.drawRect(0, 0, 165, 23);
//Serchの文字
serchWord.text = "検索:";
serchWord.width = 33;
serchWord.height = 18;
serchWord.x = 2;
serchWord.y = 2;
serchWord.textColor = 0xFFFFFF;
titleArea.addChild(serchWord);
titleArea.x =300;
//テキスト入力欄。キーボードのキーを押し上げた際にonKeyUpが実行される
var title:TextField = new TextField();
with(title){
text = "wonderfl";
width = 126;
height = 18;
x = 36;
y = 2;
border = true;
borderColor = 0xFFFFFF;
background = true;
backgroundColor = 0x99DDFF;
type = TextFieldType.INPUT;
addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
titleArea.addChild(title);
serchWord.addEventListener(MouseEvent.CLICK,function():void{
if(title.text != keyword)serch(title.text);
else{//新しいツイート取得
}
});
//検索結果を配置するSpriteをaddChild
_resultStage.y = 25;
addChild(_resultStage);
addChild(titleArea);
//初期状態で検索結果画面が出るように、検索を実行する。
serch(title.text);
//userRequest("tepe_i");
//userRequest("tepe_i");
_resultStage.addEventListener(MouseEvent.MOUSE_OVER,addScrool);
_resultStage.addEventListener(MouseEvent.MOUSE_OUT,removeScrool);
}
private function removeScrool(e:MouseEvent):void{
removeEventListener(Event.ENTER_FRAME,onFrame);
addEventListener(Event.ENTER_FRAME,stopScr);
}
private function addScrool(e:MouseEvent):void{
addEventListener(Event.ENTER_FRAME,onFrame);
}
private function stopScr(e:Event):void{
if(1<Math.abs(scrSpeed))scrSpeed/=1.001;
else{
scrSpeed=0;
removeEventListener(Event.ENTER_FRAME,stopScr);
}
if(_resultStage.y+(scrSpeed*100)>tlTop || _resultStage.y+(scrSpeed*100)<tlBottom){
scrSpeed/=1.1;
}
_resultStage.y-=scrSpeed;
}
private var scrSpeed:Number=0;
private var requestNow:Boolean=false;
private function onFrame(e:Event):void{
if(mouseY < 100 && mouseY-100 < scrSpeed*10 ){
if(tlTop > _resultStage.y)scrSpeed = (mouseY-100)/10;
else scrSpeed=0;
}
else if(350 < mouseY && scrSpeed*10 < mouseY-350){
scrSpeed = (mouseY-350)/10;
}
else {
if(0.1<Math.abs(scrSpeed))scrSpeed/=1.05;
else scrSpeed=0;
}
_resultStage.y-=scrSpeed;
if(-tlBottom+600 > _resultStage.y && requestNow==false){
moreRequest();
requestNow=true;
}
}
private function onKeyUp(e:KeyboardEvent):void {
//Enterキーのキーコード13だったら、serchメソッドを実行。引数には適すと入力欄の文字列
if (e.keyCode == 13) {
serch(e.currentTarget.text)
}
}
//キーワード検索
public function serch(serchWord:String):void {
//検索APIにリクエストを投げる
//http://search.twitter.com/crossdomain.xml上で許可をしているので
//特に指定しなくても自動的に確認して、許可を得られる
//_resultStage上のオブジェクトを全て削除
tlBottom=0;
while (_resultStage.numChildren) {
_resultStage.removeChildAt(0);
}
keyword = serchWord;
_resultStage.y = 25;
var myURLLoader:URLLoader = new URLLoader();
myURLLoader.addEventListener(Event.COMPLETE, onCompleteXML);
myURLLoader.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
var xmlURL:String = "http://search.twitter.com/search.atom?q=";
//encodeURIComponent()を使うことによって、#や日本語にも対応
xmlURL += encodeURIComponent(serchWord); //*1
myURLLoader.load(new URLRequest(xmlURL));
}
//リクエスト追加
public function moreRequest():void{
var myURLLoader:URLLoader = new URLLoader();
myURLLoader.addEventListener(Event.COMPLETE, onCompleteXML);
myURLLoader.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
var xmlURL:String = "http://search.twitter.com/search.atom?q=";
//encodeURIComponent()を使うことによって、#や日本語にも対応
xmlURL += encodeURIComponent(keyword); //*1
xmlURL += "&max_id="+sinceID;
myURLLoader.load(new URLRequest(xmlURL));
}
private var ut:userTwit = new userTwit();
//ユーザーのタイムライン取得
private function userRequest(userName:String):void {
tlBottom=0;
while (_resultStage.numChildren) {
_resultStage.removeChildAt(0);
}
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, COMPLETE);
//loader.addEventListener(Event.COMPLETE,onCompleteXML);
//var pipesURL:String = "http://pipes.yahooapis.com/pipes/pipe.run?_id=92416843789c0b7f9a0d0fbfe0aa54c6&_render=rss&statustitle=";
//pipesURL+=userName+"&username="+userName;
var tf:TextField = new TextField();
tf.text = "userRequest\n";
tf.width = 456;
_resultStage.addChild(tf);
tf.appendText(ut.Request(userName));
loader.load(new URLRequest(ut.Request(userName)));
}
private function COMPLETE(e:Event):void {
var list:XML = new XML(e.currentTarget.data);
/*var _length:int = list.channel.item.length();
var txt:String = "";
for (var i:int = 0; i < _length; i++) {
txt += String(list.channel.item[i].description) + "\n"
txt += String(list.channel.item[i].id) + "\n"
}*/
var tf:TextField = new TextField();
tf.y = 30;
tf.width = stage.stageWidth;
tf.height = stage.stageHeight;
tf.wordWrap = true;
//tf.htmlText = txt;
tf.text = String(list);
//tf.text = "complete";
_resultStage.addChild(tf);
}
public function onIoError(e:IOErrorEvent):void {
trace(e.type);
}
private function idNum(id:String):String{
var reg:RegExp = new RegExp();
reg = /:\d+/;
var result:Object = reg.exec(id);
var str:String = id.substring(result.index+1);
return str;
}
private function getHttp(str:String):String{
var reg:RegExp = new RegExp();
reg = /http:\/\/[\w.%&\/]+/g;
var result:Object;
var res2:String=new String();
var str2:String;
var prev:int=0;
for(var i:int =0;i<10;i++){
result = reg.exec(str);
if(result == null)break;
if(reg.lastIndex!=0){
res2+=str.substring(prev,result.index);
str2 = str.substring(result.index,reg.lastIndex);
res2+="<a href='" + str2 + "' target='_blank'>"+ '<u><font color="#0088ff">'+ str2+"</font></u>"+"</a>";
prev=reg.lastIndex;
}
else{
res2+=str.substring(prev,result.index);
str2 = str.substring(result.index);
res2+="<a href='" + str2 + "' target='_blank'>"+ '<u><font color="#0088ff">'+ str2+"</font></u>"+"</a>";
prev=str.length;
break;
}
}
res2+=str.substring(prev);
return res2;
}
private function getName(str:String=""):String{
var reg:RegExp = new RegExp();
reg = / (.*)/;
var result:Object;
var str2:String;
result = reg.exec(str);
str2 = str.substring(result.index+2,str.length-1);
return str2;
}
private function getUserID(str:String=""):String{
var reg:RegExp = new RegExp();
reg = /.+ /;
var result:Object;
var str2:String;
result = reg.exec(str);
str2 = str.substring(result.index,str.length-1);
return str2;
}
private function removeTag(str:String=""):String{
var reg:RegExp = new RegExp();
reg = /#[^ $]+/g;
var result:Object;
var res2:String=new String();
var str2:String;
var prev:int=0;
for(var i:int =0;i<10;i++){
result = reg.exec(str);
if(result == null)break;
if(reg.lastIndex!=0){
res2+=str.substring(prev,result.index);
prev=reg.lastIndex;
}
else{
res2+=str.substring(prev,result.index);
prev=str.length;
break;
}
}
res2+=str.substring(prev);
return res2;
}
private function getTime(str:String):Date{
var year:int;
var mon:int;
var day:int;
var hour:int;
var min:int;
var sec:int;
var reg:RegExp = new RegExp();
reg = /\d\d\d\d-\d\d-\d\d/g;//日付
var res:Object= reg.exec(str);
var str2:String = str.substring(res.index,reg.lastIndex);
var str3:String;
year=parseInt(str2.substring(0,4));
mon=parseInt(str2.substring(5,7))-1;
day=parseInt(str2.substring(8,10));
var reg2:RegExp = new RegExp()
reg2 = /\d\d:\d\d:\d\d/g;
res = reg2.exec(str);
str2 = str.substring(res.index,reg2.lastIndex);
hour = parseInt(str2.substring(0,2));
min = parseInt(str2.substring(3,5));
sec = parseInt(str2.substring(6,8));
var dat:Date = new Date(year,mon,day,hour,min,sec);
//str3 = year +"-"+mon+"-"+day+" "+hour+":"+min+":"+sec;
//str3 = str2+" "+res.index+" "+reg2.lastIndex;
return dat;
}
private function getTime2(str:String):String{
var year:int;
var mon:int;
var day:int;
var hour:int;
var min:int;
var sec:int;
var reg:RegExp = new RegExp();
reg = /\d\d\d\d-\d\d-\d\d/g;//日付
var res:Object= reg.exec(str);
var str2:String = str.substring(res.index,reg.lastIndex);
var str3:String;
year=parseInt(str2.substring(0,4));
mon=parseInt(str2.substring(5,7))-1;
day=parseInt(str2.substring(8,10));
var reg2:RegExp = new RegExp()
reg2 = /\d\d:\d\d:\d\d/g;
res = reg2.exec(str);
str2 = str.substring(res.index,reg2.lastIndex);
hour = parseInt(str2.substring(0,2));
min = parseInt(str2.substring(3,5));
sec = parseInt(str2.substring(6,8));
var dat:Date = new Date(year,mon,day,hour,min,sec,0);
str3 = year +"-"+mon+"-"+day+" "+hour+":"+min+":"+sec;
//str3 = str2+" "+res.index+" "+reg2.lastIndex;
return str3;
}
private function tagFunc(str:String):String{
var reg:RegExp = new RegExp();
reg = /#[^ $]+/g;
var result:Object;
var res2:String=new String();
var str2:String;
var prev:int=0;
for(var i:int =0;i<10;i++){
result = reg.exec(str);
if(result == null)break;
if(reg.lastIndex!=0){
res2+=str.substring(prev,result.index);
prev=reg.lastIndex;
}
else{
res2+=str.substring(prev,result.index);
prev=str.length;
break;
}
}
res2+=str.substring(prev);
return res2;
}
private var dat2:Date = new Date();
private var user:Object =new Object();
private function onCompleteXML(e:Event):void {
requestNow = false;
//eventListenerを削除
e.currentTarget.removeEventListener(Event.COMPLETE, onCompleteXML);
//XML型にキャスト
var myXML:XML = new XML(e.currentTarget.data);
//namespaceを設定
default xml namespace = new Namespace("http://www.w3.org/2005/Atom"); //*2
//ヘッダーの下
var tfY:int = tlBottom;
//要素を取り出して、textFieldを作りならべる
var itemLength:int = myXML.entry.length();
maxID=myXML.max_id;
var ID1:TextField = new TextField();
//addChild(ID1);
ID1.background = true;
ID1.y = -50;
ID1.width = 800;
ID1.height = 400;
for (var i:int = 0; i < itemLength; i++) {
//xmlからlink,authorName,titleの各値を取得
var ts:String = myXML.entry[i].published;
var dat1:Date = getTime(ts);
var idStr:String = myXML.entry[i].id;
var id:String = idNum(idStr);
var link:String = myXML.entry[i].link[0].@href;
var authorName:String = getName(myXML.entry[i].author.name);
var title:String = getHttp(myXML.entry[i].title);
var dat3:Date = new Date(dat1.fullYear,dat1.month,dat1.date,0,0,0,0);
//日時タグ作成
if(dat2.time > dat3.time){
var dTag:TextField = new TextField();
with(dTag){
width = 460;
x = 2;
Wrap = true;
background = true;
backgroundColor = 0xFFFFFF;
border = true;
borderColor = 0x39CCFF;
//htmlText = "";
autoSize = "left";
}
dTag.text = String(dat3.month+1)+"/"+String(dat3.date)+"\n";
//テキストフィールドのy座標はtfYに加算していくことにより、決定
dTag.y = tfY+50;
tfY += dTag.height + 53;
_resultStage.addChild(dTag);
dat2 = dat3;
}
//idの評価
if(maxID==null)maxID=id;
else{
if( Number(maxID) < Number(id) )maxID=id;
}
if(sinceID==null)sinceID=id;
else{
if( Number(id) < Number(sinceID) )sinceID=id;
}
//アイコン画像のURLを取得
var imgURL:String = myXML.entry[i].link[1].@href;
//bmpフォーマットのアイコン画像を使っている人も少なくないため、Flashで使えるファイルの拡張子かどうかを判別
var extention:String = imgURL.substr( -4);
extention = extention.toLowerCase();
if (extention != ".jpg" && extention != ".gif" && extention != ".png") {
//Flashで使える画像の拡張子ではなかった場合にはデフォルトアイコンを指定
imgURL = "http://s.twimg.com/a/1254440757/images/default_profile_mini.png";
}
//画像のファイル名が日本語などの規定外の文字である場合を考慮しエンコードする
imgURL = encodeURI(imgURL); //*3
//小さい画像で十分なので、名前を差し替える
//imgURL = imgURL.replace(/_normal\./, "_mini\.");
//得られた各値をhtmlTextに入るように整形
var htmldata:String = "";
htmldata = "<a href='" + link + "' target='_blank'>";
htmldata += "<img src='" + imgURL + "' width='24' height='24' />"+"</a>";
// htmldata += "<b>" + ts +"@"+ dat1.toLocaleDateString()+"\n</b> ";
htmldata += "<b>" + authorName + "</b> ";
htmldata += "\n"+title ;//+ "</a>";
//検索結果のテキストフィールド
var myTextField:TextField = new TextField();
myTextField.width = 460;
myTextField.x = 2;
myTextField.wordWrap = true;
myTextField.background = true;
myTextField.backgroundColor = 0xFFFFFF;
myTextField.border = true;
myTextField.borderColor = 0x39CCFF;
myTextField.htmlText = htmldata;
myTextField.autoSize = "left";
//テキストフィールドのy座標はtfYに加算していくことにより、決定
myTextField.y = tfY;
tfY += myTextField.height + 3;
_resultStage.addChild(myTextField);
}
tlBottom=tfY;
}
}
}
//import flash.display.*;
import flash.events.*;
import flash.net.*;
//import flash.text.*;
import flash.ui.*;
class userTwit{
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 };//取得数
public var userID:String;
public var _xml:XML;
private var DEFAULT_USER_ID:String = "Moyashipan";
public function userTwit():void
{
}
private function getHtmlText():String
{
return TWITTER_USER_LINK.replace(/\$1/g, userID);//文字列置き換え
}
//データ要求
public function Request(id:String=""):String
{
if (id == "") userID = DEFAULT_USER_ID;
else userID = id;
//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];
}
return twitterUrl;
//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
{
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);
_xml = xml;
if (xml == null)return;
if (xml.children().length() < 1)return;
var entries:XMLList = xml..entry;
if (entries.length() < 1)return;
//textField.htmlText = String(xml);
return;
}
}
}