Last active
January 26, 2020 14:06
-
-
Save alanedwardes/9a32a436623a148d320cdac710bb9233 to your computer and use it in GitHub Desktop.
Parse the UE4 Vault API to catalogue it in a spreadsheet (exports the JSON -> CSV)
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
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
namespace UnrealVaultParser | |
{ | |
class Program | |
{ | |
private class VaultItem | |
{ | |
public string ItemId { get; set; } | |
public string Title { get; set; } | |
public string Description { get; set; } | |
public string SellerName { get; set; } | |
public IList<string> Owners { get; set; } = new List<string>(); | |
} | |
static void Main(string[] args) | |
{ | |
var items = new Dictionary<string, VaultItem>(); | |
// JSON files in here, named "owner_page1.json" "owner_page2.json"... etc | |
foreach (var file in Directory.EnumerateFiles(@"C:\Users\alan\Desktop\New folder")) | |
{ | |
var owner = Path.GetFileNameWithoutExtension(file).Split("_")[0]; | |
var obj = JObject.Parse(File.ReadAllText(file)); | |
foreach (var item in obj["data"]["elements"]) | |
{ | |
var itemId = item.Value<string>("catalogItemId"); | |
var itemTitle = item.Value<string>("title"); | |
var itemDescription = item.Value<string>("description"); | |
var itemSellerName = item["seller"].Value<string>("name"); | |
if (items.ContainsKey(itemId)) | |
{ | |
items[itemId].Owners.Add(owner); | |
} | |
else | |
{ | |
items.Add(itemId, new VaultItem | |
{ | |
ItemId = itemId, | |
Title = itemTitle, | |
Description = itemDescription, | |
SellerName = itemSellerName, | |
Owners = new List<string> { owner } | |
}); | |
} | |
} | |
} | |
using (var output = File.OpenWrite(@"C:\Users\alan\Desktop\New folder\output.csv")) | |
using (var sw = new StreamWriter(output)) | |
{ | |
sw.WriteLine("Title,Description,Seller,Link,Owners"); | |
foreach (var item in items) | |
{ | |
sw.WriteLine($"\"{Escape(item.Value.Title)}\"," + | |
$"\"{Escape(item.Value.Description)}\"," + | |
$"\"{Escape(item.Value.SellerName)}\"," + | |
$"https://www.unrealengine.com/marketplace/en-US/item/{item.Value.ItemId}," + | |
$"\"{string.Join(",", item.Value.Owners.OrderBy(x => x))}\""); | |
} | |
} | |
} | |
private static string Escape(string str) => str.Replace("\"", "\"\""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment