Spaces:
Sleeping
Sleeping
| # -*- coding: utf-8 -*- | |
| """dropbox.ipynb | |
| Automatically generated by Colaboratory. | |
| Original file is located at | |
| https://colab.research.google.com/drive/11JdwIfblSKRelJJa7rllER8HwbSl_0Nu | |
| """ | |
| import dropbox | |
| import dropbox.exceptions as AuthError | |
| import cv2 | |
| import pandas as pd | |
| import os | |
| import fitz | |
| """### Lists of project names, parts, sections -- | |
| *Will get from MC API* | |
| """ | |
| prjname1=['2340 United Living Low Rise Building','2341 Bouygues-Cotswold','2342 GF Tomlinson - Hardingstone Primary School'] | |
| prjname1parts=['Block O Pumphouse','Hawthorn','Ling','Red Oak'] | |
| prjname2parts=['Block B','Block G'] | |
| prjname1sec1=['1.0 Substructure','5.1 Sanitary','8.2 Roads, Paths, Pavings','8.4 Fencing, Railing, Walls','8.5 External Fixtures','8.6 External Drainage','8.7 External Services','8.8 Minor Buildings'] | |
| prjname1sec2=['1.0 Substructure','2.1 Frames','2.3 Roof','2.4 Stairs','2.5 External Walls','2.6 External Openings','2.7 Internal Walls','2.8 Internal Openings','3.1 Wall Finishes','3.2 Floor Finishes','3.3 Ceiling Finishes','4.0 Fittings, Furnishings and Equipments','7.0 Works to exisitng buildings','8.1 Site Preparation Works','8.2 Roads, Paths, Pavings','8.3 Soft Landscaping','8.5 External Fixtures','8.6 External Drainage','8.7 External Services','8.8 Minor Buildings'] | |
| prjname1sec3=['1.0 Substructure','2.3 Roof','2.4 Stairs','2.5 External Walls','2.6 External Openings','2.7 Internal Walls','3.1 Wall Finishes','3.2 Floor Finishes','3.3 Ceiling Finishes', '4.0 Fittings, Furnishings and Equipments','7.0 Works to existing buildings','8.1 Site Preparation Works','8.2 Roads, Paths, Pavings','8.3 Soft Landscaping','8.5 External Fixtures','8.6 External Drainage'] | |
| prjname1sec4=['1.0 Substructure','2.3 Roof','2.4 Stairs','2.5 External Walls','2.6 External Openings','2.7 Internal Walls','3.1 Wall Finishes','3.2 Floor Finishes','3.3 Ceiling Finishes', '4.0 Fittings, Furnishings and Equipments','7.0 Works to existing buildings','8.1 Site Preparation Works','8.2 Roads, Paths, Pavings','8.3 Soft Landscaping','8.5 External Fixtures','8.6 External Drainage'] | |
| prj1secs=[prjname1sec1,prjname1sec2,prjname1sec3,prjname1sec4] | |
| prjname2parts=['Block B','Block G'] | |
| prjname2sec=['3.3 Ceiling Finishes'] | |
| prjname2sec2=['3.3 Ceiling Finishes'] | |
| prj2secs=[prjname2sec,prjname2sec2] | |
| prjname3parts=['Community Center building','External Works','Primary School'] | |
| prjname3sec1=['1.0 Substructure','2.3 Roof','3.1 Wall Finishes','3.2 Floor Finishes','3.3 Ceiling Finishes','4.0 Fittings, Furnishings and Equipments','5.1 Sanitary'] | |
| prjname3sec2=['8.1 Site Preparation Works','8.4 Fencing, Railing, Walls','8.5 External Fixtures', ' 8.6 External Drainage'] | |
| prjname3sec3=['1.0 Substructure','2.3 Roof','2.4 Stairs','3.2 Floor Finishes','3.3 Ceiling Finishes','4.0 Fittings, Furnishings and Equipments','5.1 Sanitary'] | |
| prj3secs=[prjname3sec1,prjname3sec2,prjname3sec3] | |
| """### Main Dropbox Functions :connect - list- download- upload""" | |
| access_token='sl.BiJ1Bte91KheyVoQI95qEQs9fVRR9Chq7U6RBtu73oJPWSrCaMUiNhRjwRzcCHbK4OrQsUTh9hPCJDJ5Ht7KASbAp-RffAqzZLIFMu6ZAHoWNygK2dShvKwznFF4eHGgp4AqD2RG' | |
| ################################################################################################################### | |
| def dropbox_connect(): | |
| """Create a connection to Dropbox.""" | |
| print('connecy') | |
| try: | |
| # print('ayhaga') | |
| dbx = dropbox.Dropbox( | |
| app_key='67w6ibpa9d2b60x', | |
| app_secret='d3ecz8g1604fu04', | |
| oauth2_refresh_token = 'R_LACBBNhysAAAAAAAAAAXt9mMy9OYIV_v4pF45lG6Z8DHNV66rq1q7acWjj_H5g', | |
| ) | |
| # dbx=dropbox.Dropbox(access_token) | |
| except AuthError as e: | |
| print('Error connecting to Dropbox with access token: ' + str(e)) | |
| return dbx | |
| ################################################################################################################ | |
| def dropbox_list_files(path): | |
| """Return a Pandas dataframe of files in a given Dropbox folder path in the Apps directory. | |
| """ | |
| dbx = dropbox_connect() | |
| try: | |
| files = dbx.files_list_folder(path).entries | |
| files_list = [] | |
| for file in files: | |
| if isinstance(file, dropbox.files.FileMetadata): | |
| metadata = { | |
| 'name': file.name, | |
| 'path_display': file.path_display, | |
| 'client_modified': file.client_modified, | |
| 'server_modified': file.server_modified | |
| } | |
| files_list.append(metadata) | |
| df = pd.DataFrame.from_records(files_list) | |
| return df.sort_values(by='server_modified', ascending=False) | |
| except Exception as e: | |
| print('Error getting list of files from Dropbox: ' + str(e)) | |
| ##################################################################################################################### | |
| def dropbox_download_file(dropbox_file_path, local_file_path): | |
| """Download a file from Dropbox to the local machine.""" | |
| try: | |
| dbx = dropbox_connect() | |
| with open(local_file_path, 'wb') as f: | |
| metadata, result = dbx.files_download(path=dropbox_file_path) | |
| f.write(result.content) | |
| except Exception as e: | |
| print('Error downloading file from Dropbox: ' + str(e)) | |
| ######################################################################################################################### | |
| def dropbox_shareableLink(path): | |
| dbx= dropbox_connect() | |
| print('herdeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee') | |
| try: | |
| shared_link_metadata = dbx.sharing_create_shared_link_with_settings(path) | |
| except: | |
| shared_link_metadata=dbx.sharing_create_shared_link(path) | |
| return shared_link_metadata.url | |
| ######################################################################################################################### | |
| def dropbox_upload_file( doc, pdfname, pdfpath,flag=0): | |
| try: | |
| dbx = dropbox_connect() | |
| path= pdfpath + pdfname | |
| print('pathindb:',path) | |
| if flag==0: | |
| meta=dbx.files_upload(doc.write(),path,mode=dropbox.files.WriteMode("overwrite")) | |
| elif flag==1: | |
| perm = int( # permissions bit flags | |
| fitz.PDF_PERM_ACCESSIBILITY # always use this | |
| | fitz.PDF_PERM_PRINT # permit printing | |
| ) | |
| encrypt = fitz.PDF_ENCRYPT_AES_256 # strongest algorithm | |
| meta=dbx.files_upload( doc.write() ,path,mode=dropbox.files.WriteMode("overwrite")) | |
| try: | |
| shared_link_metadata = dbx.sharing_create_shared_link_with_settings(path) | |
| except: | |
| shared_link_metadata=dbx.sharing_create_shared_link(path) | |
| # shared_link_metadata=dbx.sharing_get_shared_link_metadata(path) | |
| print( shared_link_metadata.url) | |
| return shared_link_metadata.url | |
| except Exception as e: | |
| print('Error uploading file to Dropbox: ' + str(e)) | |
| ######################################################################################################################### | |
| """### Create Dictionaries (relation between values)""" | |
| def createDictionaries(): | |
| projectsdictionary={} # prj name: parts | |
| sectionsdict={} #each prj part:sections | |
| #allprj parts | |
| parts=[prjname1parts,prjname2parts,prjname3parts] | |
| #allprjsections | |
| sections=[prj1secs,prj2secs,prj3secs] | |
| for i in range(len(prjname1)): | |
| projectsdictionary.update({prjname1[i]:parts[i]}) | |
| for j in range(len(parts[i]) ): | |
| sectionsdict.update({parts[i][j]:sections[i][j]}) | |
| return projectsdictionary, sectionsdict , parts | |
| """### Create prj from scratch or prj name with all subfolders in it""" | |
| def create_folder(projectsdictionary={},sectionsdict={}): | |
| dbx = dropbox_connect() | |
| projectsdictionary, sectionsdict , parts= createDictionaries() | |
| try: | |
| df1,df2=dropbox_list_files('') | |
| print(df1,df2) | |
| if (len(df2)>0): | |
| if list(df2['name'])==prjname1: | |
| return projectsdictionary, sectionsdict , parts | |
| elif list(df2['name'])!=prjname1: | |
| if len(projectsdictionary)==0 and len(sectionsdict)==0: | |
| print(len(projectsdictionary)) | |
| else: | |
| names=[] | |
| secondnames=[] | |
| for item in projectsdictionary.items(): | |
| for secondItem in item[1]: | |
| name='/'+item[0] | |
| secondname='/'+secondItem | |
| name+=secondname | |
| names.append(name) | |
| for item1 in sectionsdict.items(): | |
| for ii in item1[1]: | |
| if secondItem== item1[0]: | |
| final =name+'/'+ii | |
| print(final) | |
| dbx.files_create_folder(path=final) | |
| except Exception as e: | |
| print('eeeeeerorrrrrrrrrr',e) | |
| return projectsdictionary, sectionsdict , parts | |
| def dropbox_list_files(path): | |
| """Return a Pandas dataframe of files in a given Dropbox folder path in the Apps directory. | |
| """ | |
| dbx = dropbox_connect() | |
| try: | |
| files = dbx.files_list_folder(path).entries | |
| files_list = [] | |
| folders_list=[] | |
| for file in files: | |
| if isinstance(file, dropbox.files.FileMetadata): | |
| metadata = { | |
| 'name': file.name, | |
| 'path_display': file.path_display, | |
| # 'client_modified': file.client_modified, | |
| # 'server_modified': file.server_modified | |
| } | |
| files_list.append(metadata) | |
| else:#folder | |
| metadata = { | |
| 'name': file.name, | |
| 'path_display': file.path_display, | |
| } | |
| folders_list.append(metadata) | |
| df = pd.DataFrame.from_records(files_list) | |
| df2 = pd.DataFrame.from_records(folders_list) | |
| return df , df2 | |
| except Exception as e: | |
| print('Error getting list of files from Dropbox: ' + str(e)) | |
| def prjNameCheckandCreate(projectsdictionary, sectionsdict): | |
| df1,df2=dropbox_list_files('') | |
| if len(df2)!=0: | |
| df2Values=list(df2['name'].values) | |
| difference = list(set(prjname1) - set(df2Values)) | |
| if len(difference)==0: | |
| print(df2Values) | |
| else: | |
| newdict={} | |
| newsecdict={} | |
| print('else') | |
| for i in difference: | |
| newdict.update({i:projectsdictionary.get(i)}) | |
| for key,value in newdict.items(): | |
| for ii in value: | |
| newsecdict.update({ii:sectionsdict.get(ii)}) | |
| create_folder(projectsdictionary=newdict,sectionsdict=newsecdict) | |
| return df2 | |
| def prjPartSectionCheckandCreate(): | |
| projectsdictionary, sectionsdict , parts=create_folder() | |
| df2= prjNameCheckandCreate(projectsdictionary, sectionsdict ) | |
| for i in range(len(df2['path_display'].values)): | |
| #look for the same path | |
| df11,df21=dropbox_list_files(df2['path_display'].values[i]) | |
| if len(df21)!=0: | |
| df2Values=list(df21['name'].values) | |
| difference = list(set(parts[i]) - set(df2Values)) | |
| if len(difference)==0: | |
| print('samee') | |
| else: | |
| for j in difference: | |
| dbx = dropbox_connect() | |
| dbx.files_create_folder_v2(df2['path_display'].values[i]+'/'+j) #nopart - create part folder | |
| for k in sectionsdict.get(j): | |
| dbx.files_create_folder(df2['path_display'].values[i]+'/'+j+'/'+k) #create sections folders | |
| #check if section is missing and get prj name and part for path | |
| for arr in df2Values: | |
| df111,df211=dropbox_list_files(df2['path_display'].values[i]+'/'+arr) | |
| if len(df211)!=0: | |
| df211Values=list(df211['name'].values) | |
| for key,value in sectionsdict.items(): | |
| if key== arr: | |
| if value == df211Values: | |
| print('s') | |
| else: | |
| difference1 = list(set(value) - set(df211Values)) | |
| dbx = dropbox_connect() | |
| for d in difference1: | |
| print(arr,d) | |
| dbx.files_create_folder_v2(df2['path_display'].values[i]+'/'+arr+'/'+d) #no section - create section folder | |