-
Notifications
You must be signed in to change notification settings - Fork 2
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
10 changed files
with
171 additions
and
6 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using LinkUtilities.Helper; | ||
using LinkUtilities.Models; | ||
using LinkUtilities.Models.Epic; | ||
using Playnite.SDK; | ||
using Playnite.SDK.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Text; | ||
|
||
namespace LinkUtilities.Linker | ||
{ | ||
class LinkEpic : Link | ||
{ | ||
public override string LinkName { get; } = "Epic"; | ||
public override string BaseUrl { get; } = "https://store.epicgames.com/en-US/p/"; | ||
public override string SearchUrl { get; } = "https://www.epicgames.com/graphql?query={Catalog{searchStore(keywords:%22{SearchString}%22,category:%22games/edition%22,effectiveDate:%22[1900-01-01,{DateUntil}]%22,count:100){elements{title%20urlSlug%20seller{name}}}}}"; | ||
|
||
private readonly string CheckUrl = "https://store-content-ipv4.ak.epicgames.com/api/en-US/content/products/"; | ||
|
||
public override string GetGamePath(Game game) | ||
{ | ||
// Epic Links need the game name in lowercase without special characters and underscores instead of white spaces. | ||
return game.Name.RemoveDiacritics() | ||
.RemoveSpecialChars() | ||
.CollapseWhitespaces() | ||
.Replace(" ", "-") | ||
.ToLower(); | ||
} | ||
|
||
public override bool AddLink(Game game) | ||
{ | ||
// Unfortunately Epic returns the status code forbidden, when trying to check the url, because they want cookies and | ||
// javascipt active. Fortunately we can use the game slug in the store api. If it doesn't return an error, there should also | ||
// be a link with that slug. | ||
string gameSlug = GetGamePath(game); | ||
string url = $"{CheckUrl}{gameSlug}"; | ||
|
||
WebClient client = new WebClient() { Encoding = Encoding.UTF8 }; | ||
client.Headers.Add("Accept", "application/json"); | ||
|
||
try | ||
{ | ||
string _ = client.DownloadString(url); | ||
LinkUrl = $"{BaseUrl}{gameSlug}"; | ||
return LinkHelper.AddLink(game, LinkName, LinkUrl, Settings); | ||
} | ||
catch | ||
{ | ||
LinkUrl = string.Empty; | ||
return false; | ||
} | ||
} | ||
|
||
public override List<GenericItemOption> SearchLink(string searchTerm) | ||
{ | ||
SearchResults.Clear(); | ||
|
||
try | ||
{ | ||
WebClient client = new WebClient() { Encoding = Encoding.UTF8 }; | ||
|
||
client.Headers.Add("Accept", "application/json"); | ||
|
||
// We use replace instead of format, because the url already contains several braces. | ||
string url = SearchUrl | ||
.Replace("{SearchString}", searchTerm.UrlEncode()) | ||
.Replace("{DateUntil}", DateTime.Now.AddDays(5).ToString("yyyy-MM-dd")); | ||
|
||
string jsonResult = client.DownloadString(url); | ||
|
||
EpicSearchResult epicSearchResult = Newtonsoft.Json.JsonConvert.DeserializeObject<EpicSearchResult>(jsonResult); | ||
|
||
int counter = 0; | ||
|
||
foreach (Element element in epicSearchResult.Data.Catalog.SearchStore.Elements) | ||
{ | ||
counter++; | ||
|
||
if (!string.IsNullOrEmpty(element.UrlSlug)) | ||
{ | ||
SearchResults.Add(new SearchResult | ||
{ | ||
Name = $"{counter}. {element.Title}", | ||
Url = $"{BaseUrl}{element.UrlSlug}", | ||
Description = element.Seller.Name | ||
} | ||
); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Error(ex, $"Error loading data from {LinkName}"); | ||
} | ||
|
||
return base.SearchLink(searchTerm); | ||
} | ||
|
||
public LinkEpic(LinkUtilitiesSettings settings) : base(settings) | ||
{ | ||
} | ||
} | ||
} |
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
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
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,57 @@ | ||
using Newtonsoft.Json; | ||
using System.Collections.Generic; | ||
|
||
// Contains all the classes needed to deserialize the JSON fetched from the Epic graphql api. | ||
namespace LinkUtilities.Models.Epic | ||
{ | ||
public class Catalog | ||
{ | ||
[JsonProperty("searchStore")] | ||
public SearchStore SearchStore; | ||
} | ||
|
||
public class Data | ||
{ | ||
[JsonProperty("Catalog")] | ||
public Catalog Catalog; | ||
} | ||
|
||
public class Element | ||
{ | ||
[JsonProperty("title")] | ||
public string Title; | ||
|
||
[JsonProperty("urlSlug")] | ||
public string UrlSlug; | ||
|
||
[JsonProperty("seller")] | ||
public Seller Seller; | ||
} | ||
|
||
public class Extensions | ||
{ | ||
} | ||
|
||
public class EpicSearchResult | ||
{ | ||
[JsonProperty("data")] | ||
public Data Data; | ||
|
||
[JsonProperty("extensions")] | ||
public Extensions Extensions; | ||
} | ||
|
||
public class SearchStore | ||
{ | ||
[JsonProperty("elements")] | ||
public List<Element> Elements; | ||
} | ||
|
||
public class Seller | ||
{ | ||
[JsonProperty("name")] | ||
public string Name; | ||
} | ||
|
||
|
||
} |