summaryrefslogtreecommitdiff
path: root/StubGen/StubGen/Scrapers/ScrapedClass.cs
blob: 210bb6c333422233133fcc0463389bc3f9b754c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System.Collections.Generic;
using System.Net.Http;
using System.Linq;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using HtmlAgilityPack;

namespace StubGen
{
    public static class Extensions
    {
        public static string RealInnerText(this HtmlNode node)
        {
            if (node == null)
            {
                return "";
            }
            return Regex.Replace(node.InnerText, "<.*?>", "").Replace("&gt;", ">").Replace("&lt;", "<").Replace("NOfalse", "false").Replace("YEStrue", "true").Trim();
        }

        public static string[] Split(this string str, string splitAt)
        {
            return Regex.Split(str, Regex.Escape(splitAt));
        }

        public static string[] SplitAtFirstOccurrence(this string str, string splitAt)
        {
            return new[] {str.Split(splitAt)[0], str.Substring(str.IndexOf(splitAt) + 1)};
        }

        public static string[] SplitAtFirstOccurrence(this string str, char splitAt)
        {
            return new[] { str.Split(splitAt)[0], str.Substring(str.IndexOf(splitAt) + 1) };
        }
    }

    public class ScrapedClass : ScrapedMember
    {
        public static double? ParseAvailability(string text)
        {
            if (text == null) return null;

            try
            {
                var number = Regex.Split(text, "in iOS ")[1].Split(' ')[0];
                double version;
                if (!double.TryParse(number, out version))
                {
                    return null;
                }
                return version;
            }
            catch
            {
                return null;
            }
        }

        public string Inherits { get; set; }
        public IEnumerable<string> ConformsTo { get; set; }
        public List<ScrapedMember> Members { get; set; }
        public string Namespace { get; set; }
        public string Name { get; set; }

        public ScrapedClass() { }

        public ScrapedClass(string url)
        {
            using (var client = new HttpClient())
            {
                var data = client.GetStringAsync(url).Result;
                var doc = new HtmlDocument
                {
                    OptionFixNestedTags = true
                };
                doc.LoadHtml(data);

                var inheritanceTree = doc.DocumentNode.SelectNodes("//div[@class='inheritance']/ul[@class='Swift']/li/code");
                if (inheritanceTree != null)
                {
                    for (var i = 1; i < inheritanceTree.Count; i++)
                    {
                        if (inheritanceTree[i].SelectSingleNode("./a") == null)
                        {
                            //The current node isn't a link
                            Inherits = inheritanceTree[i - 1].RealInnerText().Trim();
                        }
                    }
                }
                else
                {
                    Inherits = null;
                }

                var conformsTo =
                    doc.DocumentNode.SelectNodes("//div[@class='conforms-to']/ul/li[@class='Swift']/code");
                ConformsTo = conformsTo == null ? new string[0] : conformsTo.Select(node => node.RealInnerText().Trim());

                var description =
                    doc.DocumentNode.SelectSingleNode("//section[@class='z-class-description section']/p")
                        .RealInnerText().Replace("More...", "").Trim();

                Description = description ?? "";
                if (Description == "")
                {
                   var desc =
                   doc.DocumentNode.SelectSingleNode("//section[@class='z-protocol-description section']/p")
                       .RealInnerText().Replace("More...", "").Trim();

                    Description = desc ?? "";
                }

                var availability =
                    doc.DocumentNode.SelectSingleNode("//div[@class='z-reference-info-availability half']").RealInnerText();

                iOSVersion = ParseAvailability(availability);

                var members = doc.DocumentNode.SelectNodes("//section[@class='section task-group-section']//ul[@class='task-group-list']/li[@class='item symbol']");
                //var members = doc.DocumentNode.SelectNodes("//li[@class='item symbol']");
                //Members = members.Select(member => ScrapedMember.ScrapeMember(member));
                Members = new List<ScrapedMember>();
                if (members != null)
                {
                    foreach (var member in members)
                    {
                        var res = ScrapeMember(member);
                        ((List<ScrapedMember>) Members).Add(res);
                    }
                }

                var import =
                    doc.DocumentNode.SelectSingleNode(
                        "//div[@class='z-module-import half']/code[@class='code-voice Swift']");
                if (import != null)
                {
                    Namespace = import.RealInnerText().Split("import ")[1].Split(' ')[0].Trim();
                }

                Name = doc.DocumentNode.SelectSingleNode("//h1[@class='chapter-name']").RealInnerText();
            }
        }
    }
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback