90 lines
3.6 KiB
C#
90 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Reflection;
|
|
using System.Text.RegularExpressions;
|
|
using System.Windows.Forms;
|
|
|
|
namespace StationDataFileGenerator
|
|
{
|
|
using Newtonsoft.Json;
|
|
|
|
static class Program
|
|
{
|
|
/// <summary>
|
|
/// 应用程序的主入口点。
|
|
/// </summary>
|
|
[STAThread]
|
|
static void Main()
|
|
{
|
|
Application.EnableVisualStyles();
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
|
|
var targetDataFile = Path.Combine(_root, @"..\Web12306\js\station\station_data.js");
|
|
var inputDataFile = Path.Combine(_root, @"station_name.js");
|
|
var inputPopFile = Path.Combine(_root, @"favorite_name.js");
|
|
var inputTimeFile = Path.Combine(_root, "time.js");
|
|
|
|
//下载数据
|
|
ServicePointManager.Expect100Continue = false;
|
|
ServicePointManager.ServerCertificateValidationCallback = (x, y, z, d) =>
|
|
{
|
|
return true;
|
|
};
|
|
Console.WriteLine("正在下载站点列表..");
|
|
var client = new WebClient();
|
|
client.DownloadFile("https://kyfw.12306.cn/otn/resources/js/framework/station_name.js", inputDataFile);
|
|
client.DownloadFile("https://kyfw.12306.cn/otn/resources/js/framework/favorite_name.js", inputPopFile);
|
|
client.DownloadFile("https://kyfw.12306.cn/otn/resources/js/query/qss.js", inputTimeFile);
|
|
|
|
Console.WriteLine("正在处理....");
|
|
//常规数据
|
|
var commonDataText = File.ReadAllText(inputDataFile);
|
|
var popDataText = File.ReadAllText(inputPopFile);
|
|
var timeDataText = File.ReadAllText(inputTimeFile);
|
|
|
|
//@bjb|北京北|VAP|beijingbei|bjb|0
|
|
//@bji|北京|BJP|0
|
|
var cities = new Regex(@"@([a-z]+)\|([^\|]+)\|([^\|]+)\|([^\|@]+)\|([^\|@]+)\|([^\|@'""]+)", RegexOptions.IgnoreCase).Matches(commonDataText)
|
|
.Cast<Match>().Select(s => new { p = s.Groups[4].Value.ToLower(), n = s.Groups[2].Value.ToLower(), c = s.Groups[3].Value, s = int.Parse(s.Groups[6].Value), h = s.Groups[5].Value.ToLower() });
|
|
var commonData = cities.GroupBy(s => GetCodeKey(s.h)).ToDictionary(s => s.Key, s => s.OrderBy(x => x.s).ToDictionary(x => x.c))
|
|
;
|
|
var popData = new Regex(@"@([a-z]+)\|([^\|]+)\|([^\|]+)\|([^\|@]+)", RegexOptions.IgnoreCase).Matches(popDataText)
|
|
.Cast<Match>().Select(s => s.Groups[3].Value).ToArray();
|
|
;
|
|
var timeData = new Regex(@"""([^""]+)""\s*:\s*""([^""]+)""").Matches(timeDataText)
|
|
.Cast<Match>()
|
|
.Select(s => new { n = s.Groups[1].Value, t = s.Groups[2].Value })
|
|
.GroupBy(s => s.n)
|
|
.ToDictionary(s =>
|
|
{
|
|
var c = cities.FirstOrDefault(x => x.n == s.Key);
|
|
if (c == null)
|
|
{
|
|
Console.WriteLine("警告:站点【" + s.Key + "】不存在!");
|
|
return s.Key;
|
|
}
|
|
return c.c;
|
|
}, s => s.First().t);
|
|
|
|
//fix: remove invalid popcity
|
|
popData = popData.Where(s => cities.Any(x => x.c == s)).ToArray();
|
|
|
|
File.WriteAllText(targetDataFile, "define(function(require, exports, module){\r\n exports.data=" + JsonConvert.SerializeObject(commonData) + ";\r\n exports.popcity=" + JsonConvert.SerializeObject(popData) + ";\r\n exports.sellTime=" + JsonConvert.SerializeObject(timeData) + ";\r\n});");
|
|
|
|
Console.Write("OK.");
|
|
Console.ReadKey();
|
|
}
|
|
static string _root = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
|
static string[] _groupStrings = new[] { "A-E", "F-J", "K-O", "P-T", "U-Z" };
|
|
static string GetCodeKey(string code)
|
|
{
|
|
var fl = char.ToUpper(code[0]);
|
|
|
|
return _groupStrings.First(s => s[0] <= fl && s[2] >= fl);
|
|
}
|
|
}
|
|
}
|