do

local makeOurDate = function(dateString)
  local pattern = "(%d+)%-(%d+)%-(%d+)"
  local year, month, day = dateString:match(pattern)
  return day..'.'..month..'.'..year
end

local DESC_LENTH = 400

local function get_psn_info(id)
  local url = 'https://store.playstation.com/store/api/chihiro/00_09_000/container/DE/de/999/'..id
  local res,code  = https.request(url)
  local data = json:decode(res)
  if code ~= 200 then return "HTTP-Fehler" end
  if not data then return "Nichts gefunden!" end
 
  local title = data.title_name
  local publish = data.provider_name
  local system = data.playable_platform[1]
  local type = data.gameContentTypesList[1].name
  if data.skus[1].rewards[1] then
  
    if data.skus[1].rewards[1].isPlus ~= true then
      psplus = ''
    else
      psplus = ' PS+ Exklusiv!'
    end  
	
    price = data.skus[1].rewards[1].display_price..' statt '..data.default_sku.display_price..' (Spare '..data.skus[1].rewards[1].discount..'%)'..psplus
  else
    price = data.default_sku.display_price..psplus
  end
  local fsk = 'FSK'..data.age_limit
  local description = string.sub(unescape(data.long_desc:gsub("%b<>", "")), 1, DESC_LENTH)..'...'
  local release = makeOurDate(data.release_date)
  if data.images[4].type ~= 10 then
    image_url = data.images[4].url
  else
    image_url = data.images[1].url
  end
  
  local text = title..' von '..publish..' für '..system..' ['..type..']\nPreis: '..price..'\n'..fsk..'\n'..'Release: '..release..'\n\nBeschreibung:\n'..description
 
    return text, image_url

end

local function run(msg, matches)
local id = matches[1]
  local text, image_url = get_psn_info(id)
  local receiver = get_receiver(msg)
  if image_url then
    local file = download_to_file(image_url)
    send_photo(receiver, file, ok_cb, false)
  end
  return text
end

return {
  description = "Zeigt PlayStation Store Infos", 
  usage = "Link zum PlayStation Store",
  patterns = {"store.playstation.com/#!/de%-de/spiel/.*/cid=(.*)"},
  run = run 
}

--by Akamaru [https://ponywave.de]

end