Skip to content

Instantly share code, notes, and snippets.

@vic
Created June 18, 2013 13:59
Show Gist options
  • Select an option

  • Save vic/5805585 to your computer and use it in GitHub Desktop.

Select an option

Save vic/5805585 to your computer and use it in GitHub Desktop.
Extracting vine's video url from tweets, from www.antojame.com code.
var $ = require('jquery')
var _ = require('underscore')
var Twitter = require('twitter-api');
var twitter_consumer = {
'consumer_key' : 'XXX',
'consumer_secret' : 'XXX'
}
var twitter_token = {
'access_token_key' : 'XXX',
'access_token_secret' : 'XXX'
}
var newTwitter = function() {
var credentials = _.extend({}, twitter_token, twitter_consumer)
var twitter = Twitter.createClient()
twitter.setAuth(
credentials.consumer_key,
credentials.consumer_secret,
credentials.access_token_key,
credentials.access_token_secret
)
return twitter;
}
var mainStream = null;
exports.DEFAULT_SEARCH = 'yum, delicious, comer, comida, antojo, antojame, antojo';
exports.DEFAULT_STREAM = 'food, yum, comida, vegan, antojame, delicious, antojo, pizza, burger, taco, sushi, enchiladas, mole, arroz, comer';
exports.PAYED_SEARCH = 'antojame'
exports.PAYED_STREAM = 'antojame'
exports.initMainStream = function() {
mainStream = exports.stream()
return mainStream
}
exports.latests = function(antojo) {
antojo = antojo || exports.DEFAULT_SEARCH;
antojo = 'vine.co ' + _(antojo.split(',')).map(function(e){ return e }).join(' OR ')
console.log(antojo)
var twitter = newTwitter()
var deferred = $.Deferred()
twitter.get( 'search/tweets', {q: antojo}, function(data, error) {
var vines = []
var seen = 0;
if (!error) {
$.each(data.statuses, function(){
var promise = getVineFromTweet(this)
promise.done(function(vine){ vines.push(vine) })
promise.always(function(){
seen ++;
if(seen == data.statuses.length) {
deferred.resolve(vines)
}
})
})
} else {
deferred.reject(error)
}
})
return deferred
}
exports.stream = function(antojo) {
var twitter = newTwitter();
antojo = antojo || exports.DEFAULT_STREAM
var stream = null;
var query = {
track: "vine.co AND (" + antojo + ")"
}
var cancel = function() {
twitter.abort()
}
var callback = function(){}
twitter.stream('statuses/filter', query, function(json){
var tweet = JSON.parse(json);
getVineFromTweet(tweet).done(callback)
})
return {
cancel: cancel,
onVine: function(cb) { callback = cb }
};
}
var getVineFromTweet = function(tweet){
var deferred = $.Deferred()
if (tweet.source && tweet.source.match(/vine\.co/)) {
var tweet = tweet.text.match(/(.*)(https?:\/\/[^ ]+)/)
var text = tweet[1]
var vine = tweet[2]
vine = vine.replace('https:', 'http:')
$.ajax({
url: vine,
success: function(){
deferred.reject(tweet)
},
error: function(data) {
var vine = data.getResponseHeader('location');
$.ajax({
url: vine,
success: function(data) {
var body = data.match(/source src=\"([^ ]+)\"/)
deferred.resolve({ url: body[1], text: text})
},
error: function(){
deferred.reject(tweet)
}
})
}
})
} else {
deferred.reject(tweet)
}
return deferred.promise()
}
@vic
Copy link
Copy Markdown
Author

vic commented Jun 18, 2013

getVineFromTweet resolves to an object with two properties, url: the mp4 video, and text description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment