我想创建Azure Logic App,它会不断请求互联网上的特定网站并解析收到的HTML。
我已经创建了Logic App并设置了间隔和HTTP请求操作。
我应该选择哪种操作作为HTML代码上简单正则表达式操作的下一步?
我想到的是创建Azure Function来完成这项任务,但我想知道是否还有其他解决方案,更适合此类任务。
我希望它尽可能简单。
修改:
刚刚发现了一些很酷的功能。 Logic Apps包含一些基本类型的基本表达式。
不幸的是,它缺少任何regex
或string.contains
。
目前,我将尝试使用Azure功能。
答案 0 :(得分:1)
按以下方式创建Azure功能:
{
log.Info(" C#HTTP触发器功能处理了一个请求。");
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
// Set name to query string or body data
string input = data?.input.ToString();
var regexJson = data?.regexList;
var regexes = regexJson.ToObject<List<RegexReplace>>();
foreach (var regex in regexes)
{
var re = Regex.Replace(regex.Regex, "\\\\","\\");
var replace = Regex.Replace(regex.Replace, "\\\\","\\");
input = Regex.Replace(input, "\\\"","\"");
input = Regex.Replace(input, re, replace);
}
input = Regex.Replace(input, "[\\r\\n]", "");
return data.regexList == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, input, "application/json");
}
公共类RegexReplace
{
public string Regex { get; set; }
public string Replace { get; set; }
}
答案 1 :(得分:0)
你可能走在正确的轨道上。 Azure函数现在是实现此功能的最合适方式。 API应用程序是一个选项,但这是一个比您需要的更重的平台。
答案 2 :(得分:0)
我设法通过使用Workflow Definition Language和Azure提供的构建块来解决我的问题。
Azure功能理念并不是那么糟糕,适合任何更复杂的情况,但正如我所提到的,我希望它尽可能简单,所以现在就是这样。
这就是我的流程现在的样子。
为了完整起见,这里是JSON格式的流程
{
"$connections": {
"value": {
"wunderlist": {
"connectionId": "/subscriptions/.../providers/Microsoft.Web/connections/wunderlist",
"connectionName": "wunderlist",
"id": "/subscriptions/.../providers/Microsoft.Web/locations/northeurope/managedApis/wunderlist"
}
}
},
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Condition": {
"actions": {
"Create_a_task": {
"inputs": {
"body": {
"completed": false,
"list_id": 000000000,
"starred": true,
"title": "@{variables('today date')}"
},
"host": {
"connection": {
"name": "@parameters('$connections')['wunderlist']['connectionId']"
}
},
"method": "post",
"path": "/tasks",
"retryPolicy": {
"type": "none"
}
},
"limit": {
"timeout": "PT20S"
},
"runAfter": {},
"type": "ApiConnection"
},
"Set_a_reminder": {
"inputs": {
"body": {
"date": "@{addHours(utcNow(), 3)}",
"list_id": 000000,
"task_id": "@body('Create_a_task')?.id"
},
"host": {
"connection": {
"name": "@parameters('$connections')['wunderlist']['connectionId']"
}
},
"method": "post",
"path": "/reminders",
"retryPolicy": {
"type": "none"
}
},
"limit": {
"timeout": "PT20S"
},
"runAfter": {
"Create_a_task": [
"Succeeded"
]
},
"type": "ApiConnection"
}
},
"expression": "@contains(body('HTTP'), variables('today date'))",
"runAfter": {
"Initialize_variable": [
"Succeeded"
]
},
"type": "If"
},
"HTTP": {
"inputs": {
"method": "GET",
"uri": "..."
},
"runAfter": {},
"type": "Http"
},
"Initialize_variable": {
"inputs": {
"variables": [
{
"name": "today date",
"type": "String",
"value": "@{utcNow('yyyy/MM/dd')}"
}
]
},
"runAfter": {
"HTTP": [
"Succeeded"
]
},
"type": "InitializeVariable"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"Recurrence": {
"recurrence": {
"frequency": "Day",
"interval": 1,
"startTime": "2017-08-01T23:55:00Z",
"timeZone": "UTC"
},
"type": "Recurrence"
}
}
}
}
答案 3 :(得分:0)
这是我用来替换字符串中的文本的函数。这是可重用的,并且该方法可用于在Logic Apps中工作的许多类似方面:
WebSocketClient mWebsocketClient = new WebSocketClient(serverUri, new Draft_6455(), mHeaders, Constants.WEBSOCKET_CONFIG.CONNECTION_TIMEOUT) {
@Override
public void onOpen(ServerHandshake handshakeData) {
Log.d(TAG, "Opened");
mWebsocketClient.getConnection().
}
// Message does not come through if it exceeds a certain size
@Override
public void onMessage(String message) {
Log.i(TAG, message);
}
@Override
public void onClose(int code, String reason, boolean remote) {
Log.i(TAG, String.format("Closed. Code: %s, Reason: %s, Remote: %s", code, reason, remote));
}
@Override
public void onError(Exception ex) {
Log.i(TAG, "Error " + ex.getMessage());
}
}
然后我会从我的逻辑应用程序发布这样的对象:
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
dynamic data = await req.Content.ReadAsAsync<object>();
string removeme = data?.removeme;
string replacewith = data?.replacewith;
string value = data?.value;
return req.CreateResponse(HttpStatusCode.OK, value.Replace(removeme, replacewith));
}
...得到结果:“P-37378633”
答案 4 :(得分:0)
您可以在逻辑应用程序中使用内联代码操作来运行javascript正则表达式代码(预览-2019年5月)(Flow不支持)。