Penggunaan REST API Tandif untuk Filtering pada Adobe AIR (Android version)

Pada beberapa hari yang lalu penulis mendapat undangan dari tandif.com untuk mencoba API yang mereka buat. Awalnya saya hanya mencoba secara sederhana, yakni dengan melalui browser saja, dengan memasukkan alamat API –nya pada URL address browser dikarenakan sangat sederhananya format API yang digunakan, doc-nya ada di sini.

Format-nya sebagi berikut :

http://green.tandif.com/api/v1/text/check?api_key=your_api_key_here&text=your_text_here
Jika kalian belum mengetahui tandif, dari website tandif, saya bisa ambil kesimpulan, bahwa tandif berfokus pada layanan filtering untuk content yang berbau pornografi. Ok, langsung saja ya. di sini saya buat aplikasi kecil yang bisa dijadikan aplikasi android pada pemrograman Adobe AIR. so cek aja code-nya di bawah ini

1 <?xml version="1.0" encoding="utf-8"?>
2 <s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
3 xmlns:s="library://ns.adobe.com/flex/spark"
4 creationComplete="view1_creationCompleteHandler(event)" title="Filter">
5 <fx:Declarations>
6 <!-- Place non-visual elements (e.g., services, value objects) here -->
7 </fx:Declarations>
8 <s:TextInput id="TextInput_URL" x="10" y="30" width="178" height="37" text=""/>
9 <s:Label x="10" y="10" text="URL"/>
10 <s:TextArea id="TextArea_Content" x="9" y="75" width="279" height="130"
11 text="fuck my ass and my dick"/>
12 <s:Button x="200" y="27" width="88" height="41" label="Ektract"
13 click="button2_clickHandler(event)"/>
14 <s:Label x="10" y="263" text="Result"/>
15 <s:TextArea id="TextArea_Result" x="10" y="286" height="116"/>
16 <s:Button x="201" y="213" width="88" height="41" label="Filter"
17 click="button1_clickHandler(event)"/>
18
19 <fx:Script>
20 <![CDATA[
21 import com.adobe.utils.StringUtil;
22
23 import mx.events.FlexEvent;
24 import mx.rpc.events.ResultEvent;
25 import mx.rpc.http.HTTPService;
26
27 private static const API_KEY_TANDIF:String = "YOUR_API_KEY_HERE";
28 private static const API_KEY_ALCHEMY:String = "YOUR_API_KEY_HERE";
29 private static const INCREMENT:int = 1000;
30
31 private var _httpService_Filter:HTTPService = new HTTPService();
32 private var _httpService_Ektract:HTTPService = new HTTPService();
33 private var _dataArrayStrings:Array = [];
34
35 private var _last:int = 0;
36 private var _index:int = 1;
37
38 private function do_filter():void{
39
40 _index = 1;
41 _dataArrayStrings = [];
42
43 var str:String = TextArea_Content.text;
44
45 // Proses Menyimpan String ke dalam Array, supaya bisa dipecah menjadi String yang lebih pendek.
46 if(str.length>INCREMENT){
47
48 var count:int = Math.floor(str.length/INCREMENT);
49
50 for (var i:int=0; i<count; i++){
51
52 var s1:String = "";
53
54 if(i == count){
55 s1 = str.substr(i*INCREMENT, str.length);
56 }else{
57 s1 = str.substr(i*INCREMENT, INCREMENT*(i+1));
58 }
59
60 _dataArrayStrings.push(s1);
61 }
62 }
63 if(_dataArrayStrings.length>0){
64
65 _httpService_Filter.url = "http://green.tandif.com/api/v1/text/check?api_key="+API_KEY_TANDIF+"&text="+_dataArrayStrings[0];
66 _dataArrayStrings.shift();
67 }else{
68 _httpService_Filter.url = "http://green.tandif.com/api/v1/text/check?api_key="+API_KEY_TANDIF+"&text="+TextArea_Content.text;
69 }
70 _httpService_Filter.send();
71 }
72
73 private function do_ektract():void {
74 _httpService_Ektract.url = "http://access.alchemyapi.com/calls/url/URLGetRawText?apikey="+API_KEY_ALCHEMY+"&url="+TextInput_URL.text+"&outputMode=json";
75 _httpService_Ektract.send();
76
77 }
78
79 protected function view1_creationCompleteHandler(event:FlexEvent):void
80 {
81 _httpService_Filter.addEventListener(ResultEvent.RESULT, onResult_Filtering, false, 0, true);
82 _httpService_Filter.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
83 _httpService_Filter.showBusyCursor = true;
84
85 _httpService_Ektract.addEventListener(ResultEvent.RESULT, onResult_Ekstract, false, 0, true);
86 _httpService_Ektract.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
87 _httpService_Ektract.showBusyCursor = true;
88
89 }
90
91 private function onResult_Filtering(e:ResultEvent):void{
92
93 try{
94 var data:Object = JSON.parse(e.result as String);
95 TextArea_Result.text += "String "+_last+" - "+(INCREMENT*_index) +" : "+(data.result as String)+"\n";
96 }catch(e:Error){
97
98 TextArea_Result.text += "No Result \n";
99 }
100
101 if(_dataArrayStrings.length > 0){
102
103 _httpService_Filter.url = "http://green.tandif.com/api/v1/text/check?api_key="+API_KEY_TANDIF+"&text="+_dataArrayStrings[0];
104 _httpService_Filter.send();
105 _dataArrayStrings.shift();
106
107 _last = (INCREMENT*_index);
108 _index++;
109
110 }
111 }
112
113 private function onResult_Ekstract(e:ResultEvent):void{
114
115 var data:Object = JSON.parse(e.result as String);
116 var str:String = data.text;
117
118 str = str.replace(/[^a-zA-Z0-9]/g, ' ');
119 str = str.replace(/\n/g, '');
120 str = StringUtil.trim(str);
121 TextArea_Content.text = str;
122
123 }
124
125 private function htmlUnescape(str:String):String
126 {
127 return new XMLDocument(str).firstChild.nodeValue;
128 }
129
130 protected function button1_clickHandler(event:MouseEvent):void
131 {
132 do_filter();
133 }
134
135 protected function button2_clickHandler(event:MouseEvent):void
136 {
137 do_ektract();
138 }
139
140 ]]>
141 </fx:Script>
142 </s:View>
Source Code | App Android (APK)


1 komentar:

  1. Anonim

    Naiiissss bro....

     

Posting Komentar