Twitter Common Members Counter
Twitterの2id間の共通人数を数えるだけだよ!
ついでにfollowing/follower間の共通人数も数えているよ!
/**
* Copyright uwi ( http://wonderfl.net/user/uwi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/tYnQ
*/
// Twitterの2id間の共通人数を数えるだけだよ!
// ついでにfollowing/follower間の共通人数も数えているよ!
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.*;
import flash.net.*;
import flash.system.*;
import flash.text.*;
import jp.progression.commands.*;
import jp.progression.commands.lists.*;
import jp.progression.commands.net.*;
import jp.progression.data.*;
import com.adobe.serialization.json.*;
import com.bit101.components.*;
public class TCMC extends Sprite {
private var _tf : TextField;
private var URIing1 : String;
private var URIing2 : String;
private var URIer1 : String;
private var URIer2 : String;
private var _id1 : InputText;
private var _id2 : InputText;
private var _ing1 : TextField;
private var _ing2 : TextField;
private var _er1 : TextField;
private var _er2 : TextField;
private var _ingCommon : TextField;
private var _erCommon : TextField;
private var _inger1 : TextField;
private var _inger2 : TextField;
public function TCMC()
{
_tf = new TextField();
_tf.width = 100;
_tf.height = 200;
addChild(_tf);
// 5ivestarお兄ちゃんのproxy
Security.loadPolicyFile("http://5ivestar.org/proxy/crossdomain.xml");
// id入力欄
_id1 = new InputText(this, 50, 50);
_id1.width = 150;
_id1.textField.restrict = "A-Za-z0-9_";
_id2 = new InputText(this, 465 - 50 - 150, 50);
var dtf : TextFormat = _id2.textField.defaultTextFormat;
dtf.align = "right";
_id2.textField.defaultTextFormat = dtf;
_id2.textField.restrict = "A-Za-z0-9_";
_id2.width = 150;
// Analyze!
new PushButton(this, 465/2-51, 20, "Analyze!", onAnalyzeClick);
// ラベル
new Label(this, 50, 35, "id1");
new Label(this, 465-50-20, 35, "id2");
new Label(this, 50, 100, "following");
new Label(this, 465-50-40, 100, "following");
new Label(this, 50, 350, "follower");
new Label(this, 465-50-40, 350, "follower");
new Label(this, 70, 220, "common(following,follower)");
new Label(this, 465-70-120, 220, "common(following,follower)");
new Label(this, 465/2-60, 120, "common(following1,following2)");
new Label(this, 465/2-60, 330, "common(follower1,follower2)");
// カウント表示欄
_ing1 = new TextField();
_ing1.x = 50; _ing1.y = 120; _ing1.autoSize = "left"; _ing1.defaultTextFormat = new TextFormat("Arial", 40); addChild(_ing1);
_ing2 = new TextField();
_ing2.x = 465 - 50 - 100; _ing2.y = 120; _ing2.autoSize = "right"; _ing2.defaultTextFormat = new TextFormat("Arial", 40); addChild(_ing2);
_er1 = new TextField();
_er1.x = 50; _er1.y = 370; _er1.autoSize = "left"; _er1.defaultTextFormat = new TextFormat("Arial", 40); addChild(_er1);
_er2 = new TextField();
_er2.x = 465 - 50 - 100; _er2.y = 370; _er2.autoSize = "right"; _er2.defaultTextFormat = new TextFormat("Arial", 40); addChild(_er2);
_inger1 = new TextField();
_inger1.x = 70; _inger1.y = 240; _inger1.autoSize = "left"; _inger1.defaultTextFormat = new TextFormat("Arial", 40); addChild(_inger1); _inger1.textColor = 0x3f3fff;
_inger2 = new TextField();
_inger2.x = 465 - 70 - 100; _inger2.y = 240; _inger2.autoSize = "right"; _inger2.defaultTextFormat = new TextFormat("Arial", 40); addChild(_inger2); _inger2.textColor = 0x3f3fff;
_ingCommon = new TextField();
_ingCommon.x = 465/2-50; _ingCommon.y = 140; _ingCommon.autoSize = "center"; _ingCommon.defaultTextFormat = new TextFormat("Arial", 40); addChild(_ingCommon); _ingCommon.textColor = 0xff3f3f;
_erCommon = new TextField();
_erCommon.x = 465/2-50; _erCommon.y = 350; _erCommon.autoSize = "center"; _erCommon.defaultTextFormat = new TextFormat("Arial", 40); addChild(_erCommon); _erCommon.textColor = 0xff3f3f;
// 背景
graphics.lineStyle(30, 0xd7d7ff);
graphics.moveTo(50, 120);
graphics.lineTo(70, 240);
graphics.lineTo(50, 370);
graphics.lineTo(465/2, 350);
graphics.lineTo(465-50, 370);
graphics.lineTo(465-70, 240);
graphics.lineTo(465-50, 120);
graphics.lineTo(465/2, 140);
graphics.lineTo(50, 120);
// デフォルト値
_id1.text = "clockmaker";
_id2.text = "uwitenpen";
}
private function onAnalyzeClick(e : MouseEvent) : void
{
if(_id1.text != "" && _id2.text != ""){
analyze(_id1.text, _id2.text);
}
}
private function analyze(id1 : String, id2 : String) : void
{
var ING : String = "http://5ivestar.org/proxy/http://twitter.com/friends/ids/";
var ER : String = "http://5ivestar.org/proxy/http://twitter.com/followers/ids/";
URIing1 = ING + id1 + ".json";
URIing2 = ING + id2 + ".json";
URIer1 = ER + id1 + ".json";
URIer2 = ER + id2 + ".json";
// JSONをロード
// ひとつでも失敗したらエラーを表示して脱出
new SerialList(null,
new LoaderList(
{catchError:function(target:Command, e:Error) : void{
tr("Error!");
}},
new LoadURL(new URLRequest(URIing1)),
new LoadURL(new URLRequest(URIing2)),
new LoadURL(new URLRequest(URIer1)),
new LoadURL(new URLRequest(URIer2))
),
parseJson
).execute();
}
// JSONを解析してカウントを出す
private function parseJson() : void
{
var fings1 : Array = JSON.decode(getResourceById(URIing1).toString());
var fings2 : Array = JSON.decode(getResourceById(URIing2).toString());
var fers1 : Array = JSON.decode(getResourceById(URIer1).toString());
var fers2 : Array = JSON.decode(getResourceById(URIer2).toString());
_ing1.text = "" + fings1.length;
_ing2.text = "" + fings2.length;
_er1.text = "" + fers1.length;
_er2.text = "" + fers2.length;
// ID昇順にソート
fings1.sort(Array.NUMERIC);
fings2.sort(Array.NUMERIC);
fers1.sort(Array.NUMERIC);
fers2.sort(Array.NUMERIC);
_ingCommon.text = "" + intersect(fings1, fings2);
_erCommon.text = "" + intersect(fers1, fers2);
_inger1.text = "" + intersect(fings1, fers1);
_inger2.text = "" + intersect(fings2, fers2);
}
// 昇順にソートされた2配列の共通要素の個数を数える
private function intersect(a : Array, b : Array) : uint
{
var p : uint = 0;
var ct : uint = 0;
for each(var id : Number in a){
while(p < b.length && id > b[p])p++;
if(p == b.length)break;
if(id == b[p])ct++;
}
return ct;
}
private function tr(...o : Array) : void
{
_tf.appendText(o + "\n");
}
}
}