main.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from enum import Enum
  2. import time
  3. from datetime import datetime
  4. from typing import Optional
  5. from pydantic import BaseModel
  6. from fastapi import FastAPI, Query, UploadFile, File
  7. from fastapi import FastAPI
  8. from fastapi.middleware.cors import CORSMiddleware
  9. import json
  10. import urllib.request
  11. import dataset,json
  12. app = FastAPI()
  13. origins = [
  14. "http://172.105.205.52",
  15. "http://172.105.205.52:8001",
  16. ]
  17. app.add_middleware(
  18. CORSMiddleware,
  19. # allow_origins=origins,
  20. allow_origins=["*"],
  21. allow_credentials=True,
  22. allow_methods=["*"],
  23. allow_headers=["*"],
  24. )
  25. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/NFTBoard?charset=utf8mb4')
  26. class subreq(BaseModel):
  27. email:str
  28. @app.get("/showdata/{limit}")
  29. async def showdata(limit):
  30. url = 'https://api.coinmarketcap.com/data-api/v3/nft/collections?start=0&limit=' + limit
  31. r = urllib.request.urlopen(url)
  32. collections = json.loads(r.read())['data']['collections']
  33. reqs = []
  34. for req in collections:
  35. logo = 'https://s3.coinmarketcap.com/generated/nft/collections/' + req['slug'] + '.png';
  36. last_7_days = 'https://s3.coinmarketcap.com/generated/sparklines/nft/collection/web/30d/' + req['slug'] +'.png';
  37. reqs.append({'rank':req['rank'],'slug':req['slug'],'logo':logo,'website':req['website'],'name':req['name'],'assets':req['transferUniqueAssetsAT'],'collection_net_worth':req['netWorth'],'volume_7d':req['volume7d'],'sales_7d':req['sales7d'],'volume_all_time':req['volumeAT'],'sales_all_time':req['salesAT'],'last_7_days':last_7_days})
  38. return reqs
  39. @app.post("/add_sub")
  40. async def add_subclient_info(req:subreq):
  41. code = 'ok'
  42. sub_table = db['Subscribe']
  43. time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
  44. pk = sub_table.insert({'email':req.email,'timestamp':time_stamp})
  45. make_ci_df()
  46. return {'msg':code}
  47. @app.get("/get_sub_list")
  48. async def add_subclient_info():
  49. statement = 'SELECT * FROM Subscribe'
  50. result = []
  51. for row in db.query(statement):
  52. result += [{'id':row['id'],'email':row['email'],'timeStamp':row['timestamp']}]
  53. return result
  54. def make_ci_df():
  55. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/NFTBoard?charset=utf8mb4')
  56. statement = 'SELECT * FROM Subscribe ORDER BY timestamp DESC'
  57. #2021-05-23 15:57:43
  58. df = pd.DataFrame()
  59. for row in db.query(statement):
  60. date_format = "%Y-%M-%d %H:%M:%S"
  61. fdate = row['time_stamp'].strftime('%Y-%m-%d %H時 %M分')
  62. df = df.append({'serial id':row['id'],'e-mail':row['email'],'登記時間':row['timestamp']}, ignore_index=True)
  63. df = df[['serial id','e-mail','登記時間']]
  64. save_sheet(df,'email_rec','Sheet1')
  65. def save_sheet(df,filename,tabname,startpos='A1'):
  66. scope = ['https://spreadsheets.google.com/feeds',
  67. 'https://www.googleapis.com/auth/drive']
  68. credentials = ServiceAccountCredentials.from_json_keyfile_name('spread2.json', scope)
  69. gc = gspread.authorize(credentials)
  70. spread = Spread(filename,creds=credentials)
  71. spread.df_to_sheet(df, index=True, sheet=tabname, start=startpos, replace=True)