Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
Implementation becoming more concrete. Gotta reinforce it.
Browse files Browse the repository at this point in the history
  • Loading branch information
corstian committed Mar 4, 2019
1 parent dcdfc0e commit 6095ecd
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 17 deletions.
11 changes: 11 additions & 0 deletions 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<Investment> Investments { get; set; }
public DbSet<Opportunity> Opportunities { get; set; }
}
Expand Down
9 changes: 8 additions & 1 deletion MemeEconomy.Data/Opportunity.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

namespace MemeEconomy.Data
{
Expand All @@ -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<Investment> Investments { get; set; }


[NotMapped]
public string RedditUri => $"https://reddit.com/r/MemeEconomy/comments/{PostId}/";
}
}
1 change: 1 addition & 0 deletions MemeEconomy.Insights.sln
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions MemeEconomy.Insights/Graph/Subscription.cs
Expand Up @@ -19,9 +19,11 @@ public Subscription()

Field<InvestmentType>()
.Name("investments")
.Argument<StringGraphType>("opportunity", "Accepts an opportunity's cursor in order to subscribe to them updates.")
.Resolve(context => context.Source as Investment)
.Subscribe(context =>
{
context.GetArgument<string>("opportunity");
throw new NotImplementedException();
});
}
Expand Down
14 changes: 9 additions & 5 deletions 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<Investment>
{
public InvestmentType()
public InvestmentType(
IConfiguration config,
MemeEconomyContext store)
{
Field<StringGraphType>()
.Name("cursor")
Expand All @@ -17,11 +20,12 @@ public InvestmentType()

Field<OpportunityType>()
.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);
}
});
}
}
Expand Down
10 changes: 7 additions & 3 deletions 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<Opportunity>
{
public OpportunityType()
public OpportunityType(IConfiguration config)
{
Field<StringGraphType>()
.Name("cursor")
Expand All @@ -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);
}
});
}
}
Expand Down
4 changes: 4 additions & 0 deletions MemeEconomy.Insights/MemeEconomy.Insights.csproj
Expand Up @@ -23,4 +23,8 @@
<ProjectReference Include="..\MemeEconomy.Data\MemeEconomy.Data.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Graph\DataLoaders\" />
</ItemGroup>

</Project>
47 changes: 39 additions & 8 deletions 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;

Expand All @@ -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;
}
Expand All @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions 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&amp;utm_medium=referral&amp;utm_content=corstian/MemeEconomy.Insights&amp;utm_campaign=Badge_Grade)

MemeEconomy.Insights
====================

Expand Down

0 comments on commit 6095ecd

Please sign in to comment.