-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
75 lines (63 loc) · 2.41 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Xml;
using Microsoft.Extensions.Configuration;
using TweetSharp;
namespace BlogPromoter
{
class Program
{
private static readonly IConfiguration Configuration =
new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();
private static readonly string[] Exclamations = new[]
{
"Check it out!",
"I wrote",
"ICYMI"
};
static void Main(string[] args)
{
var url = Configuration["blog_feed_url"];
var twitter = Configuration["twitter_account"];
using var reader = XmlReader.Create(url);
var feed = SyndicationFeed.Load(reader);
var post = feed
.Items
// don't promote anything labeled general
.Where(x => !x.Categories.Any(c => c.Name.Contains("general", StringComparison.OrdinalIgnoreCase)))
// randomly order my posts
.OrderBy(x => Guid.NewGuid())
.FirstOrDefault();
var exclamation = Exclamations
.OrderBy(x => Guid.NewGuid())
.First();
var hashTags = string.Join(
" ",
post
.Categories
.Select(x => x.Name)
.Where(x => !string.IsNullOrWhiteSpace(x))
.Select(x => $"#{x.Replace(",", string.Empty)}")
)
.Trim();
var status =
$"{exclamation} \"{post.Title.Text}\" ({post.PublishDate.Date.ToShortDateString()}) by {twitter} {hashTags} RTs appreciated. {post.Links[0].Uri} ({DateTime.Now.ToShortDateString()})";
var service = new TwitterService(
Configuration["twitter_consumer_key"],
Configuration["twitter_consumer_secret"]
);
service.AuthenticateWith(
Configuration["twitter_access_token"],
Configuration["twitter_access_token_secret"]
);
var result = service.SendTweet(new SendTweetOptions
{
Status = status
});
Console.WriteLine($"Successfully tweeted {result.IdStr}!\r\n{result.Text}");
}
}
}