Jared vor 2 Jahren
Ursprung
Commit
17ebf47d10
3 geänderte Dateien mit 955 neuen und 113 gelöschten Zeilen
  1. 13 2
      chrometest.py
  2. 131 0
      opengraph.py
  3. 811 111
      redis_set.py

+ 13 - 2
chrometest.py

@@ -75,7 +75,15 @@ def restart_browser():
 
 driver=restart_browser()
 driver.get('https://liff.googo.org/liff2/')
-time.sleep(25)
+time.sleep(10)
+
+elmt=driver.find_element(By.XPATH,"//button[@id='senddm']")
+webdriver.ActionChains(driver).move_to_element(elmt).perform()
+webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
+time.sleep(12)
+#time.sleep(99999)
+
+
 
 for window_handle in driver.window_handles:
 #    driver.switch_to(window_handle)
@@ -91,7 +99,10 @@ for window_handle in driver.window_handles:
 
         for elmt in elmts:
             print(elmt.text)
-            if elmt.text != 'LINE':
+#            if elmt.text != 'LINE':
+#            if 'ChoozMo' in elmt.text :
+            if 'Jared' in elmt.text :
+
                 if len(elmt.text)>2:
 #                    inp=elmt.find_element(By.TAG_NAME,"input") 
                     inp=elmt.find_element(By.XPATH,".//input")

+ 131 - 0
opengraph.py

@@ -0,0 +1,131 @@
+# encoding: utf-8
+
+import re
+
+try:
+    import urllib.request, urllib.error, urllib.parse
+except ImportError:
+    from urllib import request as urllib2
+
+try:
+    from bs4 import BeautifulSoup
+except ImportError:
+    from BeautifulSoup import BeautifulSoup
+
+global import_json
+try:
+    import json
+    import_json = True
+except ImportError:
+    import_json = False
+
+class OpenGraph(dict):
+    """
+    """
+
+    required_attrs = ['title', 'type', 'image', 'url', 'description']
+
+    def __init__(self, url=None, html=None, scrape=False, **kwargs):
+        # If scrape == True, then will try to fetch missing attribtues
+        # from the page's body
+
+        self.scrape = scrape
+        self._url = url
+
+        for k in list(kwargs.keys()):
+            self[k] = kwargs[k]
+
+        dict.__init__(self)
+
+        if url is not None:
+            self.fetch(url)
+
+        if html is not None:
+            self.parser(html)
+
+    def __setattr__(self, name, val):
+        self[name] = val
+
+    def __getattr__(self, name):
+        return self[name]
+
+    def fetch(self, url):
+        """
+        """
+        raw = urllib.request.urlopen(url)
+        html = raw.read()
+        return self.parser(html)
+
+    def parser(self, html):
+        """
+        """
+        if not isinstance(html,BeautifulSoup):
+            doc = BeautifulSoup(html, "lxml")
+        else:
+            doc = html
+        ogs = doc.html.head.findAll(property=re.compile(r'^og'))
+        for og in ogs:
+            if og.has_attr('content'):
+                self[og['property'][3:]]=og['content']
+        # Couldn't fetch all attrs from og tags, try scraping body
+        if not self.is_valid() and self.scrape:
+            for attr in self.required_attrs:
+                if not self.valid_attr(attr):
+                    try:
+                        self[attr] = getattr(self, 'scrape_%s' % attr)(doc)
+                    except AttributeError:
+                        pass
+
+    def valid_attr(self, attr):
+        return self.get(attr) and len(self[attr]) > 0
+
+    def is_valid(self):
+        return all([self.valid_attr(attr) for attr in self.required_attrs])
+
+    def to_html(self):
+        if not self.is_valid():
+            return "<meta property=\"og:error\" content=\"og metadata is not valid\" />"
+
+        meta = ""
+        for key,value in self.items():
+            meta += "\n<meta property=\"og:%s\" content=\"%s\" />" %(key, value)
+        meta += "\n"
+
+        return meta
+
+    def to_json(self):
+        # TODO: force unicode
+        global import_json
+        if not import_json:
+            return "{'error':'there isn't json module'}"
+
+        if not self.is_valid():
+            return json.dumps({'error':'og metadata is not valid'})
+
+        return json.dumps(self)
+
+    def to_xml(self):
+        pass
+
+    def scrape_image(self, doc):
+        images = [dict(img.attrs)['src']
+            for img in doc.html.body.findAll('img')]
+
+        if images:
+            return images[0]
+
+        return ''
+
+    def scrape_title(self, doc):
+        return doc.html.head.title.text
+
+    def scrape_type(self, doc):
+        return 'other'
+
+    def scrape_url(self, doc):
+        return self._url
+
+    def scrape_description(self, doc):
+        tag = doc.html.head.findAll('meta', attrs={"name":"description"})
+        result = "".join([t['content'] for t in tag])
+        return result

+ 811 - 111
redis_set.py

@@ -1,186 +1,886 @@
+import opengraph
 import redis
+
+from jinja2 import Environment, BaseLoader
+
 data="""
 {
-  "type": "carousel",
-  "contents": [
-    {
-      "type": "bubble",
-      "size": "nano",
-      "header": {
+  "type": "bubble",
+  "header": {
+    "type": "box",
+    "layout": "vertical",
+    "contents": [
+      {
+        "type": "text",
+        "text": "集仕多 ChoozMo",
+        "weight": "bold",
+        "size": "xl",
+        "color": "#ffffff"
+      },
+      {
+        "type": "text",
+        "text": "新聞 AI 主播媒體聯播網最佳方案",
+        "size": "md",
+        "color": "#ffffff"
+      },
+      {
         "type": "box",
-        "layout": "vertical",
+        "layout": "baseline",
+        "margin": "md",
+        "contents": [
+          {
+            "type": "icon",
+            "size": "xxl",
+            "url": "https://ppt.cc/fLP10x@.png",
+            "offsetTop": "xs"
+          }
+        ],
+        "justifyContent": "flex-end",
+        "offsetStart": "255px",
+        "position": "absolute",
+        "offsetBottom": "20px"
+      }
+    ],
+    "backgroundColor": "#e1642b"
+  },
+  "hero": {
+    "type": "image",
+    "url": "https://ppt.cc/fDmzAx@.png",
+    "size": "full",
+    "aspectRatio": "20:13",
+    "action": {
+      "type": "uri",
+      "uri": "http://linecorp.com/"
+    },
+    "offsetTop": "lg"
+  },
+  "body": {
+    "type": "box",
+    "layout": "vertical",
+    "contents": [
+      {
+        "type": "text",
+        "text": "圖文+影音+聯播",
+        "color": "#aaaaaa",
+        "size": "sm"
+      },
+      {
+        "type": "text",
+        "text": "全方位媒體曝光,一次到位!",
+        "color": "#aaaaaa",
+        "size": "sm"
+      },
+      {
+        "type": "box",
+        "layout": "baseline",
+        "margin": "md",
         "contents": [
+          {
+            "type": "icon",
+            "size": "md",
+            "url": "https://ppt.cc/feXZPx@.png",
+            "offsetTop": "xs"
+          },
           {
             "type": "text",
-            "text": "中文測是",
-            "color": "#ffffff",
-            "align": "start",
+            "text": "網路新聞圖文報導",
+            "offsetStart": "md",
+            "color": "#e47e4d",
+            "weight": "bold"
+          }
+        ]
+      },
+      {
+        "type": "box",
+        "layout": "baseline",
+        "margin": "md",
+        "contents": [
+          {
+            "type": "icon",
             "size": "md",
-            "gravity": "center"
+            "url": "https://ppt.cc/feXZPx@.png",
+            "offsetTop": "xs"
           },
           {
             "type": "text",
-            "text": "70%",
-            "color": "#ffffff",
-            "align": "start",
-            "size": "xs",
+            "text": "十二家網路媒體聯播",
+            "offsetStart": "md",
+            "color": "#e47e4d",
+            "weight": "bold"
+          }
+        ]
+      },
+      {
+        "type": "box",
+        "layout": "baseline",
+        "margin": "md",
+        "contents": [
+          {
+            "type": "icon",
+            "size": "md",
+            "url": "https://ppt.cc/feXZPx@.png",
+            "offsetTop": "xs"
+          },
+          {
+            "type": "text",
+            "text": "AI 主播影音播報影片",
+            "offsetStart": "md",
+            "color": "#e47e4d",
+            "weight": "bold"
+          }
+        ]
+      },
+      {
+        "type": "box",
+        "layout": "horizontal",
+        "margin": "lg",
+        "spacing": "sm",
+        "contents": [
+          {
+            "type": "text",
+            "text": "一次到位擁有,只要",
+            "margin": "none",
+            "size": "sm",
             "gravity": "center",
-            "margin": "lg"
+            "color": "#2384e3",
+            "weight": "bold"
+          },
+          {
+            "type": "text",
+            "text": "$36,000",
+            "size": "xxl",
+            "offsetStart": "5px",
+            "weight": "bold",
+            "color": "#2384e3"
           }
         ],
-        "backgroundColor": "#27ACB2",
-        "paddingTop": "19px",
-        "paddingAll": "12px",
-        "paddingBottom": "16px"
+        "backgroundColor": "#fff3ed",
+        "background": {
+          "type": "linearGradient",
+          "angle": "0deg",
+          "startColor": "#fff3ed",
+          "endColor": "#ffffff"
+        }
       },
-      "body": {
+      {
+        "type": "text",
+        "text": "不論是公司宣傳/產品發表/品牌經營,都能透過新聞稿發布,提升曝光及知名度!",
+        "wrap": true,
+        "size": "13px",
+        "align": "start",
+        "offsetTop": "10px"
+      }
+    ]
+  },
+  "footer": {
+    "type": "box",
+    "layout": "vertical",
+    "spacing": "sm",
+    "contents": [
+      {
         "type": "box",
         "layout": "vertical",
         "contents": [
           {
-            "type": "box",
-            "layout": "horizontal",
-            "contents": [
-              {
-                "type": "text",
-                "text": "這是一個測試efore class",
-                "color": "#8C8C8C",
-                "size": "sm",
-                "wrap": true
-              }
-            ],
-            "flex": 1
+            "type": "button",
+            "style": "link",
+            "height": "sm",
+            "action": {
+              "type": "uri",
+              "label": "請來採訪我",
+              "uri": "https://page.line.me/choozmo"
+            },
+            "color": "#FFFFFF"
           }
         ],
-        "spacing": "md",
-        "paddingAll": "12px"
+        "backgroundColor": "#e47e4d",
+        "offsetTop": "-3px"
       },
-      "styles": {
-        "footer": {
-          "separator": false
-        }
-      }
-    },
-    {
-      "type": "bubble",
-      "size": "nano",
-      "header": {
+      {
         "type": "box",
         "layout": "vertical",
         "contents": [
+          {
+            "type": "button",
+            "style": "link",
+            "height": "sm",
+            "action": {
+              "type": "uri",
+              "label": "了解更多",
+              "uri": "https://page.line.me/choozmo"
+            },
+            "color": "#FFFFFF"
+          }
+        ],
+        "backgroundColor": "#06C152"
+      }
+    ],
+    "flex": 0
+  }
+}
+"""
+
+data2="""
+{
+  "type": "bubble",
+  "header": {
+    "type": "box",
+    "layout": "vertical",
+    "contents": [
+      {
+        "type": "text",
+        "text": "集仕多 ChoozMo",
+        "weight": "bold",
+        "size": "xl",
+        "color": "#ffffff"
+      },
+      {
+        "type": "text",
+        "text": "新聞 AI 主播媒體聯播網最佳方案",
+        "size": "md",
+        "color": "#ffffff"
+      },
+      {
+        "type": "box",
+        "layout": "baseline",
+        "margin": "md",
+        "contents": [
+          {
+            "type": "icon",
+            "size": "xxl",
+            "url": "https://ppt.cc/fLP10x@.png",
+            "offsetTop": "xs"
+          }
+        ],
+        "justifyContent": "flex-end",
+        "offsetStart": "255px",
+        "position": "absolute",
+        "offsetBottom": "20px"
+      }
+    ],
+    "backgroundColor": "#e1642b"
+  },
+  "hero": {
+    "type": "image",
+    "url": "https://ppt.cc/fDmzAx@.png",
+    "size": "full",
+    "aspectRatio": "20:13",
+    "offsetTop": "lg"
+  },
+  "body": {
+    "type": "box",
+    "layout": "vertical",
+    "contents": [
+      {
+        "type": "text",
+        "text": "圖文+影音+聯播",
+        "color": "#aaaaaa",
+        "size": "sm"
+      },
+      {
+        "type": "text",
+        "text": "全方位媒體曝光,一次到位!",
+        "color": "#aaaaaa",
+        "size": "sm"
+      },
+      {
+        "type": "box",
+        "layout": "baseline",
+        "margin": "md",
+        "contents": [
+          {
+            "type": "icon",
+            "size": "md",
+            "url": "https://ppt.cc/feXZPx@.png",
+            "offsetTop": "xs"
+          },
           {
             "type": "text",
-            "text": "Pending",
-            "color": "#ffffff",
-            "align": "start",
+            "text": "網路新聞圖文報導",
+            "offsetStart": "md",
+            "color": "#e47e4d",
+            "weight": "bold"
+          }
+        ]
+      },
+      {
+        "type": "box",
+        "layout": "baseline",
+        "margin": "md",
+        "contents": [
+          {
+            "type": "icon",
             "size": "md",
-            "gravity": "center"
+            "url": "https://ppt.cc/feXZPx@.png",
+            "offsetTop": "xs"
           },
           {
             "type": "text",
-            "text": "30%",
-            "color": "#ffffff",
-            "align": "start",
-            "size": "xs",
+            "text": "十二家網路媒體聯播",
+            "offsetStart": "md",
+            "color": "#e47e4d",
+            "weight": "bold"
+          }
+        ]
+      },
+      {
+        "type": "box",
+        "layout": "baseline",
+        "margin": "md",
+        "contents": [
+          {
+            "type": "icon",
+            "size": "md",
+            "url": "https://ppt.cc/feXZPx@.png",
+            "offsetTop": "xs"
+          },
+          {
+            "type": "text",
+            "text": "AI 主播影音播報影片",
+            "offsetStart": "md",
+            "color": "#e47e4d",
+            "weight": "bold"
+          }
+        ]
+      },
+      {
+        "type": "box",
+        "layout": "horizontal",
+        "margin": "lg",
+        "spacing": "sm",
+        "contents": [
+          {
+            "type": "text",
+            "text": "一次到位擁有,只要",
+            "margin": "none",
+            "size": "sm",
             "gravity": "center",
-            "margin": "lg"
+            "color": "#2384e3",
+            "weight": "bold"
+          },
+          {
+            "type": "text",
+            "text": "$36,000",
+            "size": "xxl",
+            "offsetStart": "5px",
+            "weight": "bold",
+            "color": "#2384e3"
+          }
+        ],
+        "backgroundColor": "#fff3ed",
+        "background": {
+          "type": "linearGradient",
+          "angle": "0deg",
+          "startColor": "#fff3ed",
+          "endColor": "#ffffff"
+        }
+      },
+      {
+        "type": "text",
+        "text": "不論是公司宣傳/產品發表/品牌經營,都能透過新聞稿發布,提升曝光及知名度!",
+        "wrap": true,
+        "size": "13px",
+        "align": "start",
+        "offsetTop": "10px"
+      }
+    ]
+  },
+  "footer": {
+    "type": "box",
+    "layout": "vertical",
+    "spacing": "sm",
+    "contents": [
+      {
+        "type": "box",
+        "layout": "vertical",
+        "contents": [
+          {
+            "type": "button",
+            "style": "link",
+            "height": "sm",
+            "action": {
+              "type": "uri",
+              "label": "請來採訪我",
+              "uri": "https://page.line.me/choozmo"
+            },
+            "color": "#FFFFFF"
+          }
+        ],
+        "backgroundColor": "#e47e4d",
+        "offsetTop": "-3px"
+      },
+      {
+        "type": "box",
+        "layout": "vertical",
+        "contents": [
+          {
+            "type": "button",
+            "style": "link",
+            "height": "sm",
+            "action": {
+              "type": "uri",
+              "label": "了解更多",
+              "uri": "https://ai.choozmo.com/"
+            },
+            "color": "#FFFFFF"
+          }
+        ],
+        "backgroundColor": "#06C152"
+      },
+      {
+        "type": "box",
+        "layout": "horizontal",
+        "contents": [
+          {
+            "type": "image",
+            "url": "https://ppt.cc/fHYTTx@.png",
+            "offsetStart": "35px",
+            "size": "xl",
+            "position": "absolute"
+          },
+          {
+            "type": "text",
+            "text": "技術提供",
+            "offsetBottom": "15px",
+            "color": "#8B8B8B",
+            "size": "sm",
+            "weight": "bold",
+            "offsetEnd": "40px",
+            "position": "absolute"
           }
         ],
-        "backgroundColor": "#FF6B6E",
-        "paddingTop": "19px",
-        "paddingAll": "12px",
-        "paddingBottom": "16px"
+        "action": {
+          "type": "uri",
+          "label": "action",
+          "uri": "https://page.line.me/choozmo"
+        },
+        "width": "100%",
+        "height": "50px",
+        "alignItems": "center",
+        "offsetTop": "5px"
+      }
+    ],
+    "flex": 0,
+    "height": "160px"
+  }
+}
+
+"""
+
+bubble="""
+ {
+      "type": "bubble",
+      "size": "micro",
+      "hero": {
+        "type": "image",
+        "url": "https://scdn.line-apps.com/n/channel_devcenter/img/flexsnapshot/clip/clip10.jpg",
+        "size": "full",
+        "aspectMode": "cover",
+        "aspectRatio": "320:213"
       },
       "body": {
         "type": "box",
         "layout": "vertical",
         "contents": [
+          {
+            "type": "text",
+            "text": "{{title }}",
+            "weight": "bold",
+            "size": "sm",
+            "wrap": true
+          },
           {
             "type": "box",
-            "layout": "horizontal",
+            "layout": "vertical",
             "contents": [
               {
-                "type": "text",
-                "text": "Wash my car",
-                "color": "#8C8C8C",
-                "size": "sm",
-                "wrap": true
+                "type": "box",
+                "layout": "baseline",
+                "spacing": "sm",
+                "contents": [
+                  {
+                    "type": "text",
+                    "text": "ChoozMo商情AI",
+                    "wrap": true,
+                    "color": "#8c8c8c",
+                    "size": "xs",
+                    "flex": 5
+                  }
+                ]
               }
-            ],
-            "flex": 1
+            ]
+      }]
+}
+}
+"""
+
+bubble2="""
+{
+  "type": "bubble",
+  "header": {
+    "type": "box",
+    "layout": "horizontal",
+    "position": "relative",
+    "backgroundColor": "#FFFFFFFF",
+    "contents": [
+      {
+        "type": "box",
+        "layout": "vertical",
+        "contents": [
+          {
+            "type": "image",
+            "url": "https://i.imgur.com/wQbJeGc.png",
+            "margin": "none",
+            "align": "end",
+            "gravity": "top",
+            "size": "md",
+            "aspectRatio": "16:5",
+            "action": {
+              "type": "uri",
+              "label": "ChoozMo",
+              "uri": "https://cmm.ai/line"
+            }
+          },
+          {
+            "type": "text",
+            "text": "今日熱搜",
+            "weight": "bold",
+            "color": "#BEB7B7FF",
+            "position": "absolute",
+            "offsetTop": "5px",
+            "contents": []
           }
-        ],
-        "spacing": "md",
-        "paddingAll": "12px"
-      },
-      "styles": {
-        "footer": {
-          "separator": false
-        }
+        ]
       }
-    },
-    {
-      "type": "bubble",
-      "size": "nano",
-      "header": {
+    ]
+  },
+  "hero": {
+    "type": "image",
+    "url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/01_4_news.png",
+    "size": "full",
+    "aspectRatio": "20:13",
+    "aspectMode": "cover",
+    "action": {
+      "type": "uri",
+      "label": "Action",
+      "uri": "https://linecorp.com/"
+    }
+  },
+  "body": {
+    "type": "box",
+    "layout": "horizontal",
+    "spacing": "md",
+    "contents": [
+      {
+        "type": "spacer"
+      },
+      {
         "type": "box",
         "layout": "vertical",
         "contents": [
           {
             "type": "text",
-            "text": "In Progress",
-            "color": "#ffffff",
-            "align": "start",
-            "size": "md",
-            "gravity": "center"
+            "text": "A1",
+            "size": "xs",
+            "flex": 1,
+            "gravity": "top",
+            "contents": []
           },
           {
             "type": "text",
-            "text": "100%",
-            "color": "#ffffff",
-            "align": "start",
+            "text": "A2",
+            "size": "xs",
+            "flex": 2,
+            "gravity": "center",
+            "contents": []
+          },
+          {
+            "type": "text",
+            "text": "A3",
             "size": "xs",
+            "flex": 2,
             "gravity": "center",
-            "margin": "lg"
+            "contents": []
+          },
+          {
+            "type": "text",
+            "text": "A4",
+            "size": "xs",
+            "flex": 1,
+            "gravity": "bottom",
+            "contents": []
+          },
+          {
+            "type": "spacer"
           }
-        ],
-        "backgroundColor": "#A17DF5",
-        "paddingTop": "19px",
-        "paddingAll": "12px",
-        "paddingBottom": "16px"
-      },
-      "body": {
+        ]
+      }
+    ]
+  },
+  "footer": {
+    "type": "box",
+    "layout": "horizontal",
+    "contents": [
+      {
+        "type": "button",
+        "action": {
+          "type": "uri",
+          "label": "More",
+          "uri": "https://cmm.ai/line"
+        }
+      }
+    ]
+  }
+}
+"""
+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": "horizontal",
+            "layout": "vertical",
+            "position": "absolute",
+            "offsetBottom": "0px",
+            "width": "100%",
+            "height": "90px",
+            "backgroundColor": "#9C8E7ECC",
             "contents": [
               {
                 "type": "text",
-                "text": "Buy milk and lettuce before class",
-                "color": "#8C8C8C",
-                "size": "sm",
-                "wrap": true
+                "text": "{{ imgtitle }}",
+                "color": "#FFFFFFFF",
+                "align": "start",
+                "wrap": true,
+                "contents": []
               }
-            ],
-            "flex": 1
+            ]
           }
-        ],
-        "spacing": "md",
-        "paddingAll": "12px"
+        ]
       },
-      "styles": {
-        "footer": {
-          "separator": false
-        }
+      {
+        "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
+#from opengraph import OpenGraph
+
+
+
+db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/gtrends?charset=utf8mb4')
+
+cursor=db.query('SELECT distinct qtitle FROM gtrends.trending_search_flat order by dt desc limit 5')
+qtitles=[]
+for c in cursor:
+  qtitles.append(c['qtitle'])
+
+
+
+result=[]
+
+for qt in qtitles:
+  cursor=db.query('SELECT distinct trending_search_flat.qtitle,trending_search_flat.atitle,trending_search_flat.aimg,trending_search_flat.aurl,kw_related.original,kw_related.kw FROM gtrends.trending_search_flat,kw_related where trending_search_flat.qtitle=kw_related.original and  trending_search_flat.qtitle="'+qt+'" order by dt desc,qtitle desc;')
+  title=None
+  imgurl=None
+  imgtitle=qlink=None
+  k1=k2=k3=k4=None
+  lst_dict={}
+  for c in cursor:
+    title=c['qtitle']
+    qlink=c['aurl']
+
+    imgtitle=c['atitle']
+#    imgurl=c['aimg']
+    print(qlink)
+    if imgurl is None:
+      try:
+        video = opengraph.OpenGraph(url=qlink)
+        if video.is_valid():
+          if video['image'] is not None:
+            imgurl=video['image']
+            print(imgurl)
+      except:
+        imgurl=None
+
+    lst_dict[c['kw']]=1
+    if len(lst_dict)>=4:
+      break
+  if imgurl is None:
+    print('none')
+    imgurl=c['aimg']
+  print(lst_dict)
+  lst=list(lst_dict.keys())
+  k1=lst[0]
+  k2=lst[1]
+  if len(lst)>=3:
+    k3=lst[2]
+  else:
+    k3=' '
+  if len(lst)>=4:
+    k4=lst[3]
+  else:
+    k4=' '
+
+  result.append({'title':title,'imgurl':imgurl,'qlink':qlink,'imgtitle':imgtitle,'K1':k1,'K2':k2,'K3':k3,'K4':k4})
+
+result_lst=result
+print(result)
+
 r = redis.Redis(host='db.ptt.cx', port=6379, db=2,password='choozmo9')
-r.set('btn_data',data)
+
+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('btn_data',full_result)
+#r.set('btn_data',data2)
+sys.exit()