Omkar008 commited on
Commit
0db13ac
1 Parent(s): da64600

Update controllers/ws_controller.py

Browse files
Files changed (1) hide show
  1. controllers/ws_controller.py +45 -25
controllers/ws_controller.py CHANGED
@@ -10,12 +10,12 @@ from models.models import Message, Attachment
10
  from fastapi import WebSocket
11
  from services import utils as ut
12
 
13
- def get_company_type(company_name:str)->str:
14
  company_types_dict ={'ao yun': 'wines and spirit', 'ardbeg': 'wines and spirit', 'belvedere': 'wines and spirit', 'bodega numanthia': 'wines and spirit', 'chandon': 'wines and spirit', 'château cheval blanc': 'wines and spirit', "château d'yquem": 'wines and spirit', 'château galoupet': 'wines and spirit', 'cheval des andes': 'wines and spirit', 'clos19': 'wines and spirit', 'cloudy bay': 'wines and spirit', 'colgin cellars': 'wines and spirit', 'dom pérignon': 'wines and spirit', 'domaine des lambrays': 'wines and spirit', 'eminente': 'wines and spirit', 'glenmorangie': 'wines and spirit', 'hennessy': 'wines and spirit', 'joseph phelps': 'wines and spirit', 'krug': 'wines and spirit', 'mercier': 'wines and spirit', 'moët & chandon': 'wines and spirit', 'newton vineyard': 'wines and spirit', 'ruinart': 'wines and spirit', 'terrazas de los andes': 'wines and spirit', 'veuve clicquot': 'wines and spirit', 'volcan de mi tierra': 'wines and spirit', 'woodinville': 'wines and spirit' , 'berluti': 'Fashion & Leather Goods', 'celine': 'Fashion & Leather Goods', 'christian dior': 'Fashion & Leather Goods', 'emilio pucci': 'Fashion & Leather Goods', 'fendi': 'Fashion & Leather Goods', 'givenchy': 'Fashion & Leather Goods', 'kenzo': 'Fashion & Leather Goods', 'loewe': 'Fashion & Leather Goods', 'loro piana': 'Fashion & Leather Goods', 'louis vuitton': 'Fashion & Leather Goods', 'marc jacobs': 'Fashion & Leather Goods', 'moynat': 'Fashion & Leather Goods', 'patou': 'Fashion & Leather Goods', 'rimowa': 'Fashion & Leather Goods','acqua di parma': 'Perfumes & Cosmetics', 'benefit cosmetics': 'Perfumes & Cosmetics', 'cha ling': 'Perfumes & Cosmetics', 'fenty beauty by rihanna': 'Perfumes & Cosmetics', 'fresh': 'Perfumes & Cosmetics', 'givenchy parfums': 'Perfumes & Cosmetics', 'guerlain': 'Perfumes & Cosmetics', 'kenzo parfums': 'Perfumes & Cosmetics', 'kvd beauty': 'Perfumes & Cosmetics', 'loewe perfumes': 'Perfumes & Cosmetics', 'maison francis kurkdjian': 'Perfumes & Cosmetics', 'make up for ever': 'Perfumes & Cosmetics', 'officine universelle buly': 'Perfumes & Cosmetics', 'olehenriksen': 'Perfumes & Cosmetics', 'parfums christian dior': 'Perfumes & Cosmetics', 'stella by stella mccartney': 'Perfumes & Cosmetics','bulgari': 'Watches & Jewelry', 'chaumet': 'Watches & Jewelry', 'fred': 'Watches & Jewelry', 'hublot': 'Watches & Jewelry', 'repossi': 'Watches & Jewelry', 'tag heuer': 'Watches & Jewelry', 'tiffany & co.': 'Watches & Jewelry', 'zenith': 'Watches & Jewelry','24s': 'Selective retailing', 'dfs': 'Selective retailing', 'la grande epicerie de paris': 'Selective retailing', 'le bon marché rive gauche': 'Selective retailing', 'sephora': 'Selective retailing','belmond': 'Other activities', 'cheval blanc': 'Other activities', 'connaissance des arts': 'Other activities', 'cova': 'Other activities', 'investir': 'Other activities', "jardin d'acclimatation": 'Other activities', 'le parisien': 'Other activities', 'les echos': 'Other activities', 'radio classique': 'Other activities', 'royal van lent': 'Other activities'}
15
  print(company_types_dict["louis vuitton"])
16
  return company_types_dict.get(company_name.lower(), 'Others')
17
 
18
- def get_messages(code: str,brand_name: Optional[str] = None) -> List[Message]:
19
  access_token = code
20
  g_query = f'(subject:"your order" OR subject:receipts OR subject:receipt OR subject:aankoopbon OR subject:reçu OR subject:invoice OR subject:invoices OR category:purchases) has:attachment'
21
  if brand_name is not None:
@@ -27,10 +27,20 @@ def get_messages(code: str,brand_name: Optional[str] = None) -> List[Message]:
27
  # gmail_response = requests.get(gmail_url, headers={"Authorization": f"Bearer {access_token}"})
28
  # gmail_data = gmail_response.json()
29
  # messages.append(gmail_data['messages'])
30
- def fetch_message_wrapper(message_data):
31
  message_id = message_data.get("id")
32
  if message_id:
33
- return fetch_message_data(access_token, message_id)
 
 
 
 
 
 
 
 
 
 
34
  return None
35
 
36
  while True:
@@ -45,12 +55,16 @@ def get_messages(code: str,brand_name: Optional[str] = None) -> List[Message]:
45
 
46
  if "messages" in gmail_data:
47
  with ThreadPoolExecutor(max_workers=15) as executor:
48
- futures = [executor.submit(fetch_message_wrapper, message_data) for message_data in
49
- gmail_data["messages"]]
50
- for future in futures:
51
- message = future.result()
52
- if message:
53
- messages.append(message)
 
 
 
 
54
 
55
  if "nextPageToken" in gmail_data:
56
  page_token = gmail_data["nextPageToken"]
@@ -60,6 +74,12 @@ def get_messages(code: str,brand_name: Optional[str] = None) -> List[Message]:
60
  print(messages)
61
  return messages
62
 
 
 
 
 
 
 
63
 
64
  def fetch_message_data(access_token: str, message_id: str) -> Message:
65
  message_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}"
@@ -204,22 +224,22 @@ def extract_text(html_content: str) -> str:
204
 
205
  async def websocket_main(code: str, websocket: WebSocket,brand_name: Optional[str] = None):
206
  access_token = code
207
- messages = get_messages(access_token,brand_name)
208
- print("websocket_main")
209
- print(messages)
210
- # logging.info(f"brand_name:{brand_name}")
211
- await websocket.send_json({"total_messages": len(messages)})
212
- print("Total Length of messages")
213
- print(len(messages))
214
- chunk_size = 100000
215
- i=0
216
- for message in messages:
217
- message_json = message.to_json()
218
 
219
- logging.info(f"{i} th message")
220
- i=i+1
221
- await send_message_in_chunks(websocket, message_json, chunk_size)
222
- await websocket.send_text("NEXT_MESSAGE")
223
 
224
 
225
  await websocket.close()
 
10
  from fastapi import WebSocket
11
  from services import utils as ut
12
 
13
+ def get_company_type(company_name:str,websocket:WebSocket)->str:
14
  company_types_dict ={'ao yun': 'wines and spirit', 'ardbeg': 'wines and spirit', 'belvedere': 'wines and spirit', 'bodega numanthia': 'wines and spirit', 'chandon': 'wines and spirit', 'château cheval blanc': 'wines and spirit', "château d'yquem": 'wines and spirit', 'château galoupet': 'wines and spirit', 'cheval des andes': 'wines and spirit', 'clos19': 'wines and spirit', 'cloudy bay': 'wines and spirit', 'colgin cellars': 'wines and spirit', 'dom pérignon': 'wines and spirit', 'domaine des lambrays': 'wines and spirit', 'eminente': 'wines and spirit', 'glenmorangie': 'wines and spirit', 'hennessy': 'wines and spirit', 'joseph phelps': 'wines and spirit', 'krug': 'wines and spirit', 'mercier': 'wines and spirit', 'moët & chandon': 'wines and spirit', 'newton vineyard': 'wines and spirit', 'ruinart': 'wines and spirit', 'terrazas de los andes': 'wines and spirit', 'veuve clicquot': 'wines and spirit', 'volcan de mi tierra': 'wines and spirit', 'woodinville': 'wines and spirit' , 'berluti': 'Fashion & Leather Goods', 'celine': 'Fashion & Leather Goods', 'christian dior': 'Fashion & Leather Goods', 'emilio pucci': 'Fashion & Leather Goods', 'fendi': 'Fashion & Leather Goods', 'givenchy': 'Fashion & Leather Goods', 'kenzo': 'Fashion & Leather Goods', 'loewe': 'Fashion & Leather Goods', 'loro piana': 'Fashion & Leather Goods', 'louis vuitton': 'Fashion & Leather Goods', 'marc jacobs': 'Fashion & Leather Goods', 'moynat': 'Fashion & Leather Goods', 'patou': 'Fashion & Leather Goods', 'rimowa': 'Fashion & Leather Goods','acqua di parma': 'Perfumes & Cosmetics', 'benefit cosmetics': 'Perfumes & Cosmetics', 'cha ling': 'Perfumes & Cosmetics', 'fenty beauty by rihanna': 'Perfumes & Cosmetics', 'fresh': 'Perfumes & Cosmetics', 'givenchy parfums': 'Perfumes & Cosmetics', 'guerlain': 'Perfumes & Cosmetics', 'kenzo parfums': 'Perfumes & Cosmetics', 'kvd beauty': 'Perfumes & Cosmetics', 'loewe perfumes': 'Perfumes & Cosmetics', 'maison francis kurkdjian': 'Perfumes & Cosmetics', 'make up for ever': 'Perfumes & Cosmetics', 'officine universelle buly': 'Perfumes & Cosmetics', 'olehenriksen': 'Perfumes & Cosmetics', 'parfums christian dior': 'Perfumes & Cosmetics', 'stella by stella mccartney': 'Perfumes & Cosmetics','bulgari': 'Watches & Jewelry', 'chaumet': 'Watches & Jewelry', 'fred': 'Watches & Jewelry', 'hublot': 'Watches & Jewelry', 'repossi': 'Watches & Jewelry', 'tag heuer': 'Watches & Jewelry', 'tiffany & co.': 'Watches & Jewelry', 'zenith': 'Watches & Jewelry','24s': 'Selective retailing', 'dfs': 'Selective retailing', 'la grande epicerie de paris': 'Selective retailing', 'le bon marché rive gauche': 'Selective retailing', 'sephora': 'Selective retailing','belmond': 'Other activities', 'cheval blanc': 'Other activities', 'connaissance des arts': 'Other activities', 'cova': 'Other activities', 'investir': 'Other activities', "jardin d'acclimatation": 'Other activities', 'le parisien': 'Other activities', 'les echos': 'Other activities', 'radio classique': 'Other activities', 'royal van lent': 'Other activities'}
