-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 <andi (dot) b (at) outlook (dot) de>
-- DO NOT USE WITHOUT PERMISSION

do

-- TODO: Language selector (for e.g. this post: https://miiverse.nintendo.net/posts/AYMHAAACAAADVHkSrNJ-9Q)
-- <iframe class="youtube-player" type="text/html" width="490" height="276" src="https://www.youtube.com/embed/2kB7tUD-MJk?rel=0&amp;modestbranding=1&amp;iv_load_policy=3" frameborder="0"></iframe></div>

local function get_miiverse_data(res, post)
  username = string.match(res, "<p class%=\"user%-name\"><a href=\".-\">(.-)</a>")
  userid = string.match(res, "<span class%=\"user%-id\">(.-)</span>")
  timestamp = string.match(res, "<span class%=\"timestamp\">(.-)</span>")
   if string.starts(timestamp, 'Vor') then
     timestamp = string.gsub(timestamp, "Vor","")
     timestamp = "vor"..timestamp
   else
     timestamp = "am "..timestamp
   end
  community = string.match(res, "<div id%=\"page%-title\">(.-)</div>")
  --community = string.gsub(community, "™","")
  yeahs = string.match(res, "<span class%=\"empathy%-count\">(.-)</span>")
   if yeahs == "1" then yeahs = "1 Yeah" else yeahs = yeahs.." Yeahs" end
  replys = string.match(res, "<span class%=\"reply%-count\">(.-)</span>")
   if replys == "1" then replys = "1 Kommentar" else replys = replys.." Kommentaren" end
  youtube_link = string.match(res, "<iframe class%=\"youtube%-player\" .- src=\"(.-)\" frameborder")
   if youtube_link == nil then youtube_link = '' end
   if post ~= '' and youtube_link ~= nil then youtube_link = ' '..youtube_link end
end

local function get_miiverse_post(miiverse_postid, receiver)
  local url = 'https://miiverse.nintendo.net/posts/'..miiverse_postid
  local respbody = {}
  local options = {
    url = url,
    sink = ltn12.sink.table(respbody),
	headers = {["Accept-Language"] = "de-de",},
  }
  local response = {https.request(options)}
  local code = response[2]
  local res = table.concat(respbody)
  if code ~= 200 then return "Fehler beim Abrufen vom Miiverse" end
  local post = string.match(res, "<p class%=\"post%-content%-text.-\">(.-)<div class%=\"post%-meta\">")
  
  if post == nil then
    local memo = string.match(res, "<p class%=\"post%-content%-memo\"><img src%=\"(.-)\" class=\"post%-memo\"")
	if memo ~= nil then
	  post = ''
	  local file = download_to_file(memo)
      local cb_extra = {file_path=file}
	  send_photo(receiver, file, rmtmp_cb, cb_extra)
	else
      post = 'Fehler! Miiverse-Post konnte nicht abgerufen werden.' 
	end
  else
    post = string.gsub(post, "</p>","")
    post = string.gsub(post, "<p class.->","")
    post = unescape(post)
  end
 
  get_miiverse_data(res, post)
  local text = username..' ('..userid..') '..timestamp..' in "'..community..'" mit ' ..yeahs..' und '..replys..'\n\n'..post..youtube_link
  
  local photo = string.match(res, "<div class%=\"screenshot%-container still%-image\"><img src%=\"(.-)\"></div>")
  if photo ~= nil then
    local image_url = photo
    local cb_extra = {
      receiver=receiver,
      url=image_url
    }
    send_msg(receiver, text, send_photo_from_url_callback, cb_extra)
  else
    send_msg(receiver, text, ok_cb, false)
  end
end

local function run(msg, matches)
  local miiverse_postid = matches[1]
  local receiver = get_receiver(msg)
  get_miiverse_post(miiverse_postid, receiver)
end

return {
    description = "Postet Miiverse-Post", 
    usage = "Miiverse-Link: Postet Miiverse-Post",
    patterns = {"miiverse.nintendo.net/posts/([A-Za-z0-9-_-]+)"}, 
    run = run 
}

end