Skip to content
This repository was archived by the owner on Dec 2, 2022. It is now read-only.

Commit da61bff

Browse files
authored
Merge pull request #10 from PromoFaux/development
Release
2 parents a7c8afc + 42300e8 commit da61bff

17 files changed

+593
-457
lines changed

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ FROM microsoft/aspnetcore-build:1.0-2.0 AS builder
33
WORKDIR /source
44

55
COPY . .
6-
RUN dotnet restore MattermostRSS.sln
7-
RUN dotnet publish MattermostRSS.sln -c Release -o /publish
6+
RUN dotnet restore Matterfeed.NET.sln
7+
RUN dotnet publish Matterfeed.NET.sln -c Release -o /publish
88

99
# Stage 2
1010
FROM microsoft/dotnet:2.0-runtime
1111
WORKDIR /app
1212
COPY --from=builder /publish .
13-
ENTRYPOINT ["dotnet", "MattermostRSS.dll"]
13+
ENTRYPOINT ["dotnet", "Matterfeed.NET.dll"]

MattermostRSS.sln renamed to Matterfeed.NET.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
44
VisualStudioVersion = 15.0.26730.12
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MattermostRSS", "MattermostRSS\MattermostRSS.csproj", "{61703C09-F389-43AA-8492-9D49C10A3EEF}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Matterfeed.NET", "Matterfeed.NET\Matterfeed.NET.csproj", "{61703C09-F389-43AA-8492-9D49C10A3EEF}"
77
EndProject
88
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{4003ADB5-BDD9-4CAA-A4DF-E3AE11E8C0BF}"
99
EndProject
File renamed without changes.

MattermostRSS/Dockerfile renamed to Matterfeed.NET/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ FROM microsoft/dotnet:2.0-runtime
22
ARG source
33
WORKDIR /app
44
COPY ${source:-obj/Docker/publish} .
5-
ENTRYPOINT ["dotnet", "MattermostRSS.dll"]
5+
ENTRYPOINT ["dotnet", "Matterfeed.NET.dll"]

MattermostRSS/MattermostRSS.csproj renamed to Matterfeed.NET/Matterfeed.NET.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
<StartupObject></StartupObject>
7+
<ApplicationIcon />
8+
</PropertyGroup>
9+
10+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
11+
<PlatformTarget>AnyCPU</PlatformTarget>
12+
<WarningLevel>3</WarningLevel>
613
</PropertyGroup>
714

815
<ItemGroup>
916
<PackageReference Include="CodeHollow.FeedReader" Version="1.1.0.2" />
1017
<PackageReference Include="Matterhook.NET.MatterhookClient" Version="1.2.3.20" />
1118
<PackageReference Include="ReverseMarkdown" Version="1.3.0" />
19+
<PackageReference Include="TweetinviAPI" Version="2.0.0" />
1220
</ItemGroup>
1321

1422
<ItemGroup>
File renamed without changes.

Matterfeed.NET/Program.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Net;
5+
using System.Threading.Tasks;
6+
using Matterhook.NET.MatterhookClient;
7+
using JsonSerializer = Newtonsoft.Json.JsonSerializer;
8+
9+
namespace Matterfeed.NET
10+
{
11+
internal class Program
12+
{
13+
private const string ConfigPath = "/config/secrets.json";
14+
15+
public static Config Config = new Config();
16+
17+
private static void Main(string[] args)
18+
{
19+
Console.WriteLine($"{DateTime.Now} - Application Started.");
20+
try
21+
{
22+
var allTasks = new List<Task>();
23+
LoadConfig();
24+
if (Config.RssFeeds != null)
25+
{
26+
var rssTask = RssFeedReader.PeriodicRssAsync(TimeSpan.FromMilliseconds(Config.BotCheckIntervalMs), Config.RssFeeds);
27+
allTasks.Add(rssTask);
28+
}
29+
30+
if (Config.RedditJsonFeeds != null)
31+
{
32+
var redditTask = RedditJsonFeedReader.PeriodicRedditAsync(TimeSpan.FromMilliseconds(Config.BotCheckIntervalMs), Config.RedditJsonFeeds);
33+
allTasks.Add(redditTask);
34+
}
35+
36+
if (Config.TwitterFeed != null)
37+
{
38+
var twitterTask = TwitterFeedReader.PeriodicTwitterAsync(TimeSpan.FromMilliseconds(Config.TwitterFeed.Interval),Config.TwitterFeed);
39+
allTasks.Add(twitterTask);
40+
}
41+
42+
Task.WaitAll(allTasks.ToArray());
43+
}
44+
catch (Exception e)
45+
{
46+
Console.WriteLine(e);
47+
throw;
48+
}
49+
}
50+
51+
private static void LoadConfig()
52+
{
53+
if (File.Exists(ConfigPath))
54+
using (var file = File.OpenText(ConfigPath))
55+
{
56+
var serializer = new JsonSerializer();
57+
Config = (Config)serializer.Deserialize(file, typeof(Config));
58+
}
59+
else
60+
{
61+
Console.WriteLine("No secrets.json found! I Have no idea what to do...");
62+
Environment.Exit(1);
63+
}
64+
}
65+
66+
67+
public static async Task PostToMattermost(MattermostMessage message)
68+
{
69+
if (message.Channel == null) message.Channel = Config.BotChannelDefault;
70+
if (message.Username == null) message.Username = Config.BotNameDefault;
71+
if (message.IconUrl == null) message.IconUrl = new Uri(Config.BotImageDefault);
72+
var mc = new MatterhookClient(Config.MattermostWebhookUrl);
73+
74+
var response = await mc.PostAsync(message);
75+
76+
if (response == null || response.StatusCode != HttpStatusCode.OK)
77+
{
78+
throw new Exception(response != null
79+
? $"Unable to post to Mattermost.{response.StatusCode}"
80+
: $"Unable to post to Mattermost.");
81+
}
82+
83+
}
84+
85+
internal static void SaveConfigSection(List<RedditJsonFeed> redditFeeds)
86+
{
87+
Config.RedditJsonFeeds = redditFeeds;
88+
Config.Save(ConfigPath);
89+
}
90+
91+
internal static void SaveConfigSection(TwitterFeed twitterFeed)
92+
{
93+
Config.TwitterFeed = twitterFeed;
94+
Config.Save(ConfigPath);
95+
}
96+
97+
internal static void SaveConfigSection(List<RssFeed> rssFeeds)
98+
{
99+
Config.RssFeeds = rssFeeds;
100+
Config.Save(ConfigPath);
101+
}
102+
}
103+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"profiles": {
3+
"Matterfeed.NET": {
4+
"commandName": "Project"
5+
}
6+
}
7+
}

MattermostRSS/Reddit.cs renamed to Matterfeed.NET/Reddit.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using System;
2-
using System.Collections.Generic;
32
using Newtonsoft.Json;
43
using Newtonsoft.Json.Converters;
54

6-
namespace MattermostRSS
5+
namespace Matterfeed.NET
76
{
87
public class RedditJson
98
{
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Text.RegularExpressions;
6+
using System.Threading.Tasks;
7+
using Matterhook.NET.MatterhookClient;
8+
using Newtonsoft.Json;
9+
10+
namespace Matterfeed.NET
11+
{
12+
internal class RedditJsonFeedReader
13+
{
14+
public static async Task PeriodicRedditAsync(TimeSpan interval, List<RedditJsonFeed> redditFeeds)
15+
{
16+
while (true)
17+
{
18+
foreach (var feed in redditFeeds)
19+
{
20+
using (var wc = new WebClient())
21+
{
22+
var stuffToLog = $"\n{DateTime.Now}\nFetching Reddit URL: {feed.Url}";
23+
24+
string json;
25+
try
26+
{
27+
json = wc.DownloadString(feed.Url);
28+
}
29+
catch (Exception e)
30+
{
31+
stuffToLog += $"\nUnable to get feed, exception: {e.Message}";
32+
Console.WriteLine(stuffToLog);
33+
return;
34+
}
35+
36+
//only get items we have not already processed
37+
var items = JsonConvert.DeserializeObject<RedditJson>(json).RedditJsonData.RedditJsonChildren
38+
.Where(y => y.Data.Created > feed.LastProcessedItem).OrderBy(x => x.Data.Created);
39+
40+
var itemCount = items.Count();
41+
var procCount = 0;
42+
43+
foreach (var item in items)
44+
{
45+
var message = new MattermostMessage
46+
{
47+
Channel = feed.BotChannelOverride == ""? null : feed.BotChannelOverride,
48+
Username = feed.BotNameOverride == "" ? null : feed.BotNameOverride,
49+
IconUrl = feed.BotImageOverride == ""? null:new Uri(feed.BotImageOverride)
50+
};
51+
52+
switch (item.Kind)
53+
{
54+
case "t3":
55+
string content;
56+
switch (item.Data.PostHint)
57+
{
58+
case "link":
59+
content = $"Linked Content: {item.Data.Url}";
60+
break;
61+
default:
62+
content = item.Data.Selftext;
63+
break;
64+
}
65+
66+
message.Attachments = new List<MattermostAttachment>
67+
{
68+
new MattermostAttachment
69+
{
70+
AuthorName = $"/u/{item.Data.Author}",
71+
AuthorLink = new Uri($"https://reddit.com/u/{item.Data.Author}"),
72+
Title = item.Data.Title,
73+
TitleLink = new Uri($"https://reddit.com{item.Data.Permalink}"),
74+
Text = content,
75+
Pretext = feed.FeedPretext
76+
}
77+
};
78+
message.Text =
79+
$"#{Regex.Replace(item.Data.Title.Replace(" ", "-"), "[^0-9a-zA-Z-]+", "")}";
80+
81+
break;
82+
case "t4":
83+
84+
message.Attachments = new List<MattermostAttachment>
85+
{
86+
new MattermostAttachment
87+
{
88+
AuthorName = $"/u/{item.Data.Author}",
89+
AuthorLink = new Uri($"https://reddit.com/u/{item.Data.Author}"),
90+
Title = item.Data.Subject,
91+
TitleLink = new Uri($"https://reddit.com{item.Data.Permalink}"),
92+
Text =
93+
item.Data.Body.Replace("](/r/",
94+
"](https://reddit.com/r/"), //expand /r/ markdown links
95+
Pretext = feed.FeedPretext
96+
}
97+
};
98+
break;
99+
}
100+
101+
102+
try
103+
{
104+
//Task.WaitAll(Program.PostToMattermost(message));
105+
await Program.PostToMattermost(message);
106+
feed.LastProcessedItem = item.Data.Created;
107+
procCount++;
108+
}
109+
catch (Exception e)
110+
{
111+
stuffToLog += $"\nException: {e.Message}";
112+
}
113+
114+
}
115+
116+
stuffToLog += $"\nProcessed {procCount}/{itemCount} items.";
117+
Console.WriteLine(stuffToLog);
118+
}
119+
}
120+
Program.SaveConfigSection(redditFeeds);
121+
await Task.Delay(interval);
122+
}
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)