Skip to content

Instantly share code, notes, and snippets.

@dracan
Created February 14, 2021 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dracan/9af9e13a154bbb145b0814aeb280bf0d to your computer and use it in GitHub Desktop.
Save dracan/9af9e13a154bbb145b0814aeb280bf0d to your computer and use it in GitHub Desktop.
Unhandled Exception Podcast: LINQPad giveaway

See here for the YouTube video showing this, and doing the prize draw. And see here for the podcast episode.

record Creds(
string ConsumerKey = "PLACEHOLDER",
string ConsumerSecret = "PLACEHOLDER",
string OAuthToken = "PLACEHOLDER",
string OAuthTokenSecret = "PLACEHOLDER");
<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;
@abdelmalek-create
Copy link

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment