用戶:WJ-Bot/HWImageRefer
外觀
private void HighwayImageRefer()
{
string code_scanner_cmd = "{{#invoke:China Expwy Name|enumerate_road_ids}}";
Site zhwiki = new Site("zh.wikipedia.org", "WJ-Bot", "PASSWD");
string highway_code_str = zhwiki.ExpandTemplate(code_scanner_cmd);
var rs_split = from rstr in highway_code_str.Split(';')
select rstr.Split('|');
var rs_fnames = from rsp in rs_split
orderby rsp[0], rsp[2]
select new
{
Province = rsp[0],
Code = rsp[2],
ImageTerminal = rsp[1] + " Expwy " + rsp[2] + " sign terminal.svg",
ImageWithName = rsp[1] + " Expwy " + rsp[2] + " sign with name.svg",
ImageNoName = rsp[1] + " Expwy " + rsp[2] + " sign no name.svg"
};
var rs_fnames_by_province = (from rsp in rs_fnames
group rsp by rsp.Province into prow
select prow).ToDictionary(v => v.Key, v => v.ToList());
Site commonswiki = new Site("commons.wikimedia.org", "WJ-Bot", "PASSWD");
List<string> prov_items = new List<string>();
foreach (string province in rs_fnames_by_province.Keys)
{
List<string> road_items = new List<string>();
foreach (var rdef in rs_fnames_by_province[province])
{
List<string> comments = new List<string>();
Page pg = new Page(commonswiki, "File:" + rdef.ImageWithName);
pg.Load();
if (pg.text != null && pg.text != "")
comments.Add("with_name = true");
Page pg2 = new Page(commonswiki, "File:" + rdef.ImageNoName);
pg2.Load();
if (pg2.text != null && pg2.text != "")
comments.Add("no_name = true");
Page pg3 = new Page(commonswiki, "File:" + rdef.ImageTerminal);
pg3.Load();
if (pg3.text != null && pg3.text != "")
comments.Add("terminal = true");
if (comments.Any())
road_items.Add(" " + rdef.Code + " = { " + string.Join(", ", comments.ToArray()) + " }");
}
if (road_items.Any())
prov_items.Add(" " + province + " = {\n" + string.Join(",\n", road_items.ToArray()) + "\n }");
}
string content = "return {\n" + string.Join(",\n", prov_items.ToArray()) + "\n}";
Page module_page = new Page(zhwiki, "Module:China Expwy Name/ImageRefs");
module_page.Save(content, "机器人:更新图片引用数据", false);
}