Created
July 10, 2018 07:12
-
-
Save donma/c92bb400727643d3083e9e8cefd1f44c to your computer and use it in GitHub Desktop.
C# Get Star Sign Fate Today Utility From Sina
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
public static class StarSignsUtil | |
{ | |
/// <summary> | |
/// 紀錄星座資料的模型 | |
/// </summary> | |
public class StarSignInfo | |
{ | |
public string Title { get; set; } | |
public string Id { get; set; } | |
public string DateRange { get; set; } | |
} | |
/// <summary> | |
/// 查詢後的回應資料 | |
/// </summary> | |
public class FateResult | |
{ | |
public StarSignInfo StarSign { get; set; } | |
public string Date { get; set; } | |
public string Desc { get; set; } | |
public System.Collections.Generic.Dictionary<string, string> Datas { get; set; } | |
public FateResult() | |
{ | |
Datas = new System.Collections.Generic.Dictionary<string, string>(); | |
} | |
} | |
/// <summary> | |
/// 星座資料 | |
/// </summary> | |
public static System.Collections.Generic.List<StarSignInfo> Datas { get; set; } | |
//Ctor | |
static StarSignsUtil() | |
{ | |
Datas = new System.Collections.Generic.List<StarSignInfo>(); | |
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(@"<div class=[\s""']tab[\s""']><h4>(?<KEY>.*?)</h4><p>(?<VALUE>.*?)</p>", 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>(?<VALUE>.*?)</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(@"有效日期:(?<VALUE>.*?)</li>", 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; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
var gemini = StarSignsUtil.GetFateToday("雙子座");