hardik27 commited on
Commit
4297bea
·
verified ·
1 Parent(s): a771fd3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -44
app.py CHANGED
@@ -10,39 +10,53 @@ from google.auth.transport.requests import Request
10
  from google_auth_oauthlib.flow import InstalledAppFlow
11
  from googleapiclient.discovery import build
12
  from googleapiclient.http import MediaIoBaseDownload,MediaFileUpload
13
-
14
- # Load credentials from environment variables
15
- config = {'installed': {'client_id': os.environ.get("client_id"),
16
- 'project_id': os.environ.get("project_id"),
17
- 'auth_uri': os.environ.get("auth_uri"),
18
- 'token_uri': os.environ.get("token_uri"),
19
- 'auth_provider_x509_cert_url': os.environ.get("auth_provider_x509_cert_url"),
20
- 'client_secret': os.environ.get("client_secret"),
21
- 'redirect_uris': ['http://localhost']}}
22
 
23
  SCOPES = ['https://www.googleapis.com/auth/drive']
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  def authenticate():
26
- creds = None
 
 
 
 
 
 
 
 
27
 
28
  # Check if token file exists
29
- if os.path.exists('token.json'):
30
- creds = Credentials.from_authorized_user_file('token.json')
31
 
32
  # If no valid credentials available, ask the user to login
33
- if not creds or not creds.valid:
34
- if creds and creds.expired and creds.refresh_token:
35
- creds.refresh(Request())
36
- else:
37
- # flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
38
- flow = InstalledAppFlow.from_client_config(config,SCOPES)
39
- creds = flow.run_local_server(port=0)
40
 
41
- # Save the credentials for next run
42
- with open('token.json', 'w') as token:
43
- token.write(creds.to_json())
44
 
45
- return creds
46
 
47
  MAPPING_FILENAME = "Data Mapping with ItemCode.xlsx"
48
 
@@ -134,13 +148,17 @@ def save_mapping_file_to_drive():
134
  creds = authenticate()
135
  service = build('drive', 'v3', credentials=creds)
136
  # Authenticate with Google Drive API
137
- service = build('drive', 'v3', credentials=creds)
138
- folder_id = "1HBRUZePST0D0buyU9MxeYg2vQyEL4wLF"
139
 
140
  # List all files in the folder
 
 
 
 
141
  results = service.files().list(
142
- q=f"'{folder_id}' in parents and mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'",
143
  fields="files(id, name)").execute()
 
144
  files = results.get('files', [])
145
  files = [i for i in files if i.get('name')==MAPPING_FILENAME]
146
 
@@ -155,13 +173,13 @@ def save_mapping_file_to_drive():
155
  # Delete the existing file
156
  service.files().delete(fileId=existing_file_id).execute()
157
 
158
- file_metadata = {'name': MAPPING_FILENAME, 'parents': [folder_id]}
159
  media = MediaFileUpload(MAPPING_FILENAME, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
160
  service.files().create(body=file_metadata, media_body=media, fields='id').execute()
161
 
162
  def pull_mapping_file_from_drive():
163
  creds = authenticate()
164
- service = build('drive', 'v3', credentials=creds)
165
  # creds = Credentials.from_authorized_user_info(credentials_dict)
166
  # Authenticate with Google Drive API
167
  service = build('drive', 'v3', credentials=creds)
@@ -202,9 +220,11 @@ def main():
202
  extracted_file,data_for_mapping = convert_pdf_to_excel(uploaded_file)
203
 
204
  file_present = pull_mapping_file_from_drive()
 
205
 
206
  if file_present:
207
  mapping_data_from_drive = pd.read_excel(MAPPING_FILENAME)
 
208
  extracted_data_for_mapping = pd.read_excel(data_for_mapping)
209
  extracted_data_for_mapping = extracted_data_for_mapping.merge(mapping_data_from_drive, on = ['Customer Part no as per pdf','Customer Part name as per pdf','Customer Part color as per pdf'], how='outer')
210
  extracted_data_for_mapping.to_excel(data_for_mapping,index=False)
@@ -228,22 +248,21 @@ def main():
228
  st.markdown("## Upload the Data Master file with Item Code mapping")
229
  mapping_uploaded_file = st.file_uploader("Upload the Data Master file with Item Code mapping", type=["xlsx","ods"])
230
  else:
231
- try:
232
- mapping_data = pd.read_excel(MAPPING_FILENAME)
233
- # mapping_data = mapping_data.rename(columns = {'Customer Part no as per pdf':'Part No.'})
234
- data_for_mapping = "Data Mapping.xlsx"
235
- extracted_data_for_mapping = pd.read_excel(data_for_mapping)
236
- extracted_data_for_mapping = extracted_data_for_mapping[~extracted_data_for_mapping['Customer Part no as per pdf'].isin(mapping_data['Customer Part no as per pdf'])]
237
- unmapped_part_no = extracted_data_for_mapping['Customer Part no as per pdf'].nunique()
238
- if unmapped_part_no>0:
239
- st.markdown("#### There are {} Part No. with No ItemCode present. Upload a new file after mapping them".format(unmapped_part_no))
240
- mapping_uploaded_file = st.file_uploader("Upload the Data Master file with Item Code mapping", type=["xlsx","ods"])
241
- else:
242
- st.markdown("#### Using the Mapping file available in Google Drive")
243
- mapping_uploaded_file = MAPPING_FILENAME
244
- except:
245
- st.markdown("#### Using the Mapping file available in Google Drive")
246
- mapping_uploaded_file = MAPPING_FILENAME
247
 
248
  if mapping_uploaded_file is not None:
249
  # st.write("Uploaded Mapping Excel file:", mapping_uploaded_file.name)
 
10
  from google_auth_oauthlib.flow import InstalledAppFlow
11
  from googleapiclient.discovery import build
12
  from googleapiclient.http import MediaIoBaseDownload,MediaFileUpload
13
+ from google.oauth2 import service_account
 
 
 
 
 
 
 
 
14
 
15
  SCOPES = ['https://www.googleapis.com/auth/drive']
16
 
17
+ # Load credentials from environment variables
18
+ SERVICE_ACCOUNT_INFO = {
19
+ "type": "service_account",
20
+ "project_id": os.environ.get("project_id"),
21
+ "private_key_id": os.environ.get("private_key_id"),
22
+ "private_key": os.environ.get("private_key"),
23
+ "client_email": os.environ.get("client_email"),
24
+ "client_id": os.environ.get("client_id"),
25
+ "auth_uri": "https://accounts.google.com/o/oauth2/auth",
26
+ "token_uri": os.environ.get("token_uri"),
27
+ "auth_provider_x509_cert_url": os.environ.get("auth_provider_x509_cert_url"),
28
+ "client_x509_cert_url": os.environ.get("client_x509_cert_url"),
29
+ "universe_domain": "googleapis.com"
30
+ }
31
+
32
  def authenticate():
33
+ # Authenticate using service account credentials
34
+ creds = service_account.Credentials.from_service_account_info(SERVICE_ACCOUNT_INFO, scopes=['https://www.googleapis.com/auth/drive'])
35
+
36
+ # Build the Drive API service
37
+ # service = build('drive', 'v3', credentials=creds)
38
+ return creds
39
+
40
+ # creds = service_account.Credentials.from_service_account_file(
41
+ # SERVICE_ACCOUNT_FILE, scopes=['https://www.googleapis.com/auth/drive'])
42
 
43
  # Check if token file exists
44
+ # if os.path.exists('token.json'):
45
+ # creds = Credentials.from_authorized_user_file('token.json')
46
 
47
  # If no valid credentials available, ask the user to login
48
+ # if not creds or not creds.valid:
49
+ # if creds and creds.expired and creds.refresh_token:
50
+ # creds.refresh(Request())
51
+ # else:
52
+ # # flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
53
+ # flow = InstalledAppFlow.from_client_config(config,SCOPES)
54
+ # creds = flow.run_local_server(port=0)
55
 
56
+ # # Save the credentials for next run
57
+ # with open('token.json', 'w') as token:
58
+ # token.write(creds.to_json())
59
 
 
60
 
61
  MAPPING_FILENAME = "Data Mapping with ItemCode.xlsx"
62
 
 
148
  creds = authenticate()
149
  service = build('drive', 'v3', credentials=creds)
150
  # Authenticate with Google Drive API
151
+ # service = build('drive', 'v3', credentials=creds)
 
152
 
153
  # List all files in the folder
154
+ # results = service.files().list(
155
+ # q=f"'{folder_id}' in parents and mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'",
156
+ # fields="files(id, name)").execute()
157
+
158
  results = service.files().list(
159
+ q=f"mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'",
160
  fields="files(id, name)").execute()
161
+
162
  files = results.get('files', [])
163
  files = [i for i in files if i.get('name')==MAPPING_FILENAME]
164
 
 
173
  # Delete the existing file
174
  service.files().delete(fileId=existing_file_id).execute()
175
 
176
+ file_metadata = {'name': MAPPING_FILENAME }
177
  media = MediaFileUpload(MAPPING_FILENAME, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
178
  service.files().create(body=file_metadata, media_body=media, fields='id').execute()
179
 
180
  def pull_mapping_file_from_drive():
181
  creds = authenticate()
182
+ # service = build('drive', 'v3', credentials=creds)
183
  # creds = Credentials.from_authorized_user_info(credentials_dict)
184
  # Authenticate with Google Drive API
185
  service = build('drive', 'v3', credentials=creds)
 
220
  extracted_file,data_for_mapping = convert_pdf_to_excel(uploaded_file)
221
 
222
  file_present = pull_mapping_file_from_drive()
223
+ print(file_present)
224
 
225
  if file_present:
226
  mapping_data_from_drive = pd.read_excel(MAPPING_FILENAME)
227
+ print(mapping_data_from_drive)
228
  extracted_data_for_mapping = pd.read_excel(data_for_mapping)
229
  extracted_data_for_mapping = extracted_data_for_mapping.merge(mapping_data_from_drive, on = ['Customer Part no as per pdf','Customer Part name as per pdf','Customer Part color as per pdf'], how='outer')
230
  extracted_data_for_mapping.to_excel(data_for_mapping,index=False)
 
248
  st.markdown("## Upload the Data Master file with Item Code mapping")
249
  mapping_uploaded_file = st.file_uploader("Upload the Data Master file with Item Code mapping", type=["xlsx","ods"])
250
  else:
251
+ mapping_data = pd.read_excel(MAPPING_FILENAME)
252
+ # mapping_data = mapping_data.rename(columns = {'Customer Part no as per pdf':'Part No.'})
253
+ data_for_mapping = "Data Mapping.xlsx"
254
+ extracted_data_for_mapping = pd.read_excel(data_for_mapping)
255
+ print(extracted_data_for_mapping)
256
+ print(mapping_data)
257
+
258
+ extracted_data_for_mapping = extracted_data_for_mapping[~extracted_data_for_mapping['Customer Part no as per pdf'].isin(mapping_data['Customer Part no as per pdf'])]
259
+ unmapped_part_no = extracted_data_for_mapping['Customer Part no as per pdf'].nunique()
260
+ if unmapped_part_no>0:
261
+ st.markdown("#### There are {} Part No. with No ItemCode present. Upload a new file after mapping them".format(unmapped_part_no))
262
+ mapping_uploaded_file = st.file_uploader("Upload the Data Master file with Item Code mapping", type=["xlsx","ods"])
263
+ # else:
264
+ # st.markdown("#### Using the Mapping file available in Google Drive")
265
+ # mapping_uploaded_file = MAPPING_FILENAME
 
266
 
267
  if mapping_uploaded_file is not None:
268
  # st.write("Uploaded Mapping Excel file:", mapping_uploaded_file.name)