/
TweetStats.cs
194 lines (166 loc) · 5.48 KB
/
TweetStats.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using CsQuery;
// Build and run with:
// csc TweetStats.cs /reference:CsQuery.dll && start TweetStats
public class TweetStatsRunner
{
public static void Main()
{
List<Tweet> tweets = new TwitterService().GetTweets(/* HTML DATA SOURCE */);
List<Tweet> shortTweetsArchive = tweets.Where(t => t.Content.Length < 100).ToList();
List<Tweet> longTweetsArchive = tweets.Where(t => t.Content.Length >= 100).ToList();
List<Tweet> shortTweetsSample = new List<Tweet>();
List<Tweet> longTweetsSample = new List<Tweet>();
int sampleSize = 30;
Random rand = new Random();
for (int i = 0; i < sampleSize; i++) {
int r = rand.Next(shortTweetsArchive.Count);
shortTweetsSample.Add(shortTweetsArchive[r]);
shortTweetsArchive.RemoveAt(r); // Sampling without replacement
}
for (int i = 0; i < sampleSize; i++) {
int r = rand.Next(longTweetsArchive.Count);
longTweetsSample.Add(longTweetsArchive[r]);
longTweetsArchive.RemoveAt(r);
}
Func<Tweet, int> getEngagements = t => (t.RetweetCount + t.FavoriteCount);
IEnumerable<int> shortTweetsSampleData = shortTweetsSample.Select(getEngagements);
IEnumerable<int> longTweetsSampleData = longTweetsSample.Select(getEngagements);
double shortAvg = shortTweetsSampleData.Average();
double shortSD = shortTweetsSampleData.StandardDeviation();
double longAvg = longTweetsSampleData.Average();
double longSD = longTweetsSampleData.StandardDeviation();
Console.WriteLine("Short Tweets Sample Size: {0}", shortTweetsSample.Count);
Console.WriteLine(" Mean Engagements: {0}", shortAvg);
Console.WriteLine(" Std Dev: {0}", shortSD);
Console.WriteLine(" Long Tweets Sample Size: {0}", longTweetsSample.Count);
Console.WriteLine(" Mean Engagements: {0}", longAvg);
Console.WriteLine(" Std Dev: {0}", longSD);
IOUtils.Export(shortTweetsSampleData, /* OUTPUT LOCATION */);
IOUtils.Export(longTweetsSampleData, /* OUTPUT LOCATION */);
Console.WriteLine("* Done");
Console.ReadKey();
}
}
public class Tweet
{
public string ID { get; set; }
public string Author { get; set; }
public string PublishDate { get; set; }
public string Content { get; set; }
public int RetweetCount { get; set; }
public int FavoriteCount { get; set; }
}
public class TwitterService
{
private const string userAgent = "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36";
// TODO: Add capability of specifying number of tweets to pull
// NOTE: Scraping: Violates Twitter's Terms of Service?
private string DownloadPage(string url = "http://m.twitter.com")
{
WebClient wc = new WebClient();
wc.Headers.Add("user-agent", userAgent);
return wc.DownloadString(url);
}
private string LoadPageFromFile(string filepath)
{
string content = File.ReadAllText(filepath);
return content;
}
// Source can be a URL or an HTML file
public List<Tweet> GetTweets(string source)
{
string res = null;
if (source.ToLower().Contains("twitter.com"))
{
res = DownloadPage(source);
}
else if (source.ToLower().EndsWith(".html"))
{
res = LoadPageFromFile(source);
}
else
{
throw new ArgumentException("Invalid source.");
}
CQ dom = CQ.Create(res);
CQ timeline = dom["div.Timeline-item[role='row']:not([jsaction='moreClick']) > div[role='gridcell']"];
List<Tweet> tweets = new List<Tweet>();
foreach (var timelineItem in timeline)
{
var item = CQ.Create(timelineItem);
Tweet t = new Tweet()
{
ID = item.Find("div.Tweet").Attr("data-tweet-id"),
Author = item.Find("span.UserNames-screenName").Text(),
PublishDate = item.Find("a.Tweet-timestamp > time").Attr("datetime"),
Content = item.Find("div.Tweet-text").Text(),
RetweetCount = ParseNumber(item.Find("span.TweetAction-count").Eq(0).Text()),
FavoriteCount = ParseNumber(item.Find("span.TweetAction-count").Eq(1).Text())
};
tweets.Add(t);
}
return tweets;
}
// Converts representations of numbers like 13.5K to 135000
// and 1,234 to 1234
private int ParseNumber(string numString)
{
int num = -1;
if (numString.EndsWith("K"))
{
numString = numString.Replace("K", "");
int dotIdx = numString.IndexOf(".");
if (dotIdx > -1)
{
int thousands = Convert.ToInt32(numString.Substring(0, dotIdx));
int hundreds = Convert.ToInt32(numString.Substring(dotIdx + 1));
num = thousands * 1000 + hundreds * 100;
}
else
{
int thousands = Convert.ToInt32(numString);
num = thousands * 1000;
}
}
else if (numString.Contains(","))
{
num = Convert.ToInt32(numString.Replace(",", ""));
}
else
{
num = Convert.ToInt32(numString);
}
return num;
}
}
public static class LinqExtensions
{
// Sample Standard Deviation
public static double StandardDeviation(this IEnumerable<int> list)
{
int n = list.Count();
double avg = list.Average();
double s = 0;
foreach (double i in list)
{
double diff = i - avg;
s += diff * diff;
}
double stdDev = Math.Sqrt((1.0 / (n - 1)) * s);
return stdDev;
}
}
public static class IOUtils
{
public static void Export(IEnumerable<int> source, string path)
{
string data = String.Join("\r\n", source);
// Assume file does not already exist
File.WriteAllText(path, data);
}
}