-
Notifications
You must be signed in to change notification settings - Fork 1
/
MeldLinkParser.cs
66 lines (57 loc) · 2.33 KB
/
MeldLinkParser.cs
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
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Media;
using AutoMelder.MeldingLogic;
using LlamaLibrary.Logging;
using Newtonsoft.Json.Linq;
using static AutoMelder.Ariyala.AriyalaParser;
using static AutoMelder.Etro.EtroParser;
namespace AutoMelder
{
public static class MeldLinkParser
{
private static readonly LLogger Log = new LLogger("UriParser", Colors.Coral);
public static readonly Regex AriyalaRegex = new Regex(@"(?<code>[A-Z0-9]{4,8})[\r\n\/]*$$");
public static readonly Regex EtroRegex = new Regex(@"(?<code>[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})[\r\n\/]*$");
public static MeldRequest ParseLinkOrId(string linkOrId)
{
Match etroMatch = EtroRegex.Match(linkOrId);
if (etroMatch.Success)
{
return GetEtroMeldInfo(etroMatch.Groups["code"].Value);
}
Match ariyalaMatch = AriyalaRegex.Match(linkOrId);
if (ariyalaMatch.Success)
{
return GetAriyalaMeldInfo(ariyalaMatch.Groups["code"].Value);
}
Log.Error($"Couldn't parse {linkOrId}!");
return MeldRequest.Empty;
}
public static bool TryGetResponse(string uri, out JObject jObject)
{
jObject = null;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.ContentType = "text/json; charset=utf-8";
HttpWebResponse response = httpWebRequest.GetResponse() as HttpWebResponse;
if (response == null || response.StatusCode != HttpStatusCode.OK)
{
Log.Error($"Couldn't get a response from {uri}. StatusCode: {response?.StatusCode}");
return false;
}
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
jObject = JObject.Parse(reader.ReadToEnd());
}
if (jObject == null || jObject.Count == 0)
{
Log.Error($"Couldn't parse the retrieved json. URI: {uri}");
return false;
}
return true;
}
}
}