-
Notifications
You must be signed in to change notification settings - Fork 0
/
ballerina_search_service.bal
134 lines (125 loc) · 4.58 KB
/
ballerina_search_service.bal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import wso2/twitter;
import ballerina/io;
import ballerina/http;
import ballerina/config;
import ballerina/time;
import ballerina/log;
endpoint twitter:Client twitter{
clientId: config:getAsString("consumerKey"),
clientSecret: config:getAsString("consumerSecret"),
accessToken: config:getAsString("accessToken"),
accessTokenSecret: config:getAsString("accessTokenSecret")
};
endpoint http:Listener listener{
port:9094
};
endpoint http:Client geo_Locator{
url: "http://query.yahooapis.com"
};
@http:ServiceConfig {
basePath: "/"
}
service<http:Service> findTag bind listener{
@http:ResourceConfig{
methods:["POST"],
path:"/"
}
message (endpoint caller, http:Request request){
http:Response response = new;
//http:Request req = new;
time:Time time = time:currentTime();
//string placeName = check request.getTextPayload();
//io:println(placeName);
//string encodedPlaceName = check http:encode(placeName, "UTF-8");
////userDefinedSecureOperation(untaint placeName);
//string requestPath = "/v1/public/yql?q=select* from geo.places where text%3D\"Colombo\"&format=json";
//io:println(requestPath);
//var response1 = geo_Locator->get(requestPath);
//match locationResponse {
// twitter:Location[] trendingLocations => {
// int i = 0;
// while (i<3){
// string placeName;
// try{
// placeName = <string>trendingLocations[i].name;
// } catch (error err){
// break;
// }
// io:println(placeName);
// var trendsResponse = twitter->getTopTrendsByPlace(trendingLocations[i].woeid);
// match trendsResponse{
// twitter:Trends[] trends => {
// int j = 0;
// while (j<5){
// io:println(trends[0].trends[j].name);
// io:println("----");
// j++;
// }
// }
// twitter:TwitterError e => io:println(e);
// }
// i++;
// io:println("---------------------------------------------");
// }
// }(year, month, day) = time.getDate();
// twitter:TwitterError e => io:println(e);
//}
int year;
int month;
int day;
(year, month, day) = time.getDate();
string status = "Top trends for hour " +time.hour() + " on "+day+"/"+month+"/"+year+":\n";
var trendsResponse = twitter->getTopTrendsByPlace(1);
match trendsResponse{
twitter:Trends[] trends => {
twitter:Trend[] topTrends = sort(trends[0].trends);
int j = 0;
while (j<10){
//io:println(trends[0].trends[j].name);
int tweetVolume = <int>topTrends[j].tweetVolume/1000;
string newRecord = j+1 +". " + <string>topTrends[j].name + " | "+ tweetVolume+"K tweets";
string temp = status + newRecord;
if(temp.length() > 270){
status+="\n";
break;
}
status += newRecord;
//io:println("----");
if(j!=9){
status+="\n";
}
j++;
}
}
twitter:TwitterError e => io:println(e);
}
io:println(status);
var tweetResponse = twitter -> tweet(status);
match tweetResponse{
twitter:Status tweet => {
response.setTextPayload("Tweet successful");
}
twitter:TwitterError e => {
response.setTextPayload("[ERROR] Status Code: " + e.statusCode + " | Message: "+ e.message);
}
}
_ = caller -> respond(response);
}
}
function sort(twitter:Trend[] unsortedArray) returns @untainted twitter:Trend[] {
twitter:Trend[] sortedArray = [];
int i=0;
int j=0;
twitter:Trend key;
while(i< lengthof unsortedArray){
key = unsortedArray[i];
j=i-1;
while(j>=0 && unsortedArray[j].tweetVolume < key.tweetVolume){
unsortedArray[j+1] = unsortedArray[j];
j-=1;
}
unsortedArray[j+1] = key;
i++;
}
return unsortedArray;
}