diff options
author | bie <bie@blekksprut.net> | 2017-06-04 22:09:29 +0900 |
---|---|---|
committer | bie <bie@blekksprut.net> | 2017-06-04 22:09:29 +0900 |
commit | 360c0b89a07080150a34a3d186b61b95838e068a (patch) | |
tree | 7f3114f790ee2ce6d93baf614c6bd767e32936da | |
parent | 6937d39110c31c16dae49b43d99152adafe206b3 (diff) | |
download | lupin-360c0b89a07080150a34a3d186b61b95838e068a.tar.xz |
embed
-rwxr-xr-x | embed | 81 |
1 files changed, 81 insertions, 0 deletions
@@ -0,0 +1,81 @@ +#!/usr/bin/env lua + +require 'tokyocabinet' + +json = require 'json' +http = require 'lcurl' + +CACHE = "/home/bie/tmp.db" + +cache = tokyocabinet.bdbnew() +cache:open(CACHE, cache.OWRITER + cache.OREADER) + +function oembed(url) + local cached = cache:get(url) + if cached then + local data = json.decode(cached) + return data.html + end + + local body = "" + local result = http.easy{url=url, writefunction=function(s) + body = body .. s + end}:perform() + if result:getinfo(http.INFO_RESPONSE_CODE) == 200 then + cache:put(url, body) + local data = json.decode(body) + return data.html + else + return ("<b>Klarte ikke embedde %s :(</b>"):format(url) + end +end + +function twitter(id) + return oembed("https://publish.twitter.com/oembed?url=" .. id) +end + +function youtube(id) + return oembed("http://www.youtube.com/oembed?url=" .. id) +end + +function soundcloud(id) + return oembed("https://soundcloud.com/oembed?format=json&url=" .. id) +end + +function vimeo(id) + return oembed("https://developer.vimeo.com/apis/oembed.json?url=" .. id) +end + +function twitch(id) + return oembed("https://api.twitch.tv/v4/oembed?url=" .. id) +end + +function instagram(id) + return oembed("https://api.instagram.com/oembed?url=" .. id) +end + +function flickr(id) + return oembed("https://www.flickr.com/services/oembed?url=" .. id) +end + +function facebook(id) + return oembed("https://www.facebook.com/plugins/video/oembed.json?url=" .. id) +end + +-- if arg[1] then +-- print(arg[1]) +-- io.input(arg[1]) +-- end + +for line in io.lines() do + line = line:gsub("$%s*twitter:%s*(.*)", twitter) + line = line:gsub("$%s*youtube:%s*(.*)", youtube) + line = line:gsub("$%s*soundcloud:%s*(.*)", soundcloud) + line = line:gsub("$%s*twitch:%s*(.*)", twitch) + line = line:gsub("$%s*vimeo:%s*(.*)", vimeo) + line = line:gsub("$%s*instagram:%s*(.*)", instagram) + line = line:gsub("$%s*flickr:%s*(.*)", flickr) + line = line:gsub("$%s*facebook:%s*(.*)", facebook) + print(line) +end + |