Created
January 28, 2012 23:45
-
-
Save eljojo/1696301 to your computer and use it in GitHub Desktop.
javascript para buscar medios embebeables en una url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* media.js | |
* script para buscar medios embebeables en una url | |
* por josé tomás albornoz - www.eljojo.net | |
* usa trozos de código sacados de: https://twimg0-a.akamaihd.net/b/1/bundle/t1-more-en-201201121558.js | |
*/ | |
var mediajs = { | |
providers: { | |
youtube:{ | |
type: 'video', | |
matchers: { | |
//tinyUrl: /^(?:(?:https?\:\/\/)?(?:www\.)?)?youtu\.be\/([a-zA-Z0-9_\-\?\&\=\/]+)/i, | |
standardUrl: /^(?:(?:https?\:\/\/)?(?:www\.)?)?youtube\.com\/watch[a-zA-Z0-9_\-\?\&\=\/]+/i | |
}, | |
process: function(url, match_name, match) { | |
var result = {} | |
if(match_name == 'tinyURL') { | |
//return this.location.pathname.replace(/\//g, ""); | |
}else{ | |
result.id = url.match(/[\?\&]v\=([\w\-]+)(?:\&|$)/)[1] | |
} | |
return result; | |
}, | |
templates: { | |
thumbnail: 'http://img.youtube.com/vi/{id}/0.jpg', | |
video: 'http://www.youtube.com/embed/{id}' | |
} | |
}, | |
yfrog: { | |
type: 'photo', | |
matchers: { | |
media: /^(?:(?:https?\:\/\/)?(?:www\.)?)?yfrog\.(?:com|ru|com\.tr|it|fr|co\.il|co\.uk|com\.pl|pl|eu|us)\/(\w+)/i | |
}, | |
process: function(url, match_name, match) { | |
return {base_url: match[0]} | |
}, | |
templates: { | |
thumbnail: '{base_url}:small', | |
medium: '{base_url}:medium', | |
} | |
}, | |
twitpic: { | |
type: 'photo', | |
matchers: { | |
media: /^(?:(?:https?\:\/\/)?(?:www\.)?)?twitpic\.com\/(?!(?:place|photos|events)\/)([a-zA-Z0-9\?\=\-]+)/i | |
}, | |
process: function(url, match_name, match) { | |
return {id: match[1]} | |
}, | |
templates: { | |
thumbnail: 'http://twitpic.com/show/thumb/{id}', | |
mini: 'http://twitpic.com/show/mini/{id}', | |
medium: 'http://twitpic.com/show/iphone/{id}', | |
} | |
} | |
}, | |
utils: { | |
//optional_protocol: /(?:(?:https?\:\/\/)?(?:www\.)?)/, | |
//wildcard: /[a-zA-Z0-9_#\.\-\?\&\=\/]+/, | |
findProvider: function(url, providers) { | |
for(var provider_name in providers) { | |
var provider = providers[provider_name], | |
matchers = provider.matchers; | |
for(var match_name in matchers) { | |
var match = url.match(matchers[match_name]) | |
if(match) return {provider: provider, match: match, match_name: match_name, provider_name: provider_name}; | |
} | |
} | |
return false; | |
}, | |
parseTemplate: function(template, data) { | |
for(var data_name in data) { | |
template = template.replace('{'+data_name+'}', data[data_name]) | |
} | |
return template | |
} | |
}, | |
parseUrl: function(url) { | |
//console.log('parseando: '+url) | |
var provider_found = this.utils.findProvider(url, this.providers); | |
if(provider_found == false) return false; | |
var provider = provider_found.provider, | |
match = provider_found.match, | |
match_name = provider_found.match_name, | |
templates = provider.templates; | |
var provider_data = provider.process(url, match_name, match); | |
var extracted = {}; | |
for(var template_name in templates) { | |
extracted[template_name] = this.utils.parseTemplate(templates[template_name], provider_data) | |
} | |
var result = { | |
extracted: extracted, | |
provider: provider_found.provider_name, | |
url: url, | |
type: provider.type | |
} | |
return result; | |
} | |
} | |
/* | |
//var url = 'http://www.youtube.com/watch?v=t2hZA9T07sc&feature=g-vrec&context=G2dc6d8cRVAAAAAAAAAQ' | |
//var url = 'http://yfrog.com/kfae65j:medium'; | |
var url = 'http://twitpic.com/882xnc'; | |
var parsed_url = mediajs.parseUrl(url); | |
console.log(parsed_url) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment