using System.Linq;
public static class StarSignsUtil
{
///
/// 紀錄星座資料的模型
///
public class StarSignInfo
{
public string Title { get; set; }
public string Id { get; set; }
public string DateRange { get; set; }
}
///
/// 查詢後的回應資料
///
public class FateResult
{
public StarSignInfo StarSign { get; set; }
public string Date { get; set; }
public string Desc { get; set; }
public System.Collections.Generic.Dictionary Datas { get; set; }
public FateResult()
{
Datas = new System.Collections.Generic.Dictionary();
}
}
///
/// 星座資料
///
public static System.Collections.Generic.List Datas { get; set; }
//Ctor
static StarSignsUtil()
{
Datas = new System.Collections.Generic.List();
Datas.Add(new StarSignInfo { Title = "白羊座", Id = "aries", DateRange = "03/21-04/19" });
Datas.Add(new StarSignInfo { Title = "金牛座", Id = "taurus", DateRange = "04/20-05/20" });
Datas.Add(new StarSignInfo { Title = "雙子座", Id = "gemini", DateRange = "05/21-06/21" });
Datas.Add(new StarSignInfo { Title = "巨蟹座", Id = "cancer", DateRange = "06/22-07/22" });
Datas.Add(new StarSignInfo { Title = "獅子座", Id = "leo", DateRange = "07/23-08/22" });
Datas.Add(new StarSignInfo { Title = "處女座", Id = "virgo", DateRange = "08/23-09/22" });
Datas.Add(new StarSignInfo { Title = "天秤座", Id = "libra", DateRange = "09/23-10/23" });
Datas.Add(new StarSignInfo { Title = "天蠍座", Id = "scorpio", DateRange = "10/24-11/22" });
Datas.Add(new StarSignInfo { Title = "射手座", Id = "sagittarius", DateRange = "11/23-12/21" });
Datas.Add(new StarSignInfo { Title = "魔羯座", Id = "capricorn", DateRange = "12/22-01/19" });
Datas.Add(new StarSignInfo { Title = "水瓶座", Id = "aquarius", DateRange = "01/20-02/18" });
Datas.Add(new StarSignInfo { Title = "雙魚座", Id = "pisces", DateRange = "02/19-03/20" });
}
public static FateResult GetFateToday(string title)
{
var sInfo = Datas.SingleOrDefault(x => x.Title == title);
if (sInfo == null)
{
throw new System.Exception("此星座我沒有找到");
}
var result = new StarSignsUtil.FateResult();
result.StarSign = sInfo;
var wc = new System.Net.WebClient();
wc.Encoding = System.Text.Encoding.UTF8;
var source = wc.DownloadString("http://vip.astro.sina.com.cn/astro/view/" + sInfo.Id + "/");
var regex = new System.Text.RegularExpressions.Regex(@"
(?.*?)
(?.*?)
", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.MatchCollection matches = regex.Matches(source);
foreach (System.Text.RegularExpressions.Match match in matches)
{
if (match.Success)
{
var k = match.Groups["KEY"].Value.Replace(" ", "");
var v = match.Groups["VALUE"].Value.Replace(" ", "");
if (!string.IsNullOrEmpty(k) && !string.IsNullOrEmpty(v) && !result.Datas.ContainsKey(k))
{
result.Datas.Add(k, v);
}
}
}
regex = new System.Text.RegularExpressions.Regex(@"<p>(?.*?)</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline);
matches = regex.Matches(source);
foreach (System.Text.RegularExpressions.Match match in matches)
{
if (match.Success)
{
var v = match.Groups["VALUE"].Value.Trim();
if (!string.IsNullOrEmpty(v))
{
if (v.Contains(":"))
{
result.Datas.Add(v.Split(':')[0].Trim(), v.Split(':')[1].Trim());
}
else
{
result.Desc = v.Trim();
}
}
}
}
regex = new System.Text.RegularExpressions.Regex(@"有效日期:(?.*?)", System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline);
matches = regex.Matches(source);
foreach (System.Text.RegularExpressions.Match match in matches)
{
if (match.Success)
{
result.Date = match.Groups["VALUE"].Value.Trim();
}
}
return result;
}
}
呼叫方式也很簡單只要這樣 :
vargenimi=StarSignsUtil.GetFateToday("雙子座");
基本上自己做一下 cache 不要一直取,不然我相信很快就不能用了,基本上,能用多久我也不知道。
就自行取用吧 :)
--
Before you read this, the article was in a superposition of human and AI. Upon observation, the wave function collapsed into a sleep-deprived developer.