简体   繁体   English

解析 Azure 逻辑应用中的文本

[英]Parse text in Azure Logic Apps

I want to create Azure Logic App which will constantly request a specific website on the Internet and parse received HTML.我想创建Azure 逻辑应用程序,它将不断请求 Internet 上的特定网站并解析收到的 HTML。

I've created Logic App and set up interval and HTTP request action.我已经创建了逻辑应用程序并设置了间隔和 HTTP 请求操作。

Which action should I choose as the next step for simple regex operation on HTML code?我应该选择哪个操作作为对 HTML 代码进行简单正则表达式操作的下一步?

What comes to my mind is creating Azure Function which will do the job, but I wonder if there is any other solution, more suitable for such task.我想到的是创建可以完成这项工作的Azure 函数,但我想知道是否还有其他解决方案,更适合此类任务。

I want it the be simple as possible.我希望它尽可能简单。


Edit:编辑:

Just found out some cool feature.刚刚发现了一些很酷的功能。 Logic Apps contain some basic expressions for primitive types.逻辑应用包含一些基本类型的基本表达式。

Unfortunetly it lacks of any regex or string.contains .不幸的是,它缺少任何regexstring.contains

For now, I'll try with Azure Functions.现在,我将尝试使用 Azure Functions。

I've managed to solve my problem with use of Workflow Definition Language and building blocks provided by Azure.我已经设法使用工作流定义语言和 Azure 提供的构建块解决了我的问题。

The Azure Function idea was not that bad and would fit perfectly for any more complex case, but as I mentioned, I wanted it as simple as possible, so here it is. Azure Function 的想法并没有那么糟糕,并且非常适合任何更复杂的情况,但正如我所提到的,我希望它尽可能简单,所以就在这里。

This is how my flow looks now.这就是我的流程现在的样子。

For sake of completeness, here is the flow in JSON format为了完整起见,这里是 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"
            }
        }
    }
}

You can use inline code action in logic app to run javascript regex code (preview- May 2019) (Not supported on Flow).您可以在逻辑应用程序中使用内联代码操作来运行 javascript 正则表达式代码(预览版 - 2019 年 5 月)(Flow 不支持)。

Iniline Code内联代码

Logic App Inline Code Ref 逻辑应用内联代码参考

You're probably on the right track.你可能走在正确的轨道上。 An Azure Function would be the most appropriate way to implement this right now. Azure 函数将是目前最合适的实现方式。 An API App is an option but that's a heavier platform than you need. API 应用程序是一种选择,但这是一个比您需要的更重的平台。

create an Azure Function along the lines of:按照以下方式创建 Azure 函数:

{ {

log.Info("C# HTTP trigger function processed a request."); 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");

} }

public class RegexReplace公共类正则表达式替换

{ {

public string Regex { get; set; }

public string Replace { get; set; }

} }

This is my function to use for replacing text in a string.这是我用于替换字符串中文本的函数。 this is reusable and the approach can be used for many similar type of aspects of working in Logic Apps:这是可重用的,并且该方法可用于在逻辑应用程序中工作的许多类似类型的方面:

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));
}

I would then post an object like this from my logic app:然后我会从我的逻辑应用程序中发布一个这样的对象:

{
    "removeme": "SKU-",
    "replacewith": "P-",
    "value": "SKU-37378633"
}

... to get the result: "P-37378633" ...得到结果:“P-37378633”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM