Skip to content

Commit

Permalink
Implemented Counts queries. (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeMayo authored Sep 18, 2021
1 parent a907b16 commit 1b5585e
Show file tree
Hide file tree
Showing 11 changed files with 778 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using LinqToTwitter;
using System.Collections.Generic;
using System.Diagnostics;
using LinqToTwitter.Common;

namespace ConsoleDemo.CSharp
{
public class CountsDemos
{
internal static async Task RunAsync(TwitterContext twitterCtx)
{
char key;

do
{
ShowMenu();

key = Console.ReadKey(true).KeyChar;

switch (key)
{
case '0':
Console.WriteLine("\n\tGetting recent counts...\n");
await DoCountsRecentAsync(twitterCtx);
break;
case '1':
Console.WriteLine("\n\tGetting all counts...\n");
await DoCountsAllAsync(twitterCtx);
break;
case 'q':
case 'Q':
Console.WriteLine("\nReturning...\n");
break;
default:
Console.WriteLine(key + " is unknown");
break;
}

} while (char.ToUpper(key) != 'Q');
}

static void ShowMenu()
{
Console.WriteLine("\nCounts Demos - Please select:\n");

Console.WriteLine("\t 0. Recent Counts");
Console.WriteLine("\t 1. All Counts");
Console.WriteLine();
Console.Write("\t Q. Return to Main menu");
}

static async Task DoCountsRecentAsync(TwitterContext twitterCtx)
{
string searchTerm = "\"LINQ to Twitter\" OR Linq2Twitter OR LinqToTwitter OR JoeMayo";
//searchTerm = "Twitter";

Counts? countsResponse =
await
(from count in twitterCtx.Counts
where count.Type == CountType.Recent &&
count.Query == searchTerm &&
count.Granularity == Granularity.Day
select count)
.SingleOrDefaultAsync();

if (countsResponse?.CountRanges != null)
countsResponse.CountRanges.ForEach(range =>
Console.WriteLine(
$"\nStart: {range.Start}" +
$"\nEnd: {range.End}" +
$"\nTweet: {range.TweetCount}"));
else
Console.WriteLine("No entries found.");
}

static async Task DoCountsAllAsync(TwitterContext twitterCtx)
{
string searchTerm = "\"LINQ to Twitter\" OR Linq2Twitter OR LinqToTwitter OR JoeMayo";
//searchTerm = "Twitter";

Counts? countsResponse =
await
(from count in twitterCtx.Counts
where count.Type == CountType.All &&
count.Query == searchTerm &&
count.Granularity == Granularity.Day
select count)
.SingleOrDefaultAsync();

if (countsResponse?.CountRanges != null)
countsResponse.CountRanges.ForEach(range =>
Console.WriteLine(
$"\nStart: {range.Start}" +
$"\nEnd: {range.End}" +
$"\nTweet: {range.TweetCount}"));
else
Console.WriteLine("No entries found.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ static async Task DoDemosAsync()
Console.WriteLine("\n\tRunning Like Demos...\n");
await LikeDemos.RunAsync(twitterCtx);
break;
case 'm':
case 'M':
Console.WriteLine("\n\tRunning Counts Demos...\n");
await CountsDemos.RunAsync(twitterCtx);
break;
case 'q':
case 'Q':
Console.WriteLine("\nQuitting...\n");
Expand Down Expand Up @@ -188,6 +193,7 @@ static void ShowMenu()
Console.WriteLine("\t J. Tweet Demos");
Console.WriteLine("\t K. Compliance Demos");
Console.WriteLine("\t L. Like Demos");
Console.WriteLine("\t M. Counts Demos");
Console.WriteLine();
Console.Write("\t Q. End Program");
}
Loading

0 comments on commit 1b5585e

Please sign in to comment.