2 Commits 89fe394083 ... fa4e7d0de4

Author SHA1 Message Date
  aaron-1015 fa4e7d0de4 Merge branch 'master' of http://git.choozmo.com:3000/choozmo/NFTBoard 3 years ago
  aaron-1015 3621698b93 add ContactUs api 3 years ago
2 changed files with 71 additions and 3 deletions
  1. 12 0
      api/cred.json
  2. 59 3
      api/main.py

+ 12 - 0
api/cred.json

@@ -0,0 +1,12 @@
+{
+  "type": "service_account",
+  "project_id": "model-coral-322005",
+  "private_key_id": "1DbcgePUkCncNZ4zYa-k3b_RQEi0RrqxvuBSS81T_LEM",
+  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC+/yvd+HnOQ0C/\nvTeHPaFK9DjkeUpar9mCpVe0I+WJ8M/KZTUDp2NDkevvhqsGB1B9KvqxEgmCO6ol\nYvfPpONSDInIyK98XfBQrdctgTETu4lDzYGldbbeiCr6fpepd+V/BdE/joQovVPc\nvjoXIuDrJ9DozRo2wMag8RtgDLuQUFXSIivTizBwsA9xR2k37qKsHHIFF9p0Mc4I\nqrn4YjVE3nBOwc0bVV10M+jyzpYdYx4wNv0ad30gZyeIF7M5K16+UIwKEOgPXb0Y\nWG2Yw5o6/eiJtw/4Uuk9rtJL+Zj+VGeNZGSWusFKTAguEWIdq8EpvGh91lsJRlDV\n32h9s7zDAgMBAAECggEACS7dnhOHT7p+D3n6hH8bFS5+2cVXJNsVmyl1+QxMygAR\nDYsmLGHM7P/EF0UjTevmG86INSFH4cIaoSeMAKjN4TEHIuc7QMCHgGmLUf3HLUq0\nA9WKqTjqrPJhFqAljJleKGhQMj0Y75iCwrdGtbwQkl3ctoS+AOnt4ukUxt512i9d\nUn+wK1y/XWMpXjVI3wQweGAQSxO4x9jyRUKvrm7+7g95FV5nBLNvkxUhoMZRxhcz\n2IJApPAOVmI7/k9ZMZJ8sQEyPyXIjkxHnVH7c34B99flcyB/uuHSDpQRC52tAyLK\nXEOkbOAzoTGH51q2wf8Eam0w+9jOlETylKzMhS48yQKBgQDrrAHqDLeepg/P4b+J\nRrsEyHruauULsuV4CY3Izrkt1jkpNWF0OW67Ee1m3QoZbploUWYE8J85n+o+AL02\n2Dx09+9+TEeshOcJxwhiV3MpJ2k/6zyC+oJL84Yc281PvxLI1+Es5uxB2HM/wY1F\n0laegyYeaJ9BooMEkc+T9d39NwKBgQDPeKtK3zTXPoS59yUoJMlkKPhPRQ2wz56L\nmc4j2WrZCYuXyY4jK/Vl+jnGvm83rFo/rwlr67eplwb/GG10v4b7CjoeCyDBqw5P\nGE/mabyKRjTt6xiVAPBN9oyByvH7rKB5AeG6xcXYSylefbCKun4hUJTf+Zv4P2Xh\nIriO5/di1QKBgQCX1LNPBG5v6XCab0gkusrWJAr++yMhTFoF4bHUw0oOO8uLvvZQ\nGIg8QSp/cLyG8fgnevmNCTCrwVj6JSPB58HbYbiL17z9aoomhHHkWY653sgyA+1y\nPM4g/H1Urk8xp1oExwAtG4DuUvK9CodnikF/0eKHCMRBVt+t2/qtymMANQKBgQC6\niIihgXt1xtz4gk9i/2geaIOQacp8XL70Tq27J6BcUKu0JeBomP5C2K4XlWJTUN+3\ngD1dTYpMrN/6fND0dbr7fF9yYZjq50uvK3Gy/QAp5iNygbwRQ83KmtALZhcYq0WA\nZoO4NFbEOBeSwDj5EeGTPGsN0NM9aGrF9zyuJ7sRnQKBgQDVk3CLLxWYAWr9s2ZM\n48hIfh2RpzTDyx1VkymP7hbcNDZmHwhUv0eu+L/jxSIQCOtncGv7rupfMlTQrTbG\nEKVgMW1H/a1tmBXhOKFekPYDazg8GKuxIV63OWJkoT+d6ZWmLvnXhovx/04KimE8\nJkvIp0V/JKDbbLkgmMF3V72+1A==\n-----END PRIVATE KEY-----\n",
+  "client_email": "id-23456@model-coral-322005.iam.gserviceaccount.com",
+  "client_id": "101565450616622846759",
+  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
+  "token_uri": "https://oauth2.googleapis.com/token",
+  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
+  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/id-23456%40model-coral-322005.iam.gserviceaccount.com"
+}

