fork download
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Xml.Linq;
  7. // System.Web
  8. using System.Net;
  9. // SGMLReader
  10. // http://d...content-available-to-author-only...h.com/SgmlReader
  11. using Sgml;
  12.  
  13. namespace Ranking
  14. {
  15. class Program
  16. {
  17. static void Main(string[] args)
  18. {
  19. string rankingUrl = "http://w...content-available-to-author-only...o.jp/ranking";
  20.  
  21. var doc = LoadHtml(rankingUrl);
  22.  
  23. var ranking = from x in doc.Descendants("div")
  24. let a = x.Attribute("class")
  25. where a != null && a.Value == "ranking_box"
  26. // <a href="watch/sm10000000"... → sm10000000
  27. select x.Descendants("a").First().Attribute("href").Value.Substring(6);
  28.  
  29. /* ranking.xml
  30.   * <ranking>
  31.   * <video id="sm*">
  32.   * <tag>*</tag>...
  33.   * </video>...
  34.   * </ranking>
  35.   */
  36. var cache = new XDocument(
  37. new XElement("ranking",
  38. from id in ranking
  39. select new XElement("video",
  40. new XAttribute("id", id),
  41. from tag in GetTags(id)
  42. select new XElement("tag") { Value = tag })));
  43.  
  44. cache.Save("ranking.xml");
  45. }
  46.  
  47. static IEnumerable<string> GetTags(string videoId)
  48. {
  49. string getthumbinfoUrl = "http://e...content-available-to-author-only...o.jp/api/getthumbinfo/";
  50.  
  51. var doc = XDocument.Load(getthumbinfoUrl + videoId);
  52. return from x in doc.Descendants("tag")
  53. select x.Value;
  54. }
  55.  
  56. static XDocument LoadHtml(string url)
  57. {
  58. using (var stream = new WebClient().OpenRead(url))
  59. using (var reader = new StreamReader(stream, Encoding.UTF8))
  60. {
  61. return ParseHtml(reader);
  62. }
  63. }
  64.  
  65. static XDocument ParseHtml(TextReader reader)
  66. {
  67. var sgmlReader = new SgmlReader()
  68. {
  69. DocType = "HTML",
  70. WhitespaceHandling = WhitespaceHandling.All,
  71. CaseFolding = CaseFolding.ToLower,
  72. InputStream = reader,
  73. };
  74. return XDocument.Load(sgmlReader);
  75. }
  76. }
  77. }
  78.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty