This repository has been archived by the owner on Apr 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TweetsImporter.cs
68 lines (60 loc) · 2.48 KB
/
TweetsImporter.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Newtonsoft.Json;
using PoliticalTweetsImporter.Models;
using TweetSharp;
namespace PoliticalTweetsImporter
{
public static class TweetsImporter
{
private const string
ConsumerKey = "gNc5En2ZKVHqo86hMWr1iRyxv",
ConsumerSecret = "OkPMufDcJWNBd01Y2SCGg2jhc7uJBWoYEIFtnNgd9bZ7swqxsC",
AccessToken = "3543553817-oe3YfgSknaQ4FQ77Mutpmyzy3Git9ReTBsz6m3H",
AccessTokenSecret = "E23J1deJIHGqlpu542TKLHjhPP3j41UTwmCzdM4CvhT8Q";
public static IReadOnlyCollection<PoliticalTweet> LoadTweets( IEnumerable<string> handles )
{
var service = new TwitterService( ConsumerKey, ConsumerSecret );
service.AuthenticateWith( AccessToken, AccessTokenSecret );
var results = new List<PoliticalTweet>();
foreach ( var handle in handles.Distinct() )
{
try
{
var response = service.ListTweetsOnUserTimeline( new ListTweetsOnUserTimelineOptions
{
ScreenName = handle,
Count = 200,
IncludeRts = false,
ExcludeReplies = true
} );
results.AddRange( response.Select( t => new PoliticalTweet( handle, t.Text, t.CreatedDate, t.IdStr ) ) );
Debug.WriteLine( $"{service.Response.RateLimitStatus.RemainingHits} remaining hits." );
if ( service.Response.RateLimitStatus.RemainingHits <= 0 )
{
var wait = service.Response.RateLimitStatus.ResetTime.ToUniversalTime() - DateTime.UtcNow;
Debug.WriteLine( $"Rate limit reached. Sleeping for {wait}." );
Thread.Sleep( wait );
}
}
catch
{
Debug.WriteLine( $"Skipping {handle}" );
}
}
return results;
}
private sealed class TwitterTweet
{
[JsonProperty( PropertyName = "id_str" )]
public string Id { get; set; }
[JsonProperty( PropertyName = "text" )]
public string Text { get; set; }
[JsonProperty( PropertyName = "created_at" )]
public string Created { get; set; }
}
}
}