diff --git a/MemeEconomy.Data/MemeEconomyContext.cs b/MemeEconomy.Data/MemeEconomyContext.cs index 7a82f5b..dd2416e 100644 --- a/MemeEconomy.Data/MemeEconomyContext.cs +++ b/MemeEconomy.Data/MemeEconomyContext.cs @@ -1,9 +1,20 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; namespace MemeEconomy.Data { public class MemeEconomyContext : DbContext { + public MemeEconomyContext() + { + + } + + public MemeEconomyContext(IConfiguration config) + { + + } + public DbSet Investments { get; set; } public DbSet Opportunities { get; set; } } diff --git a/MemeEconomy.Data/Opportunity.cs b/MemeEconomy.Data/Opportunity.cs index aed0dbb..0653842 100644 --- a/MemeEconomy.Data/Opportunity.cs +++ b/MemeEconomy.Data/Opportunity.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; namespace MemeEconomy.Data { @@ -9,9 +10,15 @@ public class Opportunity public string Title { get; set; } public DateTime Timestamp { get; set; } - public string RedditUri { get; set; } + + public string PostId { get; set; } + public string MemeUri { get; set; } public List Investments { get; set; } + + + [NotMapped] + public string RedditUri => $"https://reddit.com/r/MemeEconomy/comments/{PostId}/"; } } diff --git a/MemeEconomy.Insights.sln b/MemeEconomy.Insights.sln index f487f81..3df2122 100644 --- a/MemeEconomy.Insights.sln +++ b/MemeEconomy.Insights.sln @@ -10,6 +10,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{47659FFE-5048-4E4E-B325-C28EEDFC4E7C}" ProjectSection(SolutionItems) = preProject .dockerignore = .dockerignore + .gitignore = .gitignore README.md = README.md EndProjectSection EndProject diff --git a/MemeEconomy.Insights/Graph/Subscription.cs b/MemeEconomy.Insights/Graph/Subscription.cs index 04c11ce..340c05c 100644 --- a/MemeEconomy.Insights/Graph/Subscription.cs +++ b/MemeEconomy.Insights/Graph/Subscription.cs @@ -19,9 +19,11 @@ public Subscription() Field() .Name("investments") + .Argument("opportunity", "Accepts an opportunity's cursor in order to subscribe to them updates.") .Resolve(context => context.Source as Investment) .Subscribe(context => { + context.GetArgument("opportunity"); throw new NotImplementedException(); }); } diff --git a/MemeEconomy.Insights/Graph/Types/InvestmentType.cs b/MemeEconomy.Insights/Graph/Types/InvestmentType.cs index 5f162a4..c9558d1 100644 --- a/MemeEconomy.Insights/Graph/Types/InvestmentType.cs +++ b/MemeEconomy.Insights/Graph/Types/InvestmentType.cs @@ -1,12 +1,15 @@ using GraphQL.Types; using MemeEconomy.Data; +using Microsoft.Extensions.Configuration; using System; namespace MemeEconomy.Insights.Graph.Types { public class InvestmentType : ObjectGraphType { - public InvestmentType() + public InvestmentType( + IConfiguration config, + MemeEconomyContext store) { Field() .Name("cursor") @@ -17,11 +20,12 @@ public InvestmentType() Field() .Name("opporunity") - .Resolve(context => + .ResolveAsync(async context => { - // ToDo: Return new opportunity instance - - throw new NotImplementedException(); + using (var store = new MemeEconomyContext(config)) + { + return await store.Opportunities.FindAsync(context.Source.OpportunityId); + } }); } } diff --git a/MemeEconomy.Insights/Graph/Types/OpportunityType.cs b/MemeEconomy.Insights/Graph/Types/OpportunityType.cs index a1b25ee..71315e4 100644 --- a/MemeEconomy.Insights/Graph/Types/OpportunityType.cs +++ b/MemeEconomy.Insights/Graph/Types/OpportunityType.cs @@ -1,12 +1,13 @@ using GraphQL.Types; using MemeEconomy.Data; -using System; +using Microsoft.Extensions.Configuration; +using System.Linq; namespace MemeEconomy.Insights.Graph.Types { public class OpportunityType : ObjectGraphType { - public OpportunityType() + public OpportunityType(IConfiguration config) { Field() .Name("cursor") @@ -20,7 +21,10 @@ public OpportunityType() .Name("investments") .Resolve(context => { - throw new NotImplementedException(); + using (var store = new MemeEconomyContext(config)) + { + return store.Investments.Where(q => q.OpportunityId == context.Source.Id); + } }); } } diff --git a/MemeEconomy.Insights/MemeEconomy.Insights.csproj b/MemeEconomy.Insights/MemeEconomy.Insights.csproj index 279432b..768e8fa 100644 --- a/MemeEconomy.Insights/MemeEconomy.Insights.csproj +++ b/MemeEconomy.Insights/MemeEconomy.Insights.csproj @@ -23,4 +23,8 @@ + + + + diff --git a/MemeEconomy.Insights/Services/MemeEconomyStalker.cs b/MemeEconomy.Insights/Services/MemeEconomyStalker.cs index 1a8d35f..4a91512 100644 --- a/MemeEconomy.Insights/Services/MemeEconomyStalker.cs +++ b/MemeEconomy.Insights/Services/MemeEconomyStalker.cs @@ -1,6 +1,10 @@ -using Microsoft.Extensions.Hosting; +using MemeEconomy.Data; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; using RedditSharp; using RedditSharp.Things; +using System; +using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; @@ -11,7 +15,11 @@ public class MemeEconomyStalker : IHostedService private readonly Reddit _reddit; private RedditUser _memeInvestorBot; - public MemeEconomyStalker(Reddit reddit) + private readonly Regex _checkInvestment = new Regex(@"\*([0-9,]+) MemeCoins invested @ ([0-9,]+) upvotes\*"); + + public MemeEconomyStalker( + IConfiguration config, + Reddit reddit) { _reddit = reddit; } @@ -24,13 +32,36 @@ public Task StartAsync(CancellationToken cancellationToken) { foreach (var comment in _memeInvestorBot.Comments.GetListingStream()) { - /* - * Because investments are acknowledged by the MemeInvestor_bot we should be fine - * by just parsing all comments written by this bot. If we catch the updates we should - * also be able to see the return on investment etc. - */ + try + { + var match = _checkInvestment.Match(comment.Body); - + if (match.Success) + { + var investment = new Investment + { + Amount = Convert.ToInt64(match.Groups[1].Value.Replace(",", "")), + Upvotes = Convert.ToInt32(match.Groups[2].Value.Replace(",", "")), + Timestamp = comment.Created.UtcDateTime + }; + } + else if (comment.Body.Contains("**INVESTMENTS GO HERE - ONLY DIRECT REPLIES TO ME WILL BE PROCESSED**")) + { + // We have a new post. Retrieve information. + var opportunity = new Opportunity + { + Title = comment.LinkTitle, + Timestamp = comment.Created.UtcDateTime, + PostId = comment.LinkId.Split('_')[1], + MemeUri = _reddit.GetPost(new Uri(comment.Shortlink)).Url.ToString() + }; + } + + // We're not handling anything else rn + } catch (Exception) + { + // ToDo: Add error handling + } } }, cancellationToken); diff --git a/README.md b/README.md index c9676ad..39d6539 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ ![](https://styles.redditmedia.com/t5_3gl3k/styles/bannerPositionedImage_3it9rpm9xab21.png) +[![Codacy Badge](https://api.codacy.com/project/badge/Grade/1d4f0422e86140949f0701f2ef2fb890)](https://www.codacy.com/app/corstian/MemeEconomy.Insights?utm_source=github.com&utm_medium=referral&utm_content=corstian/MemeEconomy.Insights&utm_campaign=Badge_Grade) + MemeEconomy.Insights ====================