Skip to content

Instantly share code, notes, and snippets.

@PontiacGTX
Last active May 13, 2019 16:26
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Http;
using Newtonsoft.Json;
namespace ConsoleApp
{
public class GistObject
{
public string url { get; set; }
public string forks_url { get; set; }
public string commits_url { get; set; }
public string id { get; set; }
public string node_id { get; set; }
public string git_pull_url { get; set; }
public string git_push_url { get; set; }
public string html_url { get; set; }
public FileObject _files { get; set; }
public bool _public { get; set; }
public DateTime created_at { get; set; }
public DateTime updated_at { get; set; }
public string description { get; set; }
public int comments { get; set; }
public object user { get; set; }
public string comments_url { get; set; }
public Owner owner { get; set; }
public object[] forks { get; set; }
public History[] history { get; set; }
public bool truncated { get; set; }
}
public class FileObject
{
public FileType _file { get; set; }
}
public class FileType
{
public string _filename { get; set; }
public string type { get; set; }
public string language { get; set; }
public string raw_url { get; set; }
public int size { get; set; }
public bool truncated { get; set; }
public string content { get; set; }
}
public class Owner
{
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 History
{
public User user { get; set; }
public string version { get; set; }
public DateTime committed_at { get; set; }
public Change_Status change_status { get; set; }
public string url { get; set; }
}
public class User
{
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 Change_Status
{
public int total { get; set; }
public int additions { get; set; }
public int deletions { get; set; }
}
class Program
{
static string gistAPIUrl = "http://api.github.com/gists/:gist_id";
static string url = "https://gist.github.com/PontiacGTX/d99835d35fb2f29e4382bd23656b860f";
private static int GetIndex(string str, char c, int n)
{
int stringPosition = -1;
for (int i = 0; i < n; i++)
{
stringPosition = str.IndexOf(c, stringPosition + 1);
if (stringPosition == -1) break;
}
return stringPosition;
}
private static void GetGistUrl(ref string localurl, ref string fileName, ref string extension)
{
fileName = "";
string gistAPI = gistAPIUrl;
int gistIndexBegin = GetIndex(url, '/', 4);
int gistIndexEnd = url.IndexOf('#', 1) > 0 ? url.IndexOf('#', 1) : url.Length;
string gistID = url.Substring(gistIndexBegin + 1, gistIndexEnd - (gistIndexBegin + 1));
gistAPI = gistAPIUrl.Replace(":gist_id", gistID);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(gistAPI);
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36";
request.Method = WebRequestMethods.Http.Get;
request.Timeout = 5000;
Stream response = request.GetResponse().GetResponseStream();
StreamReader readstream = new StreamReader(response);
string responsetr = readstream.ReadToEnd();
readstream.Dispose();
readstream.Close();
var gistResultObj = JsonConvert.DeserializeObject<IList<GistObject>>(responsetr).ToList();
for (int i = 0; i < gistResultObj.Count; i++)
{
foreach (FileObject obj in gistResultObj[i]._files)
{
if (!string.IsNullOrEmpty(obj._file.raw_url))
{
localurl = obj._file.raw_url;
}
if (!string.IsNullOrEmpty(obj._file._filename))
{
fileName = obj._file._filename;
}
}
}
if (fileName == "")
{
fileName = gistID;
}
extension = Path.GetExtension(localurl);
}
static void Main(string[] args)
{
string dlurl, filename, ext;
dlurl = ""; filename = ""; ext = "";
GetGistUrl(ref dlurl, ref filename, ref ext);
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment