-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransitcardGrabber.cs
322 lines (286 loc) · 15.1 KB
/
TransitcardGrabber.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using System;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using Newtonsoft.Json;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Core;
namespace TransitcardGrabber
{
class TransitcardGrabber
{
// All is very simple
// Plugin Must return fileName in last line (or in single line)
// if last line (or single) is empty - file is not exists
static void Main(string[] args)
{
try
{
double left = -180.0;
double right = 180.0;
double bottom = -90.0;
double top = 90.0;
if(!String.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("MAPLEFT")))
double.TryParse(System.Environment.GetEnvironmentVariable("MAPLEFT"), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out left);
if (!String.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("MAPRIGHT")))
double.TryParse(System.Environment.GetEnvironmentVariable("MAPRIGHT"), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out right);
if (!String.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("MAPBOTTOM")))
double.TryParse(System.Environment.GetEnvironmentVariable("MAPBOTTOM"), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out bottom);
if (!String.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("MAPTOP")))
double.TryParse(System.Environment.GetEnvironmentVariable("MAPTOP"), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out top);
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("*** locator.transitcard.ru grabber ***");
Console.WriteLine("Getting gas stations from locator.transitcard.ru");
Console.WriteLine(String.Format(System.Globalization.CultureInfo.InvariantCulture, "BOX [[{0}:{1}],[{2}:{3}]]", left, bottom, right, top));
string url = "http://locator.transitcard.ru/web/v1/point/transpose-list?countryCode=ru";
Console.WriteLine(url);
Console.WriteLine("Grabbing ...");
string res = HTTPCall.ViaTCP(url);
Console.WriteLine(" ... {0} bytes OK", res.Length);
Console.WriteLine("Parsing ... ");
Response obj = JsonConvert.DeserializeObject<Response>(res);
Console.WriteLine(" ... {0} objects OK", obj.size);
Console.WriteLine("Saving to file ... ");
res = obj.ToKMLFile();
Console.WriteLine(" ... OK");
bool SAVE_2_KMZ = true;
if (SAVE_2_KMZ)
{
string df = res;
Console.WriteLine("Preparing KMZ ... ");
res = Response.ToKMZFile();
File.Delete(df);
Console.WriteLine(" ... OK");
};
Console.WriteLine("Data saved to file: ");
Console.WriteLine(res);
}
catch (Exception ex)
{
Console.WriteLine("Error: ");
Console.WriteLine(ex.ToString());
Console.WriteLine();
};
}
public static class HTTPCall
{
public static string ViaNet(string url)
{
string regexPattern = @"^(?<proto>[^:/\?#]+)://(?:(?<user>[^@:]*):?(?<pass>[^@]*)@)?(?<host>[^@/\?#:]*)?:?(?<port>\d*)(?<path>[^@\?#]*)(?<ask>\?(?<query>[^#]*))?(?<sharp>#(?<hash>.*))?";
Match m = (new Regex(regexPattern, RegexOptions.IgnoreCase)).Match(url);
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(url);
wreq.Referer = "http://" + m.Groups["host"].Value + "/";
wreq.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:52.0) Gecko/20100101 Firefox/52.0";
HttpWebResponse wres = (HttpWebResponse)wreq.GetResponse();
System.IO.Stream wr = wres.GetResponseStream();
StreamReader sr = new StreamReader(wr, Encoding.GetEncoding(wres.ContentEncoding));
string res = sr.ReadToEnd();
sr.Close();
wr.Close();
wres.Close();
return res;
}
public static string ViaTCP(string url)
{
string regexPattern = @"^(?<proto>[^:/\?#]+)://(?:(?<user>[^@:]*):?(?<pass>[^@]*)@)?(?<host>[^@/\?#:]*)?:?(?<port>\d*)(?<path>[^@\?#]*)(?<ask>\?(?<query>[^#]*))?(?<sharp>#(?<hash>.*))?";
Match m = (new Regex(regexPattern, RegexOptions.IgnoreCase)).Match(url);
string host = m.Groups["host"].Value;
int port = 80;
if (!String.IsNullOrEmpty((m.Groups["port"].Value))) port = int.Parse(m.Groups["port"].Value);
string path = m.Groups["path"].Value + m.Groups["ask"].Value;
System.Net.Sockets.TcpClient tcpc = new System.Net.Sockets.TcpClient();
tcpc.Connect(host, port);
System.IO.Stream tcps = tcpc.GetStream();
//int unixTime = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
string data = "GET " + path + " HTTP/1.0\r\n";
data += "Accept: text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01\r\n";
data += "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3\r\n";
data += "Connection: close\r\n";
data += "Host: " + host + "\r\n";
data += "Referer: http://" + host + "/\r\n";
data += "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:52.0) Gecko/20100101 Firefox/52.0\r\n";
data += "\r\n";
byte[] buff = Encoding.ASCII.GetBytes(data);
tcps.Write(buff, 0, buff.Length);
List<byte> rcvd = new List<byte>();
buff = new byte[4];
tcps.Read(buff, 0, 4);
rcvd.AddRange(buff);
while (Encoding.ASCII.GetString(rcvd.ToArray(), rcvd.Count - 4, 4) != "\r\n\r\n")
rcvd.Add((byte)tcps.ReadByte());
Encoding enc = Encoding.ASCII;
string headers = enc.GetString(rcvd.ToArray());
Regex rx = new Regex(@"Content-Type:\s{0,1}([^;]+)(;\s{0,1}charset=(.+)){0,}");
Match mx = rx.Match(headers);
if ((mx.Success) && (!String.IsNullOrEmpty((mx.Groups[3].Value.Trim('\r')))))
enc = Encoding.GetEncoding(mx.Groups[3].Value.Trim('\r'));
StreamReader sr = new StreamReader(tcps, enc);
string res = sr.ReadToEnd();
sr.Close();
tcps.Close();
tcpc.Close();
return res;
}
}
public class Response
{
public string[] id;
public double[] latitude;
public double[] longitude;
public string[] brand;
public int[] brandId;
public int[] typeNetwork;
public bool[] brandIconExists;
public bool[] fuelStation;
public bool[] gasStation;
public bool[] washing;
public bool[] closed;
public int[] propertiesType;
public bool[] enabled;
public int size;
public AZS[] list
{
get
{
AZS[] res = new AZS[this.size];
for (int i = 0; i < this.size; i++)
res[i] = AZS.FromResponse(this, i);
return res;
}
}
public string ToKMLFile()
{
if (this.size < 1) return "";
string fileName = System.AppDomain.CurrentDomain.BaseDirectory + @"\TransitcardGrabber.kml";
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
StreamWriter sb = new StreamWriter(fs, Encoding.UTF8);
sb.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.WriteLine("<kml>");
sb.WriteLine("<Document>");
sb.WriteLine("<name>locator.transitcard.ru</name>");
sb.WriteLine("<createdby>locator.transitcard.ru grabber</createdby>");
Dictionary<int, Brand> brandlist = new Dictionary<int, Brand>();
for (int i = 0; i < this.size; i++)
{
int bid = this.brandId[i];
if (brandlist.ContainsKey(bid))
{
Brand b = brandlist[bid];
b.count++;
brandlist[bid] = b;
}
else
brandlist.Add(bid, new Brand(bid, this.brand[i]));
};
foreach (KeyValuePair<int, Brand> b in brandlist)
{
sb.WriteLine(String.Format("<Folder><name><![CDATA[{0} (Count: {1})]]></name>", b.Value.name, b.Value.count));
int cnt = 0;
Response.AZS[] alist = this.list;
foreach (Response.AZS a in alist)
{
if (a.brandId != b.Value.id) continue;
sb.WriteLine("<Placemark>");
sb.WriteLine(String.Format("<styleUrl>#brand{0}</styleUrl>", a.brandId));
sb.WriteLine(String.Format("<name><![CDATA[{0} - {1}]]></name>", a.brand, ++cnt));
sb.WriteLine(String.Format("<description><![CDATA[ID: {0},\r\nClosed: {1},\r\nFuel: {2},\r\nGaz: {3},\r\nWash: {4}\r\nBrand: {5} - {6}]]></description>", a.id, a.closed, a.fuelStation, a.gasStation, a.washing, a.brandId, a.brand));
sb.WriteLine(String.Format(System.Globalization.CultureInfo.InvariantCulture, "<Point><coordinates>{1},{0},0</coordinates></Point>", a.latitude, a.longitude));
sb.WriteLine("</Placemark>");
};
sb.WriteLine("</Folder>");
};
foreach (KeyValuePair<int, Brand> b in brandlist)
sb.WriteLine(String.Format("<Style id=\"brand{0}\"><IconStyle><Icon><href>images/brand{0}.png</href></Icon></IconStyle></Style>", b.Value.id));
sb.WriteLine("</Document>");
sb.WriteLine("</kml>");
sb.Close();
return fileName;
}
public class AZS
{
public string id;
public double latitude;
public double longitude;
public string brand;
public int brandId;
public int typeNetwork;
public bool brandIconExists;
public bool fuelStation;
public bool gasStation;
public bool washing;
public bool closed;
public int propertiesType;
public bool enabled;
private AZS() { }
public static AZS FromResponse(Response resp, int num)
{
AZS res = new AZS();
res.brand = resp.brand[num];
res.brandIconExists = resp.brandIconExists[num];
res.brandId = resp.brandId[num];
res.closed = resp.closed[num];
res.enabled = resp.enabled[num];
res.fuelStation = resp.fuelStation[num];
res.gasStation = resp.gasStation[num];
res.id = resp.id[num];
res.latitude = resp.latitude[num];
res.longitude = resp.longitude[num];
if(resp.propertiesType != null) res.propertiesType = resp.propertiesType[num];
res.typeNetwork = resp.typeNetwork[num];
res.washing = resp.washing[num];
return res;
}
}
public struct Brand
{
public int id;
public string name;
public int count;
public Brand(int id, string name) { this.id = id; this.name = name; this.count = 1; }
}
public static string ToKMZFile()
{
string fileName = System.AppDomain.CurrentDomain.BaseDirectory + @"\TransitcardGrabber.kmz";
FileStream fsOut = File.Create(fileName);
ZipOutputStream zipStream = new ZipOutputStream(fsOut);
zipStream.SetComment("Created by locator.transitcard.ru grabber");
zipStream.SetLevel(3);
// doc.kml
{
FileInfo fi = new FileInfo(System.AppDomain.CurrentDomain.BaseDirectory + @"\TransitcardGrabber.kml");
ZipEntry newEntry = new ZipEntry("doc.kml");
newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity
newEntry.Size = fi.Length;
zipStream.PutNextEntry(newEntry);
byte[] buffer = new byte[4096];
using (FileStream streamReader = File.OpenRead(fi.FullName))
StreamUtils.Copy(streamReader, zipStream, buffer);
zipStream.CloseEntry();
};
// images
{
string[] files = Directory.GetFiles(System.AppDomain.CurrentDomain.BaseDirectory + @"\images");
foreach (string filename in files)
{
FileInfo fi = new FileInfo(filename);
ZipEntry newEntry = new ZipEntry(@"images\" + fi.Name);
newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity
newEntry.Size = fi.Length;
zipStream.PutNextEntry(newEntry);
byte[] buffer = new byte[4096];
using (FileStream streamReader = File.OpenRead(filename))
StreamUtils.Copy(streamReader, zipStream, buffer);
zipStream.CloseEntry();
}
};
zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream
zipStream.Close();
return fileName;
}
}
}
}