-
Notifications
You must be signed in to change notification settings - Fork 1
/
Github.cs
183 lines (166 loc) · 6.42 KB
/
Github.cs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
namespace ElectricShimmer
{
class Github
{
private string _repo;
public Github(string repo)
{
_repo = repo;
}
public List<string> ReleaseNames
{
get
{
if (_releases == null)
_releases = GetReleasesList();
return _releases.Select(x => x.name).ToList();
}
}
public GithubObjects.Releases GetReleasebyVersion(string version = "latest")
{
try
{
if (_releases == null)
_releases = GetReleasesList();
return version == "latest" ? _releases[0] : _releases.Where(x => x.tag_name == version).ToList()[0];
}
catch (Exception exc)
{
Log.Write(exc.Message, LogLevel.EXCEPTION);
return null;
}
}
public GithubObjects.Releases GetReleasebyAssetName(string name)
{
try
{
if (_releases == null)
_releases = GetReleasesList();
foreach (var release in _releases)
{
if (release.assets.Where(x => Regex.Match(x.name, name).Success).Count() > 0)
return release;
}
return null;
}
catch (Exception exc)
{
Log.Write(exc.Message, LogLevel.EXCEPTION);
return null;
}
}
private List<GithubObjects.Releases> _releases;
public string Get(string uri)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.UserAgent = "ElectricShimmer";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
catch (Exception exc)
{
Log.Write(exc.Message, LogLevel.EXCEPTION);
return null;
}
}
private List<GithubObjects.Releases> GetReleasesList()
{
return JsonConvert.DeserializeObject<List<GithubObjects.Releases>>(Get(_repo));
}
}
class GithubObjects
{
public class Releases
{
public string url { get; set; }
public string html_url { get; set; }
public string assets_url { get; set; }
public string upload_url { get; set; }
public string tarball_url { get; set; }
public string zipball_url { get; set; }
public int id { get; set; }
public string node_id { get; set; }
public string tag_name { get; set; }
public string target_commitish { get; set; }
public string name { get; set; }
public string body { get; set; }
public bool draft { get; set; }
public bool prerelease { get; set; }
public DateTime created_at { get; set; }
public DateTime published_at { get; set; }
public Author author { get; set; }
public List<Assets> assets { get; set; }
public class Author
{
public string login { get; set; }
public int id { get; set; }
public string node_id { get; set; }
public string avatar_url { get; set; }
public string gravatar_id { get; set; }
public string url { get; set; }
public string html_url { get; set; }
public string followers_url { get; set; }
public string following_url { get; set; }
public string gists_url { get; set; }
public string starred_url { get; set; }
public string subscriptions_url { get; set; }
public string organizations_url { get; set; }
public string repos_url { get; set; }
public string events_url { get; set; }
public string received_events_url { get; set; }
public string type { get; set; }
public bool site_admin { get; set; }
}
public class Assets
{
public string url { get; set; }
public string browser_download_url { get; set; }
public int id { get; set; }
public string node_id { get; set; }
public string name { get; set; }
public string label { get; set; }
public string state { get; set; }
public string content_type { get; set; }
public int size { get; set; }
public int download_count { get; set; }
public DateTime created_at { get; set; }
public DateTime updated_at { get; set; }
public Uploader uploader { get; set; }
public class Uploader
{
public string login { get; set; }
public int id { get; set; }
public string node_id { get; set; }
public string avatar_url { get; set; }
public string gravatar_id { get; set; }
public string url { get; set; }
public string html_url { get; set; }
public string followers_url { get; set; }
public string following_url { get; set; }
public string gists_url { get; set; }
public string starred_url { get; set; }
public string subscriptions_url { get; set; }
public string organizations_url { get; set; }
public string repos_url { get; set; }
public string events_url { get; set; }
public string received_events_url { get; set; }
public string type { get; set; }
public bool site_admin { get; set; }
}
}
}
}
}