forked from IoTSharp/IoTSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
323 additions
and
122 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace IoTSharp.Releases | ||
{ | ||
public class GithubRelease | ||
{ | ||
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 string id { get; set; } | ||
public string tag_name { get; set; } | ||
public string name { get; set; } | ||
public string body { get; set; } | ||
|
||
public ICollection<GithubReleaseAsset> assets { get; set; } | ||
|
||
} | ||
|
||
public class GithubReleaseAsset | ||
{ | ||
public string url { get; set; } | ||
public string browser_download_url { get; set; } | ||
public string 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 long size { get; set; } | ||
public int download_count { get; set; } | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<Description></Description> | ||
<PackageProjectUrl>https://github.com/maikebing/GithubReleasesDownloader</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/maikebing/GithubReleasesDownloader</RepositoryUrl> | ||
<Authors>mwhitis;IoTSharp;maikebing</Authors> | ||
<Company>mwhitis;IoTSharp;maikebing</Company> | ||
<PackageTags>Github;Releases;Downloader;IoTSharp</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace IoTSharp.Releases | ||
{ | ||
public class LinkHeaderParser | ||
{ | ||
/* | ||
<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=2>; rel="next", | ||
<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last" | ||
*/ | ||
|
||
public string GetNextPageFromHeader(string headerText) | ||
{ | ||
if (string.IsNullOrWhiteSpace(headerText)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
var links = headerText.Split(','); | ||
|
||
foreach (var link in links.Where(link => link.Contains("rel=\"next\""))) | ||
{ | ||
return link.Split(';')[0].Replace("<","").Replace(">","").Trim(); | ||
} | ||
|
||
return string.Empty; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Mime; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
|
||
namespace IoTSharp.Releases | ||
{ | ||
public class ReleaseDownloader | ||
{ | ||
private readonly string _baseUri; | ||
|
||
private readonly string _accessToken; | ||
|
||
private readonly string _userAgent; | ||
|
||
private readonly string _releaseUri; | ||
|
||
private string _user; | ||
private string _repo; | ||
private string _token; | ||
|
||
|
||
public ReleaseDownloader(string _url, string accessToken) | ||
{ | ||
var uri = new Uri(_url); | ||
_baseUri = $"{ GetBaseUri(uri)}/repos/{GetUserFromUri(uri)}/{GetRepoFromUri(uri)}"; | ||
_accessToken = accessToken; | ||
_userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3829.0 Safari/537.36 Edg/77.0.197.1"; ; | ||
_releaseUri = GetReleaseUri(); | ||
} | ||
|
||
private static string GetUserFromUri(Uri uri) | ||
{ | ||
return !uri.LocalPath.Contains("/") ? string.Empty : uri.Segments[1].TrimEnd('/'); | ||
} | ||
|
||
private static string GetRepoFromUri(Uri uri) | ||
{ | ||
return !uri.LocalPath.Contains("/") ? string.Empty : uri.Segments[2].TrimEnd('/'); | ||
} | ||
|
||
private static string GetBaseUri(Uri uri) | ||
{ | ||
return uri.Host.Equals("github.com", StringComparison.OrdinalIgnoreCase) ? "https://api.github.com" : $"{uri.Scheme}://{uri.Host}/api/v3"; | ||
} | ||
|
||
|
||
public ICollection<GithubRelease> GetDataForAllReleases() | ||
{ | ||
var requestingUri = GetAccessTokenUri(_releaseUri); | ||
return DownloadReleases(requestingUri); | ||
} | ||
|
||
public ICollection<GithubRelease> DownloadReleases(string requestingUri) | ||
{ | ||
|
||
Console.WriteLine("Requesting: {0}", requestingUri); | ||
var request = (HttpWebRequest)WebRequest.Create(new Uri(requestingUri)); | ||
request.UserAgent = _userAgent; | ||
|
||
var response = request.GetResponse(); | ||
Console.WriteLine(((HttpWebResponse)response).StatusDescription); | ||
// Get the stream containing content returned by the server. | ||
|
||
var responseFromServer = ReadResponseFromServer(response); | ||
|
||
var releases = JsonConvert.DeserializeObject<List<GithubRelease>>(responseFromServer); | ||
|
||
var parser = new LinkHeaderParser(); | ||
|
||
var linkHeader = response.Headers["Link"]; | ||
|
||
var nextUrl = parser.GetNextPageFromHeader(linkHeader); | ||
|
||
if (!string.IsNullOrEmpty(nextUrl)) | ||
{ | ||
releases.AddRange(DownloadReleases(nextUrl)); | ||
} | ||
|
||
// Clean up the streams and the response. | ||
response.Close(); | ||
return releases; | ||
} | ||
|
||
private string GetReleaseUri() | ||
{ | ||
var releaseUri = $"{_baseUri}/releases"; | ||
return releaseUri; | ||
} | ||
|
||
private static string ReadResponseFromServer(WebResponse response) | ||
{ | ||
using (var dataStream = response.GetResponseStream()) | ||
{ | ||
// Open the stream using a StreamReader for easy access. | ||
using (var reader = new StreamReader(dataStream)) | ||
{ | ||
// Read the content. | ||
return reader.ReadToEnd(); | ||
} | ||
} | ||
} | ||
|
||
private string GetAssetsUriForId(string id) | ||
{ | ||
var assetUri = $"{_releaseUri}/assets/{id}"; | ||
return assetUri; | ||
} | ||
|
||
private string GetAccessTokenUri(string uri) | ||
{ | ||
return _accessToken == string.Empty ? uri : uri += $"?access_token={_accessToken}"; | ||
} | ||
|
||
public bool DownloadAsset(string id, string path) | ||
{ | ||
WebResponse response = GetAssetResponse(id); | ||
GetBinaryResponseFromResponse(path,response); | ||
return true; | ||
} | ||
public bool DownloadAsset(string id, out byte[] assetdata) | ||
{ | ||
WebResponse response = GetAssetResponse(id); | ||
assetdata = GetBinaryResponseFromResponse(response); | ||
return true; | ||
} | ||
|
||
private WebResponse GetAssetResponse(string id) | ||
{ | ||
var assetUri = GetAccessTokenUri(GetAssetsUriForId(id)); | ||
|
||
var request = (HttpWebRequest)WebRequest.Create(new Uri(assetUri)); | ||
request.Accept = "application/octet-stream"; | ||
request.UserAgent = "mwhitis"; | ||
|
||
var response = request.GetResponse(); | ||
return response; | ||
} | ||
|
||
private static byte[] GetBinaryResponseFromResponse( WebResponse response) | ||
{ | ||
byte[] result = null; | ||
long received = 0; | ||
byte[] buffer = new byte[1024]; | ||
using (var ms = new MemoryStream() ) | ||
{ | ||
using (var input = response.GetResponseStream()) | ||
{ | ||
int size = input.Read(buffer, 0, buffer.Length); | ||
while (size > 0) | ||
{ | ||
ms.Write(buffer, 0, size); | ||
received += size; | ||
|
||
size = input.Read(buffer, 0, buffer.Length); | ||
} | ||
} | ||
|
||
result = ms.ToArray(); | ||
} | ||
return result; | ||
} | ||
private static void GetBinaryResponseFromResponse(string path, WebResponse response) | ||
{ | ||
System.IO.File.WriteAllBytes(path, GetBinaryResponseFromResponse(response)); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.