File size: 2,184 Bytes
a431caa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using System;
using System.Threading.Tasks;
using System.Text.Json;
using System.Text.Json.Nodes;

class BatchCoder
{
    static async Task Main(string[] args)
    {
        // read JSON input
        JsonObject input = JsonNode.Parse(Console.OpenStandardInput()).AsObject();

        // initialize Web Services and results objects
        JsonObject config = input["config"].AsObject();
        OnetWebService onet_ws = new OnetWebService(config["username"].GetValue<string>(), config["password"].GetValue<string>());
        int max_results = 1;
        if (config.ContainsKey("max_results"))
        {
            max_results = Math.Max(1, config["max_results"].GetValue<int>());
        }
        JsonObject output = new JsonObject
        {
            ["output"] = new JsonArray { }
        };

        // call keyword search for each input query
        foreach (string q in input["queries"].AsArray())
        {
            JsonArray res = new JsonArray { };
            OnetWebService.QueryParams query = new OnetWebService.QueryParams()
            {
                { "keyword", q },
                { "end", max_results.ToString() },
            };
            JsonObject kwresults = (await onet_ws.Call("online/search", query)).AsObject();
            if (kwresults.ContainsKey("occupation") && kwresults["occupation"].AsArray().Count > 0)
            {
                foreach (JsonNode occnode in kwresults["occupation"].AsArray())
                {
                    JsonObject occ = occnode.AsObject();
                    JsonObject ores = new JsonObject
                    {
                      ["code"] = occ["code"].GetValue<string>(),
                      ["title"] = occ["title"].GetValue<string>()
                    };
                    res.Add(ores);
                }
            }
            JsonObject qres = new JsonObject
            {
              ["query"] = q,
              ["results"] = res
            };
            output["output"].AsArray().Add(qres);
        }

        Console.WriteLine(output.ToJsonString(new JsonSerializerOptions { WriteIndented = true }));
    }
}