Skip to content

Instantly share code, notes, and snippets.

@iwa4
Created April 10, 2014 00:58
Show Gist options
  • Save iwa4/10334474 to your computer and use it in GitHub Desktop.
Save iwa4/10334474 to your computer and use it in GitHub Desktop.
携帯キャリアのモバイルIPアドレスを取得するやーつ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Sgml;
namespace SgmlReaderTest
{
internal class Program
{
private static void Main(string[] args)
{
// http://neue.cc/2010/03/02_244.html
var docomo = Docomo();
var au = Au();
var softbank = Softbank();
var willcom = Willcom();
var emobile = Emobile();
Console.WriteLine("処理終了。");
}
private static XDocument GetSgml(string href)
{
using (var sgml = new SgmlReader {Href = href, IgnoreDtd = true})
{
return XDocument.Load(sgml);
}
}
private static IEnumerable<string> Docomo()
{
var xml = GetSgml("https://www.nttdocomo.co.jp/service/developer/make/content/ip/index.html");
var ns = xml.Root.Name.Namespace;
return xml.Descendants(ns + "ul")
.Where(e => e.Attribute("class") != null && e.Attribute("class").Value == "normal txt")
.SelectMany(e => e.Descendants(ns + "li"))
.Select(e => e.Value)
.Distinct();
}
private static IEnumerable<string> Au()
{
var xml = GetSgml("http://www.au.kddi.com/ezfactory/tec/spec/ezsava_ip.html");
var ns = xml.Root.Name.Namespace;
var all = xml.Descendants(ns + "tr")
.Where(e => e.Attribute("bgcolor") != null && e.Attribute("bgcolor").Value == "#ffffff")
.SelectMany(e => e.Descendants(ns + "td"));
var ipAddress = all
.Where(e => Regex.IsMatch(e.Value, @"(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})"))
.Select((e, idx) => new {Idx = idx, e.Value});
var subnet = all
.Where(e => e.Value.StartsWith("/"))
.Select((e, idx) => new {Idx = idx, e.Value});
return ipAddress.Join(subnet, x => x.Idx, y => y.Idx, (x, y) => x.Value + y.Value);
}
private static IEnumerable<string> Softbank()
{
var xml = GetSgml("http://creation.mb.softbank.jp/mc/tech/tech_web/web_ipaddress.html");
var ns = xml.Root.Name.Namespace;
return xml.Descendants(ns + "table")
.Where(e => e.Attribute("class") != null && e.Attribute("class").Value == "onece_table")
.SelectMany(e => e.Descendants(ns + "th"))
.Select(e => e.Value)
.Distinct();
}
private static IEnumerable<string> Willcom()
{
var xml = GetSgml("http://www.willcom-inc.com/ja/service/contents_service/create/center_info/index.html");
var ns = xml.Root.Name.Namespace;
return xml.Descendants(ns + "table")
.Where(e => e.Attribute("class") != null && e.Attribute("class").Value == "plan03")
.SelectMany(e => e.Descendants(ns + "td"))
.Where(e => Regex.IsMatch(e.Value, @"^[0-9].*"))
.Select(e => e.Value)
.Distinct();
}
private static IEnumerable<string> Emobile()
{
var xml = GetSgml("http://developer.emnet.ne.jp/ipaddress.html");
var ns = xml.Root.Name.Namespace;
return xml.Descendants(ns + "table")
.SelectMany(e => e.Descendants(ns + "td"))
.Where(e => Regex.IsMatch(e.Value, @"^[0-9].*"))
.Select(e => e.Value)
.Distinct();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment