import opengraph
import redis

from jinja2 import Environment, BaseLoader
import traceback

bubble3="""
{
  "type": "bubble",
  "direction": "ltr",
  "header": {
    "type": "box",
    "layout": "vertical",
    "contents": [
      {
        "type": "box",
        "layout": "horizontal",
        "action": {
          "type": "uri",
          "uri": "https://page.line.me/choozmo"
        },
        "width": "100%",
        "contents": [
          {
            "type": "text",
            "text": "點我申請",
            "align": "start",
            "contents": []
          },
          {
            "type": "image",
            "url": "https://i.imgur.com/wQbJeGc.png",
            "align": "end",
            "aspectRatio": "4:1",
            "aspectMode": "cover"
          }
        ]
      }
    ]
  },
  "body": {
    "type": "box",
    "layout": "vertical",
    "paddingAll": "0px",
    "width": "100%",
    "backgroundColor": "#000000FF",
    "action": {
          "type": "uri",
          "uri": "{{ qlink }}"
    },

    "contents": [
      {
        "type": "box",
        "layout": "vertical",

        "contents": [
          {
            "type": "image",
            "url": "{{ imgurl }}",
            "size": "full",
            "aspectMode": "cover"
          },
          {
            "type": "box",
            "layout": "vertical",
            "position": "absolute",
            "offsetBottom": "0px",
            "width": "100%",
            "height": "90px",
            "backgroundColor": "#9C8E7ECC",
            "contents": [
              {
                "type": "text",
                "text": "{{ imgtitle }}",
                "color": "#FFFFFFFF",
                "align": "start",
                "wrap": true,
                "contents": []
              }
            ]
          }
        ]
      },
      {
        "type": "box",
        "layout": "horizontal",
        "position": "absolute",
        "offsetTop": "10px",
        "offsetStart": "5px",
        "width": "100px",
        "borderWidth": "1px",
        "backgroundColor": "#898CDFFF",
        "cornerRadius": "5px",
        "contents": [
          {
            "type": "text",
            "text": "{{title}}",
            "color": "#FFFFFFFF",
            "align": "center",
            "contents": []
          }
        ]
      }
    ]
  },
"footer": {
    "type": "box",
    "layout": "vertical",
    "contents": [
      {
        "type": "text",
        "text": "相關關鍵字",
        "contents": []
      },
      {
        "type": "separator"
      },
      {
        "type": "box",
        "layout": "horizontal",
        "contents": [
          {
            "type": "text",
            "text": "{{K1}}",
            "contents": []
          },
          {
            "type": "text",
            "text": "{{K2}}",
            "contents": []
          }
        ]
      },
      {
        "type": "box",
        "layout": "horizontal",
        "contents": [
          {
            "type": "text",
            "text": "{{K3}}",
            "contents": []
          },
          {
            "type": "text",
            "text": "{{K4}}",
            "contents": []
          }
        ]
      }
    ]
  }
}
"""


data3_begin="""
{
  "type": "carousel",
  "contents": [
"""


data3_end="""
  ]
}
"""


import dataset
import json
import sys


result_lst=[]
result_lst.append({'title':'餐飲業補助','imgurl':'https://s.yimg.com/ny/api/res/1.2/9YvKYByhEh1BZ7T4I.r.2A--/YXBwaWQ9aGlnaGxhbmRlcjt3PTk2MDtoPTU0MDtjZj13ZWJw/https://s.yimg.com/uu/api/res/1.2/x61AeEz53wkYlI70v5o7gQ--~B/aD01NzY7dz0xMDI0O2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/zh-tw/ftvn.com.tw/8d2b186dec3629f6a7af920dbbe5a48b','qlink':'https://finance.ettoday.net/news/2252097','imgtitle':'經濟部助餐飲業抗疫 補助行銷最高10萬','K1':'餐飲 業者','K2':'行銷 補助','K3':'經濟部 餐飲業 補助','K4':'餐飲業 補助'})


r = redis.Redis(host='db.ptt.cx', port=6379, db=2,password='choozmo9')

rtemplate = Environment(loader=BaseLoader).from_string(bubble3)

result=""
for r2 in result_lst:
#  r3 = rtemplate.render({'title':title,'imgurl':imgurl,'qlink':qlink,'imgtitle':imgtitle,'K1':k1,'K2':k2,'K3':k3,'K4':k4})
  r3 = rtemplate.render(r2)

  result+=r3
  result+=','

result=result[:-1]
final_result=data3_begin+result+data3_end
full_result='{"title":"申請餐飲補助","data":'+final_result+'}'
#final_result=data3_begin+result+data3_end

print(final_result)
#r.set('btn_data',final_result)
r.set('liff3',full_result)
#r.set('btn_data',data2)
sys.exit()
