-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExecutionHandler.ds
114 lines (113 loc) · 3.41 KB
/
ExecutionHandler.ds
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
/*
On typing the /medium command on the chat window, the user suggestions of -@Username and -Publication pops up.
*/
flag = false;
message = Map();
if(arguments == "") // The user is prompted to choose either of the suggestions.
{
message.put("text","Please choose from the _options_ and enter something you want search as input. Usage \"/medium *option* free-code-camp\"");
return message;
}
/*
https://rss2json.com/, is an online rss to json convertor used to convert rss feed from medium.com to json format.
*/
if(selections.size() > 0)
{
details = selections.get(0).toMap();
if(details.get("title").containsIgnoreCase("Username"))
{
url = "https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/@" + encodeUrl(arguments);
url = url.removeFirstOccurence("+");
flag = true;
}
else if(details.get("title").containsIgnoreCase("Publication"))
{
url = "https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/" + encodeUrl(arguments);
url = url.removeFirstOccurence("+");
}
}
else
{
url = "https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/" + encodeUrl(arguments);
}
response = getUrl(url,{"Content-Encoding":"gzip"});
status = response.toMap().get("status");
/*
If the status returned in json is "ok" then the required keys are obtained from json.
*/
if(status == "ok")
{
itemsList = response.toMap().get("items");
feed = response.toMap().get("feed");
title = feed.toMap().get("title");
image = feed.toMap().get("image");
slidesList = list();
slidesList0 = Map();
slidesList0.put("type","images");
dataList = list();
dataList.add(image);
slidesList0.put("data",dataList);
slidesList.add(slidesList0);
message.put("slides",slidesList);
text = "*There are no " + title + "*\n";
if(itemsList.isEmpty())
{
message.put("text",text);
return message;
}
count = 0;
response = "*" + title + "*\n";
indexes = {"1️⃣","2️⃣","3️⃣","4️⃣","5️⃣","6️⃣","7️⃣","8️⃣","9️⃣","🔟"};// displaying first 10 items
for each item in itemsList
{
l = item.get("link");
link = l.replaceFirst("@","%40"); // converting special character @ to %40 for proper rendering of url.
response = response + indexes.get(count) + " [" + item.get("title") + "](" + link + ") " + "\n";
count = count + 1;
if(count > 9)
{
break;
}
}
message.put("text",response);
return message;
}
else if(flag == true)
{
/*
use google custom search api to search medium.com specifically.
*/
url = "https://www.googleapis.com/customsearch/v1?key={YOUR KEY}&cx=002084031288054668458:vcly9iguf3p&q=" + encodeUrl(arguments);
response = getUrl(url,{"Content-Encoding":"gzip"});
itemsList = "";
res = response.toMap().get("searchInformation");
r = res.get("totalResults");
if(r == 0)
{
message.put("text","*No such _User/Publication_ exists in medium*");
return message;
}
itemsList = response.toMap().get("items");
count = 0;
response = "*User name, not found! Maybe try any of these.*" + "\n";
indexes = {"1️⃣","2️⃣","3️⃣","4️⃣","5️⃣"};
for each item in itemsList
{
l = item.get("link");
result = l.removeFirstOccurence("https://medium.com/@");
response = response + indexes.get(count) + result + "\n";
count = count + 1;
if(count > 4)
{
break;
}
}
message.put("text",response);
return message;
}
else
{
response = "*Publication name, not found in medium! Enter a valid publication *" + "\n";
message.put("text",response);
return message;
}