Created
November 25, 2018 03:08
-
-
Save SteveH3032/0444337171443d0fd6ea0c3cc979f306 to your computer and use it in GitHub Desktop.
Download NuGet Catalog Using C# Console App
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Text.RegularExpressions; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json.Linq; | |
namespace NuGetDownloader | |
{ | |
class Program | |
{ | |
public static List<jsonSource> jsonSources = new List<jsonSource>(); | |
public static Dictionary<string, Version> packageDictionary = new Dictionary<string, Version>(); | |
static void Main(string[] args) | |
{ | |
using (WebClient client = new WebClient()) | |
{ | |
// Create a list of source sites | |
List<JToken> response = JObject | |
.Parse(client.DownloadString("https://api.nuget.org/v3/catalog0/index.json") | |
.Replace("@", "")) | |
.Children().ToList()[8].Children().Children().ToList(); | |
// Add each JSON object from the 'response' to the list of JSON sources | |
response.ForEach(item => jsonSources.Add(item.ToObject<jsonSource>())); | |
// Parse the 5000 lists of 500 items each (15 at a time) and build dictionary of highest version packages | |
Parallel.ForEach(jsonSources, new ParallelOptions { MaxDegreeOfParallelism = 15 }, buildDictionary); | |
// Download Highest Versions from the packageDictionary | |
Parallel.ForEach(packageDictionary, new ParallelOptions { MaxDegreeOfParallelism = 15 }, downloadDictionaryItems); | |
Console.WriteLine("Completed download at: " + System.DateTime.Now); | |
Console.ReadLine(); | |
} | |
} | |
private static void buildDictionary(jsonSource obj) | |
{ | |
// Get the page number (page1525.json) is 1525 | |
int myPage = Int32.Parse(Regex.Match(obj.id.Split('/').Last(), @"\d+").Value); | |
List<JToken> packageResponse = new List<JToken>(); | |
// Condition below is due to format change at page 1525 | |
if (myPage < 1525) | |
{ | |
WebClient client = new WebClient(); | |
packageResponse = JObject.Parse(client.DownloadString(obj.id) | |
.Replace("@", "").Replace("nuget:", "nuget")) | |
.Children().ToList()[5].Children().Children().ToList(); | |
} | |
else | |
{ | |
WebClient client = new WebClient(); | |
packageResponse = JObject.Parse(client.DownloadString(obj.id)) | |
.Children().ToList()[6].Children().Children().ToList(); | |
} | |
packageResponse.ForEach(item => | |
{ | |
try | |
{ | |
packageSource myItem = item.ToObject<packageSource>(); | |
if (packageDictionary.ContainsKey(myItem.nugetId)) | |
{ | |
Version vNew = myItem.nugetVersion; | |
Version vExists = packageDictionary[myItem.nugetId]; | |
packageDictionary[myItem.nugetId] = | |
(vNew.CompareTo(vExists) > 0) ? vNew : vExists; | |
} | |
else | |
{ | |
packageDictionary.Add(item.ToObject<packageSource>().nugetId, | |
item.ToObject<packageSource>().nugetVersion); | |
} | |
} | |
catch (Exception ex) { } | |
}); | |
Console.WriteLine(packageDictionary.Count + " - " + obj.id); | |
} | |
private static void downloadDictionaryItems(KeyValuePair<string, Version> entry) | |
{ | |
try | |
{ | |
WebClient client = new WebClient(); | |
string downloadFile = "https://www.nuget.org/api/v2/package/" + entry.Key + "/" + entry.Value; | |
string outFile = "C:" + "\\" + "Nuget" + "\\" + entry.Key + "." + entry.Value + "." + "nupkg"; | |
client.DownloadFile(downloadFile, outFile); | |
} | |
catch(Exception ex) { //Swallow it } | |
} | |
public class jsonSource | |
{ | |
public string id { get; set; } | |
} | |
public class packageSource | |
{ | |
public string nugetId { get; set; } | |
public Version nugetVersion { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment