From 360c0b89a07080150a34a3d186b61b95838e068a Mon Sep 17 00:00:00 2001 From: bie Date: Sun, 4 Jun 2017 22:09:29 +0900 Subject: embed --- embed | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100755 embed diff --git a/embed b/embed new file mode 100755 index 0000000..59cb388 --- /dev/null +++ b/embed @@ -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 ("Klarte ikke embedde %s :("):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 + -- cgit v1.0