Arafath10 commited on
Commit
f38f2ab
·
verified ·
1 Parent(s): 697732f

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +75 -8
main.py CHANGED
@@ -1,19 +1,86 @@
1
  from fastapi import FastAPI, File, UploadFile
2
- from fastapi.responses import StreamingResponse
3
  import os
4
  import io
5
- temp = open("t.txt","w")
6
- temp.write("aaaaaaaaaaaaa")
7
- temp.close()
8
 
9
- temp = open("t.txt","r")
10
 
11
  app = FastAPI()
 
 
 
 
 
 
 
 
 
 
12
 
13
 
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- @app.get("/sample")
17
- def read_root():
18
- return {"message": str(temp.read())}
19
 
 
1
  from fastapi import FastAPI, File, UploadFile
2
+ from fastapi.responses import FileResponse
3
  import os
4
  import io
5
+ from fastapi.middleware.cors import CORSMiddleware
6
+ import requests
7
+ import pandas as pd
8
 
 
9
 
10
  app = FastAPI()
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"],
14
+ allow_credentials=True,
15
+ allow_methods=["*"],
16
+ allow_headers=["*"],
17
+ )
18
+ # Parameters
19
+ API_KEY = "an3vib2nh4-3R48tMWfBZg"
20
+ WEBSITE_COLUMN = "Website"
21
 
22
 
23
 
24
+ def get_company_data(api_key, domain):
25
+ response = requests.get(f"https://api.apollo.io/v1/organizations/enrich?api_key={api_key}&domain={domain}")
26
+ result = response.json()
27
+
28
+ if "organization" in result:
29
+ org = result["organization"]
30
+ return {
31
+ "domain": domain,
32
+ "alexa_ranking": org.get("alexa_ranking", "unknown"),
33
+ "annual_revenue": org.get("annual_revenue", "unknown"),
34
+ "country": org.get("country", "unknown"),
35
+ "estimated_num_employees": org.get("estimated_num_employees", "unknown"),
36
+ "industry": org.get("industry", "unknown"),
37
+ "keywords": org.get("keywords", "unknown"),
38
+ "linkedin_uid": org.get("linkedin_uid", "unknown")
39
+ }
40
+ else:
41
+ print(f"No data for {domain}")
42
+ return {
43
+ "domain": domain,
44
+ "alexa_ranking": "unknown",
45
+ "annual_revenue": "unknown",
46
+ "country": "unknown",
47
+ "estimated_num_employees": "unknown",
48
+ "industry": "unknown",
49
+ "keywords": "unknown",
50
+ "linkedin_uid": "unknown"
51
+ }
52
+
53
+ @app.get("/get_data_file")
54
+ def main(file: UploadFile = File(...)):
55
+ LEAD_LIST_PATH = file.filename
56
+ print(file.filename)
57
+ with open(file.filename, "wb") as file_object:
58
+ file_object.write(file.file.read())
59
+
60
+ def get_domain(url):
61
+ if "//" in url:
62
+ start = url.index("//") + 2
63
+ else:
64
+ start = 0
65
+ result = url[start:].strip("/")
66
+ return result
67
+
68
+ # Read the list of websites from the Excel file
69
+ data = pd.read_excel(LEAD_LIST_PATH)
70
+ websites = data[WEBSITE_COLUMN].drop_duplicates().apply(get_domain)
71
+
72
+ # Fetch company data for each website
73
+ company_data = []
74
+ for website in websites:
75
+ company_data.append(get_company_data(API_KEY, website))
76
+
77
+ OUTPUT_PATH = "CompanyData.xlsx"
78
+ # Create a DataFrame and save to Excel
79
+ df = pd.DataFrame(company_data)
80
+ df.to_excel(OUTPUT_PATH, index=False)
81
+
82
+ print("Company data has been successfully fetched and saved.")
83
+ return FileResponse(OUTPUT_PATH, media_type='application/octet-stream', filename=OUTPUT_PATH)
84
+
85
 
 
 
 
86