-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreddit.html
More file actions
182 lines (159 loc) · 8.3 KB
/
Copy pathreddit.html
File metadata and controls
182 lines (159 loc) · 8.3 KB
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
<html>
<meta http-equiv="Cache-Control" content="no-store" />
<head>
<title>Reddit WDC</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script src="https://online.tableau.com/javascripts/api/tableauwdc-2.2.0.js"; type="text/javascript"></script>
<script type="text/javascript">
(function() {
// Reddit WebDataConnector for Tableau
// Written by Justin Dallal for Tableau hackathon
// TODO:
// --DONE: Implement different types of "top" dropdown - all time, hour, day, week, month, year
// --DONE: Implement new, rising, controversial
// --DONE: Suppert paging/multiple queries to get more than 100 items back (can be done using &after=t3_link)
// See API usage for fullnames usage in using "after": https://www.reddit.com/dev/api/oauth#fullnames
// Note that we are rate limited to 30 queries/minute (ideally, 1 request every 2 seconds)
// --DONE: Implement dropdown of how many items to retrieve
// --DONE: Do testing to determine how many results we can retrieve on average without hitting rate limits
// See here: https://github.com/reddit/reddit/wiki/API
// --Move resultsCompleted to use connectionData and remove that god awful global variable
// --Implement incremental loading of data to get >3000 results
// --DONE: Support multiple subreddits (could be done w/ multiple queries OR with /r/politics+pics method)
// --DONE (NSFW): Implement exclusions in searches to remove: guilded posts, NSFW posts, stickied posts
// --DONE: Support proper Tableau dates.
var myConnector = tableau.makeConnector();
// Note that, even though the API supports upvotes/downvotes, up just means score and down is always 0.
// see the announcement here: https://www.reddit.com/r/announcements/comments/28hjga/reddit_changes_individual_updown_vote_counts_no/
myConnector.getColumnHeaders = function() {
var fieldNames = ['Title', 'Score', 'Comment Count', 'ID', 'URL', 'Domain', 'Created', 'Flair Text', 'Author', 'NSFW', 'Kind'];
var fieldTypes = ['string', 'int', 'int', 'string', 'string', 'string', 'datetime', 'string', 'string', 'bool', 'string'];
tableau.headersCallback(fieldNames, fieldTypes);
};
function buildURLAndSortingOptions(subReddit, sortingOption, afterID, count)
{
var urlToParse = 'http://www.reddit.com/r/' + subReddit;
var modifiers = "";
var queryOptions = {limit: 100};
switch(sortingOption)
{
case "":
break;
case "hot":
modifiers += "/hot";
break;
case "controversial":
modifiers += "/controversial";
break;
case "new":
modifiers += "/new";
break;
default:
modifiers += "/top";
queryOptions['t'] = sortingOption;
}
if(afterID != "")
{
queryOptions["after"] = afterID;
queryOptions["count"] = count;
}
urlToParse += modifiers + '/.json?jsonp=?';
return {"url": urlToParse, "options": queryOptions}
}
var resultsCompleted = 0; // stupid ugly hack. I'm not proud of this.
function getJSON(queryURL, queryOptions, moreResults, disableNSFW)
{
$.getJSON(queryURL, queryOptions, function(rd)
{
var results = [];
$.each(rd.data.children, function(k, v)
{
if(v.data.over_18 && disableNSFW)
return;
var d = new Date(parseInt(v.data.created) * 1000);
var entry = {
'Title': v.data.title,
'Score': v.data.score,
'Comment Count': v.data.num_comments,
'ID': v.data.id,
'URL': v.data.url,
'Domain': v.data.domain,
'Created': (d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate()),
'Flair Text': v.data.link_flair_text,
'Author': v.data.author,
'NSFW': v.data.over_18,
'Kind': v.data.kind,
};
results.push(entry);
});
if(results.length > 0)
tableau.dataCallback(results, 't3_' + results[results.length - 1]['ID'], moreResults);
});
}
myConnector.getTableData = function(lastRecordToken)
{
var connectionData = JSON.parse(tableau.connectionData);
var numPosts = connectionData["resultsRequested"];
var query = buildURLAndSortingOptions(connectionData["subreddit"], connectionData["sortingType"], lastRecordToken, resultsCompleted);
resultsCompleted += 100;
getJSON(query["url"], query["options"], numPosts - resultsCompleted > 0, connectionData["disableNSFW"]);
};
myConnector.setSubReddit = function(subreddit, sortingType, resultsRequested, disableNSFW)
{
// We are using tableau.connectionData to pass information around and maintain context without worrying about scope
var connectionData = {"subreddit": subreddit, "sortingType": sortingType, "resultsRequested": resultsRequested, "disableNSFW": disableNSFW};
tableau.connectionData = JSON.stringify(connectionData);
tableau.connectionName = 'Subreddit Data: ' + subreddit;
};
tableau.registerConnector(myConnector);
$(document).ready(function()
{
$("#submitButton").click(function()
{
var resultsRequested = parseInt($("#postCount").val(), 10);
var sortingType = $("#sorting").val()
var subreddit = $('#subredditName').val().trim();
var disableNSFW = $('#nsfw')[0].checked;
if (subreddit)
{
myConnector.setSubReddit(subreddit, sortingType, resultsRequested, disableNSFW);
tableau.submit();
}
});
});
})();
</script>
</head>
<body>
<form>
<p>Subreddit Data Grabber.</p>
Input subreddit name(s):
<input type="text" style="width: 500px;" id="subredditName" value="Examples: aww, pics+videos"/>
<br />
<br />
Sorting:
<select id="sorting">
<option value="hot">Hot</option>
<option value="controversial">Controversial</option>
<option value="new">New</option>
<option value="hour">Top of this hour</option>
<option value="day">Top of the last 24h</option>
<option value="year">Top of the last year</option>
<option value="all">Top of all time</option>
</select>
<br/>
Number of posts
<select id="postCount">
<option value="100">100</option>
<option value="300">300</option>
<option value="500">500</option>
<option value="1000">1000</option>
</select>
<br />
Disable NSFW posts <input type="checkbox" id="nsfw"/>
<br />
<br />
<button type="button" id="submitButton">Get Subreddit Data</button>
</form>
</body>
</html>