See here for the YouTube video showing this, and doing the prize draw. And see here for the podcast episode.
Created
February 14, 2021 08:25
-
-
Save dracan/9af9e13a154bbb145b0814aeb280bf0d to your computer and use it in GitHub Desktop.
Unhandled Exception Podcast: LINQPad giveaway
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
record Creds( | |
string ConsumerKey = "PLACEHOLDER", | |
string ConsumerSecret = "PLACEHOLDER", | |
string OAuthToken = "PLACEHOLDER", | |
string OAuthTokenSecret = "PLACEHOLDER"); |
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
<Query Kind="Program"> | |
<NuGetReference>linqtotwitter</NuGetReference> | |
<Namespace>LinqToTwitter</Namespace> | |
<Namespace>LinqToTwitter.OAuth</Namespace> | |
<Namespace>System.Threading.Tasks</Namespace> | |
</Query> | |
#load ".\Creds" | |
async Task Main() | |
{ | |
var context = CreateContext(new Creds()); | |
var results = (await SearchTwitter(context, "\"UnhandledException\" OR \"Unhandled Exception podcast\"")).Dump("Search results"); | |
var winningIndex = new Random().Next(results.Length); | |
Util.RawHtml(await GetEmbeddedTweet(context, results[winningIndex].StatusId)).Dump("Winner!"); | |
} | |
record Tweet(string Text, string UserName, DateTime Date, int Retweets, ulong StatusId, List<string> HashTags, List<string> Mentions); | |
TwitterContext CreateContext(Creds creds) => | |
new TwitterContext(new SingleUserAuthorizer | |
{ | |
CredentialStore = new InMemoryCredentialStore() | |
{ | |
ConsumerKey = creds.ConsumerKey, | |
ConsumerSecret = creds.ConsumerSecret, | |
OAuthToken = creds.OAuthToken, | |
OAuthTokenSecret = creds.OAuthTokenSecret, | |
} | |
}); | |
async Task<Tweet[]> SearchTwitter(TwitterContext context, string query) | |
{ | |
var searchResponse = await context.Search | |
.Where(x => x.Type == SearchType.Search && x.Query == query) | |
.SingleOrDefaultAsync(); | |
return searchResponse.Statuses | |
.Where(x => !x.Retweeted) | |
.Select(x => new Tweet( | |
x.Text, | |
x.User.Name, | |
x.CreatedAt, | |
x.RetweetCount, | |
x.StatusID, | |
x.Entities.HashTagEntities.Select(h => h.Text).ToList(), | |
x.Entities.UserMentionEntities.Select(u => u.Name).ToList()) | |
).ToArray(); | |
} | |
async Task<string> GetEmbeddedTweet(TwitterContext context, ulong tweetId) => | |
(await context.Status | |
.Where(x => x.Type == StatusType.Oembed && x.ID == tweetId) | |
.Select(x => x.EmbeddedStatus) | |
.SingleOrDefaultAsync())?.Html; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you