+ 59 - 3
api/main.py

@@ -34,6 +34,18 @@ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/NFTBoard?charset=u
 class subreq(BaseModel):
     email:str
 
+class ContactUs(BaseModel):
+    country_code: str
+    username: str
+    email: str
+    phone: str
+    coop_dif: Optional[bool] = 0
+    coop_ad: Optional[bool] = 0
+    coop_acting: Optional[bool] = 0
+    coop_marketing: Optional[bool] = 0
+    work_address: str
+
+
 @app.get("/showdata/{limit}")
 async def showdata(limit):
     url = 'https://api.coinmarketcap.com/data-api/v3/nft/collections?start=0&limit=' + limit
@@ -62,6 +74,37 @@ async def add_subclient_info(req:subreq):
         code = 0
     return {'msg':code}#0 means succss added, 1 means email is duplicated
 
+
+@app.post('/add_contact_us')
+async def add_contact_us_client_info(req: ContactUs):
+    """ 新增Contact Us資料,先存入DB,並從DB撈回全部資料,更新Google Sheet. """
+    code = 1
+    table = db['ContactUs']
+    time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
+
+    statement = 'SELECT COUNT(1) FROM ContactUs WHERE email = "'+ req.email +'"'
+    if first(db.query(statement))['COUNT(1)'] == 0:
+        table.insert(
+            {
+                'country_code':req.country_code,
+                'username': req.username,
+                'email': req.email,
+                'phone': req.phone,
+                'coop_dif': req.coop_dif,
+                'coop_ad': req.coop_ad,
+                'coop_acting': req.coop_acting,
+                'coop_marketing': req.coop_marketing,
+                'work_address': req.work_address,
+                'timestamp': time_stamp
+            }
+        )
+        code = 0
+        # 更新sheet
+        make_contact_us_df()
+    return {'msg':code}#0 means succss added, 1 means email is duplicated
+
+
+
 @app.get("/get_sub_list")
 async def add_subclient_info():
     statement = 'SELECT * FROM Subscribe'
@@ -85,10 +128,23 @@ def make_ci_df():
     df = df[['serial id','e-mail','登記時間']]
     save_sheet(df,'subscribe_rec','Sheet1')
 
-def save_sheet(df,filename,tabname,startpos='A1'):
+
+def make_contact_us_df():
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/NFTBoard?charset=utf8mb4')
+    statement = 'SELECT * FROM ContactUs ORDER BY timestamp DESC'
+    df = pd.DataFrame()
+    for row in db.query(statement):
+        
+        df = df.append({'id':  row['id'], '國家區域': row['country_code'], '姓名': row['username'], '聯絡Email': row['email'], '聯絡手機': row['phone'],  '合作方式_異業合作': row['coop_dif'], '合作方式_廣告刊登': row['coop_ad'], '合作方式_代理發行':  row['coop_acting'], '合作方式_知識產權行銷': row['coop_marketing'], '作品連結': row['work_address'], '申請時間': row['timestamp']}, ignore_index=True)
+    df = df[['id', '國家區域', '姓名', '聯絡Email', '聯絡手機', '合作方式_異業合作', '合作方式_廣告刊登', '合作方式_代理發行', '合作方式_知識產權行銷', '作品連結', '申請時間']]
+    print(df)
+    save_sheet(df, '聯絡NFTBoard (回應)', 'Sheet1',  json_name='cred.json')
+
+
+def save_sheet(df,filename,tabname,startpos='A1', json_name='spread2.json'):
     scope = ['https://spreadsheets.google.com/feeds',
             'https://www.googleapis.com/auth/drive']
-    credentials = ServiceAccountCredentials.from_json_keyfile_name('spread2.json', scope)
+    credentials = ServiceAccountCredentials.from_json_keyfile_name(json_name, scope)
     gc = gspread.authorize(credentials)
     spread = Spread(filename,creds=credentials)
-    spread.df_to_sheet(df, sheet=tabname, start=startpos, replace=True)
+    spread.df_to_sheet(df, sheet=tabname, start=startpos, replace=True, index=False)