Skip to content

Commit

Permalink
it's starting to grow (like my cock)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluscream committed Aug 28, 2020
1 parent 176d4fb commit c951c1d
Show file tree
Hide file tree
Showing 5 changed files with 618 additions and 17 deletions.
105 changes: 105 additions & 0 deletions Classes/PCGWCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using Bluscream;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace PCGWMetaData.Classes
{
public class Cache
{
internal List<Game> games;
internal DirectoryInfo cacheDir;
internal PCGWMetaDataPlugin plugin;

public Cache(PCGWMetaDataPlugin plugin)
{
this.plugin = plugin;
cacheDir = new DirectoryInfo(plugin.GetPluginUserDataPath()).Combine("cache");
cacheDir.Create();
games = new List<Game>();
refresh();
}

public List<Game> refresh()
{
games.Clear();
foreach (var file in cacheDir.EnumerateFiles("*.json", SearchOption.TopDirectoryOnly))
{
games.Add(new Game(file.FileNameWithoutExtension(), this));
}
// games = .Select(f => f.FileNameWithoutExtension()).ToList();
return games;
}

private Game addGame(string name)
{
var game = new Game(name, this);
var json = plugin.webClient.DownloadString(string.Format(PCGWMetaDataPlugin.url_base, game.EncodedName()));
game.File().WriteAllText(json);
games.Add(game);
return game;
}

private Game _getGame(string name)
{
return games.FirstOrDefault(g => g.Name == name.ToLowerInvariant());
}

public Game getGame(string name)
{
var game = _getGame(name);
if (game is null) addGame(name);
return game;
}

public void Purge()
{
foreach (var file in cacheDir.EnumerateFiles())
file.Delete();
refresh();
}

public void PurgeOutdated()
{
foreach (var game in games)
{
if (game.isOutdated())
game.File().Delete();
}
refresh();
}
}

public class Game
{
public string Name;
private Cache _cache;

public Game(string Name, Cache _cache)
{
this.Name = Name.ToLowerInvariant(); this._cache = _cache;
}

public FileInfo File()
{
return _cache.cacheDir.CombineFile(string.Format("{0}.json", this.EncodedName()));
}

public ApiResult Data()
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<ApiResult>(File().ReadAllText());
}

public string EncodedName() => HttpUtility.UrlEncode(this.Name);
// public string DecodedName() => HttpUtility.HtmlDecode(this.Name);

public bool isOutdated()
{
return DateTime.Now - System.IO.File.GetLastWriteTime(File().FullName) > TimeSpan.FromDays(30); // option
}
}
}
42 changes: 25 additions & 17 deletions PCGWMetaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using System.Linq;
using System.Net;
using System.Web;
using Bluscream;
using PCGWMetaData.Classes;
using System.Windows.Controls;

namespace PCGWMetaData
{
Expand All @@ -16,30 +19,32 @@ public class PCGWMetaDataPlugin : MetadataPlugin
public override Guid Id { get; } = Guid.Parse("111001DB-DBD1-46C6-B5D0-B1BA559D10E4");
public override List<MetadataField> SupportedFields { get; } = new List<MetadataField> { MetadataField.Tags };
public IPlayniteAPI api;
internal Cache cache;
internal WebClient webClient = new WebClient();
internal const string url_base = "https://www.pcgamingwiki.com/w/api.php?action=browsebysubject&format=json&subject={0}";

public PCGWMetaDataPlugin(IPlayniteAPI playniteAPI) : base(playniteAPI)
{
api = playniteAPI;
this.api = playniteAPI;
this.cache = new Cache(this);
}

public override OnDemandMetadataProvider GetMetadataProvider(MetadataRequestOptions options)
{
return new PCGWMetadataProvider(options, api);
return new PCGWMetadataProvider(options, this);
}
}

public class PCGWMetadataProvider : OnDemandMetadataProvider
{
private WebClient webClient = new WebClient();
private const string url_base = "https://www.pcgamingwiki.com/w/api.php?action=browsebysubject&format=json&subject=";
private readonly MetadataRequestOptions options;
private List<MetadataField> availableFields;
public IPlayniteAPI api;
private PCGWMetaDataPlugin plugin;

public PCGWMetadataProvider(MetadataRequestOptions options, IPlayniteAPI api)
public PCGWMetadataProvider(MetadataRequestOptions options, PCGWMetaDataPlugin plugin)
{
this.options = options;
this.api = api;
this.plugin = plugin;
}

public override List<MetadataField> AvailableFields
Expand All @@ -64,7 +69,7 @@ public override List<string> GetTags()
{
// api.Dialogs.ShowMessage("Requested metadata for game " + options.GameData.Name);
var tags = new List<string>();
var l_ = api.Database.Games.FirstOrDefault(g => g.Id == options.GameData.Id);
var l_ = plugin.api.Database.Games.FirstOrDefault(g => g.Id == options.GameData.Id);
if (l_ != null)
{
var l__ = l_.Tags;
Expand All @@ -73,15 +78,18 @@ public override List<string> GetTags()
tags = l__.Select(t => t.Name).ToList();
}
}
var url = url_base + HttpUtility.HtmlEncode(options.GameData.Name);
var json = webClient.DownloadString(url);
var result = JsonConvert.DeserializeObject<Classes.ApiResult>(json);
// api.Dialogs.ShowMessage(JsonConvert.SerializeObject(result));
if (result is null || result.Query is null || result.Query.Data is null) return null;
var engine = result.Query.Data.Where(i => i.Property == "Uses_engine").FirstOrDefault()?.Dataitem.FirstOrDefault().Item;
if (engine != null)
tags.Add("engine:" + engine.Replace("#404#", ""));
// api.Dialogs.ShowMessage(JsonConvert.SerializeObject(tags));
var _result = plugin.cache.getGame(options.GameData.Name);
if (_result != null)
{
var result = _result.Data();
// api.Dialogs.ShowMessage(JsonConvert.SerializeObject(result));
if (result is null || result.Query is null || result.Query.Data is null) return null;
var engine = result.Query.Data.Where(i => i.Property == "Uses_engine").FirstOrDefault()?.Dataitem.FirstOrDefault().Item;
if (engine != null)
tags.Add("engine:" + engine.Replace("#404#", ""));
// api.Dialogs.ShowMessage(JsonConvert.SerializeObject(tags));
}
tags.ForEach(t => t.ToLowerInvariant().Trim()); // Todo: option
return tags;
}
}
Expand Down
3 changes: 3 additions & 0 deletions PCGWMetaData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Classes\ApiResult.cs" />
<Compile Include="Classes\PCGWCache.cs" />
<Compile Include="PCGWMetaData.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils\Extensions.cs" />
<Compile Include="Utils\Utils.cs" />
</ItemGroup>
<ItemGroup>
<None Include=".gitignore" />
Expand Down
Loading

0 comments on commit c951c1d

Please sign in to comment.