-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgethtmlmeta.fs
67 lines (52 loc) · 2.27 KB
/
gethtmlmeta.fs
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
namespace H3tech.Function
open System
open System.IO
open Microsoft.AspNetCore.Mvc
open Microsoft.Azure.WebJobs
open Microsoft.Azure.WebJobs.Extensions.Http
open Microsoft.AspNetCore.Http
open Newtonsoft.Json
open Microsoft.Extensions.Logging
open FSharp.Data
module gethtmlmeta =
[<AllowNullLiteral>]
type UrlContainer() =
member val Url = "" with get, set
[<Literal>]
let Url = "url"
[<FunctionName("gethtmlmeta")>]
let run ([<HttpTrigger(AuthorizationLevel.Function, "get", Route = null)>]req: HttpRequest) (log: ILogger) =
async {
log.LogInformation("F# HTTP trigger function processed a request.")
let urlOpt =
if req.Query.ContainsKey(Url) then
Some(req.Query.[Url].[0])
else
None
use stream = new StreamReader(req.Body)
let! reqBody = stream.ReadToEndAsync() |> Async.AwaitTask
let data = JsonConvert.DeserializeObject<UrlContainer>(reqBody)
let url =
match urlOpt with
| Some n -> n
| None ->
match data with
| null -> ""
| nc -> nc.Url
let results = HtmlDocument.Load(url)
let openGraphProperties =
results.Descendants ["meta"]
|> Seq.choose (fun x ->
x.TryGetAttribute("property")
|> Option.map (fun a -> a.Value(), x.AttributeValue("content"))
)
|> Seq.map (fun (a, b) -> a.Replace(":",""), b)
let authorAppValues =
Seq.map ((fun ((a: string), b) -> a.Replace(":",""), b) >> (fun (a, b) -> a.Replace("-",""), b)) (results.Descendants ["meta"]
|> Seq.choose (fun x ->
x.TryGetAttribute("name")
|> Option.map (fun a -> a.Value(), x.AttributeValue("content"))
))
let fullSeq = Seq.append openGraphProperties authorAppValues
return OkObjectResult(Map fullSeq) :> IActionResult
} |> Async.StartAsTask