Skip to content

Instantly share code, notes, and snippets.

@DannyDelott
DannyDelott / setInterval().js
Last active August 29, 2015 14:15
Standard setInterval() implementation
var interval = setInterval(function() {
// all my code
}, 3000);
@DannyDelott
DannyDelott / setInterval().js
Last active August 29, 2015 14:15
Function-first setInterval() implementation
var interval = setInterval(function update() {
// all my code
return update;
}(), 3000);
@DannyDelott
DannyDelott / getStream().java
Last active August 29, 2015 14:15
Gets the TwitterStream object by authenticating with the Twitter API
public static TwitterStream getStream() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey("<YOUR CONSUMER KEY>");
cb.setOAuthConsumerSecret("<YOUR CONSUMER SECRET>");
cb.setOAuthAccessToken("<YOUR ACCESS TOKEN>");
cb.setOAuthAccessTokenSecret("<YOUR ACCESS TOKEN SECRET>");
return new TwitterStreamFactory(cb.build()).getInstance();
@DannyDelott
DannyDelott / Main.java
Created February 11, 2015 21:35
Gets the TwitterStream object inside Main.java
private static TwitterStream twitter;
private static int numVinesScraped = 0; // see Step 4
private static final int NUM_VINES_TO_DOWNLOAD = -1; // see Step 4
private static final String SAVE_DIRECTORY = “vines/“; // see Step 4
public static void main(String[] args) {
// Connects to the Streaming API
twitter = TwitterStreamBuilderUtil.getStream();
@DannyDelott
DannyDelott / Track keywords.java
Created February 12, 2015 17:54
Tracking keywords
// sets keyword to track
FilterQuery fq = new FilterQuery();
String keyword[] = { “http” };
fq.track(keyword);
@DannyDelott
DannyDelott / TweetBuffer.java
Last active August 29, 2015 14:15
Stores collected tweets from the Twitter API
public class TweetBuffer {
private int id;
private String saveDirectory;
private HashSet<Status> tweets;
private boolean isProcessing;
// constructor
public TweetBuffer(int id, String saveDirectory) {
@DannyDelott
DannyDelott / Main.java
Created February 12, 2015 17:58
Global variables in Main.java
private static TweetBuffer buffer1, buffer2;
private static int currentBufferId; // 1 or 2
private static final int MIN_BUFFER_SIZE = 10;
private static HashSet<String> duplicateUrls; // holds Vine URLs
@DannyDelott
DannyDelott / Main.java
Created February 14, 2015 23:53
Begin scraping Vine Videos
// instantiates buffers
buffer1 = new TweetBuffer(1, SAVE_DIRECTORY);
buffer2 = new TweetBuffer(2, SAVE_DIRECTORY);
currentBufferId = 1;
duplicateUrls = new HashSet < String > ();
// instantiates listener to fill and process the buffers
StatusListener listener = new StatusListener() {
@Override
@DannyDelott
DannyDelott / Main.java
Created February 14, 2015 23:56
Process a buffer to find Vine videos
private static void processCurrentBuffer() {
// Does nothing if the current buffer is already processing or
// if it contains fewer elements than the minimum buffer size.
if ((currentBufferId == 1 && (buffer1.isProcessing() || buffer1
.getTweets().size() < MIN_BUFFER_SIZE)) ||
(currentBufferId == 2 && (buffer2.isProcessing() || buffer2
.getTweets().size() < MIN_BUFFER_SIZE))) {
return;
}
@DannyDelott
DannyDelott / ProcessingListener.java
Created February 14, 2015 23:59
Listener for when the buffer has finished processing
public interface ProcessingListener {
public void onProcessFinished(int bufferId, HashSet urls,
int numScraped);
}