main.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from fastapi import FastAPI, File, UploadFile
  2. from fastapi.middleware.cors import CORSMiddleware
  3. import dataset
  4. import zipfile
  5. import pandas as pd
  6. import glob
  7. app = FastAPI()
  8. app.add_middleware(
  9. CORSMiddleware,
  10. allow_origins=['*'],
  11. allow_credentials=True,
  12. allow_methods=["*"],
  13. allow_headers=["*"],
  14. )
  15. @app.post("/upload/")
  16. async def upload( title:str, file: UploadFile = File(...)):
  17. file = file.file
  18. target = title
  19. with zipfile.ZipFile(file, "r") as zip_ref:
  20. zip_ref.extractall(target)
  21. await write(target, title)
  22. @app.post("/write")
  23. async def write(dir, title):
  24. path = dir
  25. all_files = glob.glob(path + "/*.csv")
  26. li = []
  27. for filename in all_files:
  28. df = pd.read_csv(filename, index_col=None, header=0)
  29. li.append(df)
  30. # pre-clean data
  31. frame = pd.concat(li, axis=0, ignore_index=True)
  32. data_name = "./"+dir+"/"+title+".csv"
  33. data = frame.to_csv(data_name)
  34. await todb(data_name, title)
  35. print("data", data_name)
  36. @app.post("/todb")
  37. async def todb(file, title):
  38. # read csv
  39. filename = title
  40. data = file
  41. df = pd.read_csv(data, sep=",",
  42. skiprows=0, na_values='NULL')
  43. # db connect
  44. db = dataset.connect(
  45. 'mysql://root:pAssw0rd@localhost:3306/keywordweb?charset=utf8mb4'
  46. )
  47. sql = "CREATE TABLE IF NOT EXISTS " \
  48. + filename + \
  49. "(ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, " \
  50. "keyword VARCHAR(100), " \
  51. "currency VARCHAR(100), avg_monthly_searches VARCHAR(100), " \
  52. "three_month_change VARCHAR(100), " \
  53. "yearly_change VARCHAR(100), competition VARCHAR(100), " \
  54. "competition_indexed VARCHAR(100), " \
  55. "top_page_bid_low_range VARCHAR(100), " \
  56. "top_page_bid_hi_range VARCHAR(100)" \
  57. ");"
  58. db.query(sql)
  59. # write to db
  60. table = db[filename]
  61. lines = df.shape
  62. rows = lines[0]
  63. i = 0
  64. for i in range(rows):
  65. row1 = df.iloc[i]
  66. k = row1[0]
  67. c = row1[1]
  68. a = row1[2]
  69. t = row1[3]
  70. y = row1[4]
  71. c1 = row1[5]
  72. c2 = row1[6]
  73. t1 = row1[7]
  74. t2 = row1[8]
  75. dbdata = dict(keyword=k, currency=c, avg_monthly_searches=a,
  76. three_month_change=t, yearly_change=y, competition=c1,
  77. competition_indexed=c2, top_page_bid_low_range=t1,
  78. top_page_bid_hi_range=t2)
  79. table.insert(dbdata)
  80. db.close()