15
  print(company_types_dict["louis vuitton"])
16
  return company_types_dict.get(company_name.lower(), 'Others')
17
 
18
+ def get_messages(code: str,brand_name: Optional[str] = None):
19
  access_token = code
20
  g_query = f'(subject:"your order" OR subject:receipts OR subject:receipt OR subject:aankoopbon OR subject:reçu OR subject:invoice OR subject:invoices OR category:purchases) has:attachment'
21
  if brand_name is not None:
 
27
  # gmail_response = requests.get(gmail_url, headers={"Authorization": f"Bearer {access_token}"})
28
  # gmail_data = gmail_response.json()
29
  # messages.append(gmail_data['messages'])
30
+ def fetch_message_wrapper(message_data,websocket:WebSocket):
31
  message_id = message_data.get("id")
32
  if message_id:
33
+ message=fetch_message_data(access_token, message_id)
34
+ loop = asyncio.new_event_loop()
35
+ asyncio.set_event_loop(loop)
36
+
37
+ # Run the asynchronous method within the event loop
38
+ result = loop.run_until_complete(process_message(message,websocket,10000))
39
+
40
+ # Close the event loop
41
+ loop.close()
42
+
43
+
44
  return None
45
 
46
  while True:
 
55
 
56
  if "messages" in gmail_data:
57
  with ThreadPoolExecutor(max_workers=15) as executor:
58
+ executor.submit(fetch_message_wrapper, message_data , websocket) for message_data in
59
+ gmail_data["messages"]
60
+
61
+ # futures=executor.submit(fetch_message_wrapper, message_data) for message_data in
62
+ # gmail_data["messages"]
63
+ # for future in futures:
64
+ # message = future.result()
65
+ # if message:
66
+ # messages.append(message)
67
+
68
 
69
  if "nextPageToken" in gmail_data:
70
  page_token = gmail_data["nextPageToken"]
 
74
  print(messages)
75
  return messages
76
 
77
+ async def process_message(message:Message, websocket:WebSocket, chunk_size:int):
78
+ if message:
79
+ message_json = message.to_json()
80
+ await send_message_in_chunks(websocket, message_json, chunk_size)
81
+ await websocket.send_text("NEXT_MESSAGE")
82
+
83
 
84
  def fetch_message_data(access_token: str, message_id: str) -> Message:
85
  message_url = f"https://www.googleapis.com/gmail/v1/users/me/messages/{message_id}"
 
224
 
225
  async def websocket_main(code: str, websocket: WebSocket,brand_name: Optional[str] = None):
226
  access_token = code
227
+ messages = get_messages(access_token,brand_name , websocket)
228
+ # print("websocket_main")
229
+ # print(messages)
230
+ # # logging.info(f"brand_name:{brand_name}")
231
+ # await websocket.send_json({"total_messages": len(messages)})
232
+ # print("Total Length of messages")
233
+ # print(len(messages))
234
+ # chunk_size = 100000
235
+ # i=0
236
+ # for message in messages:
237
+ # message_json = message.to_json()
238
 
239
+ # logging.info(f"{i} th message")
240
+ # i=i+1
241
+ # await send_message_in_chunks(websocket, message_json, chunk_size)
242
+ # await websocket.send_text("NEXT_MESSAGE")
243
 
244
 
245
  await websocket.close()