File size: 44,309 Bytes
157e0b6
553d55b
154e666
045f497
b6242a9
7052a05
be9ee6a
2ea734c
49c1df5
be9ee6a
34d0189
154e666
 
be9ee6a
7b6c54a
be9ee6a
154e666
 
 
 
 
0aba0d5
4d37bdb
 
0aba0d5
11f0a45
 
0ab388f
7f7c726
be9ee6a
7b6c54a
7f7c726
157e0b6
be9ee6a
157e0b6
4a0f3d6
553d55b
49c1df5
7b6c54a
a26093a
df721bf
be9ee6a
50b5518
 
88e5483
a8b20a4
59b1e8f
 
 
d3102fd
59b1e8f
c754a09
 
59b1e8f
aee818b
f3168d5
50b5518
f3168d5
 
 
 
fb7e6dd
 
f3168d5
 
fb7e6dd
c2a6a94
885dc37
 
c2a6a94
49c1df5
 
4c59dd2
49c1df5
 
 
4c59dd2
0f8ca87
f3168d5
885dc37
f3168d5
 
49c1df5
f3168d5
49c1df5
 
 
f3168d5
da9599e
f3168d5
49c1df5
17e5ab8
 
0442e64
 
49c1df5
0442e64
 
49c1df5
 
d4881b3
 
 
efd2757
d4881b3
efd2757
 
 
 
 
d4881b3
efd2757
d4881b3
efd2757
 
 
 
d4881b3
efd2757
 
 
0aba0d5
 
d25a836
9b4cb90
 
0aba0d5
 
 
b20bfb9
 
0aba0d5
 
49c1df5
154e666
49c1df5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154e666
0ab388f
 
 
 
27cf908
 
7516de2
27cf908
0ab388f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aba0d5
d0760b8
 
11f0a45
 
d0760b8
7f7c726
d0760b8
11f0a45
154e666
 
 
 
 
 
 
 
 
 
7c0369c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f757146
7c0369c
 
 
 
 
 
 
4d37bdb
 
be9ee6a
045f497
7b6c54a
fbd490f
ed2b8be
fbd490f
 
 
4a0f3d6
 
 
 
 
 
 
 
7b6c54a
 
4a0f3d6
fbd490f
4a0f3d6
 
 
 
 
 
 
a26093a
045f497
 
 
 
 
 
 
 
 
 
 
4d37bdb
 
b20bfb9
0aba0d5
 
 
 
 
154e666
69d1aa6
4a0f3d6
154e666
c754a09
4d37bdb
154e666
 
0aba0d5
154e666
 
 
 
 
 
4d37bdb
 
154e666
b20bfb9
be9ee6a
c754a09
ee735af
154e666
 
 
f757146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
553d55b
 
 
 
 
c754a09
553d55b
c754a09
 
553d55b
 
c754a09
553d55b
 
7b6c54a
 
 
 
 
 
 
 
 
 
 
 
 
 
aee818b
7b6c54a
 
aee818b
7b6c54a
 
 
 
 
aee818b
553d55b
df721bf
 
553d55b
4a0f3d6
 
34d0189
392b463
 
6fe526f
a26093a
49c1df5
 
 
 
 
 
 
6fe526f
a26093a
6fe526f
49c1df5
 
 
 
 
 
f83d14a
a26093a
 
 
 
 
 
 
7b6c54a
561bba8
a26093a
7b6c54a
 
 
b6242a9
4a0f3d6
154e666
 
 
 
 
 
 
 
 
 
 
a26093a
 
 
0aba0d5
4d37bdb
154e666
 
 
4d37bdb
 
11f0a45
4d37bdb
 
 
 
 
154e666
11f0a45
 
 
 
 
 
 
4d37bdb
8ec3986
 
0fddb89
4d37bdb
 
 
11f0a45
154e666
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c754a09
 
11f0a45
154e666
c754a09
 
 
 
 
154e666
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be9ee6a
 
27cf908
be9ee6a
 
7c0369c
be9ee6a
 
4a0f3d6
 
 
 
 
 
 
 
 
 
be9ee6a
 
 
154e666
 
 
49c1df5
154e666
49c1df5
 
 
154e666
49c1df5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154e666
 
0ab388f
 
 
 
 
 
 
 
 
154e666
 
 
 
157e0b6
 
553d55b
157e0b6
 
 
 
 
 
 
 
154e666
 
 
 
8cebb71
553d55b
 
154e666
0061c53
154e666
 
 
 
 
0061c53
154e666
157e0b6
 
 
 
0061c53
157e0b6
 
 
 
 
 
208f1e7
157e0b6
 
 
 
 
154e666
 
 
0061c53
 
157e0b6
aee818b
 
34d0189
7f7c726
b6242a9
a26093a
b6242a9
154e666
7f7c726
154e666
a26093a
154e666
a26093a
154e666
b6242a9
a26093a
154e666
 
 
 
 
 
 
 
7b6c54a
11f0a45
7b6c54a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154e666
 
 
7b6c54a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154e666
 
 
7f7c726
154e666
 
 
34d0189
154e666
7ad3519
34d0189
7ad3519
34d0189
8c48588
34d0189
 
8c48588
7ad3519
154e666
11f0a45
 
 
 
 
 
 
d3102fd
 
 
 
7f7c726
 
9b4cb90
7f7c726
d3102fd
 
 
 
7f7c726
d3102fd
 
d0760b8
 
d3102fd
69d1aa6
 
7f7c726
 
69d1aa6
7f7c726
 
 
d0760b8
7f7c726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3102fd
7f7c726
 
 
 
 
 
 
d25a836
7f7c726
 
d25a836
7f7c726
 
 
 
d25a836
7f7c726
 
d25a836
 
7f7c726
d25a836
7f7c726
 
d25a836
7f7c726
49c1df5
9b4cb90
d3102fd
 
69d1aa6
 
 
7b6c54a
69d1aa6
d0760b8
 
157e0b6
d0760b8
 
 
 
 
 
 
 
69d1aa6
d0760b8
7b6c54a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0760b8
7b6c54a
d0760b8
7b6c54a
 
69d1aa6
7b6c54a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0760b8
 
69d1aa6
 
 
d3102fd
 
7f7c726
 
d3102fd
 
7f7c726
 
 
 
8cebb71
7f7c726
 
 
 
 
 
 
 
 
df721bf
 
154e666
 
 
 
 
 
 
 
157e0b6
 
 
b499b18
157e0b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b499b18
157e0b6
b499b18
157e0b6
b499b18
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
from __future__ import print_function
from flask import Flask, render_template,request,flash , json, url_for,g , redirect , jsonify , send_file ,make_response
import json
import fitz
from PIL import Image
import cv2  
import numpy as np
import pilecaps_adr   
import base64
from db import dropbox_connect
import cv2
import pandas as pd
import time
from io import BytesIO, StringIO
import urllib
import tempfile
from flask import Flask, Response
from werkzeug.wsgi import wrap_file
import tameem3_2
import pypdfium2 as pdfium
import pixelconversion
import tameem2_1
import io
from urllib.parse import unquote
import API
import MC_Templates_API
import tsadropboxretrieval
import doc_search
import google_sheet_Legend
import dxf__omar3_2
import requests
import google_sheet_to_xml
from threading import Thread
import mainDBAlaa
import datetime
import doors_fasterrcnn
import deploying_3_3
import Doors_Schedule
import Code_2_7
import Counting_Columns_2_1
# import Find_Hyperlinking_text
import ezdxf
from flask import Flask, render_template, session, redirect, url_for
from flask_session import Session  # Capital "S"
app = Flask(__name__)

prjnamesURL = 'https://docs.google.com/spreadsheets/d/1nsIgi9o9VSBKQlNxbxihPzG_N7s4um0eNVfgL4gaGPc/export?format=csv&gid=0'
prjpartsURL = 'https://docs.google.com/spreadsheets/d/1nsIgi9o9VSBKQlNxbxihPzG_N7s4um0eNVfgL4gaGPc/export?format=csv&gid=34865056'
prjsectionsURL = 'https://docs.google.com/spreadsheets/d/1nsIgi9o9VSBKQlNxbxihPzG_N7s4um0eNVfgL4gaGPc/export?format=csv&gid=1751466819'

# Global variables (example)
global colorsused
global pdflink
# For 2.7
global hatched_areas2_7

# Configure Flask-Session for server-side session storage
app.config["SESSION_TYPE"] = "filesystem"
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_FILE_DIR"] = "./flask_session_files"
app.secret_key = "your_secret_key"

# Initialize Flask-Session
sess = Session()
sess.init_app(app)

@app.route("/", methods=["GET", "POST"])
def getInfotoMeasure():
    return render_template("gui2.html")

@app.route("/password", methods=["GET", "POST"])
def password_page():
    return render_template("gui2.html")

@app.route("/check_password", methods=["POST"])
def check_password():
    password = request.form.get("password")
    correct_password = "c900"

    if password == correct_password:
        session["authenticated"] = True
        return jsonify({"authenticated": True}), 200
    else:
        return jsonify({"authenticated": False}), 200

@app.route("/mainGUI", methods=["GET", "POST"])
def main_gui():
    if "authenticated" not in session or not session["authenticated"]:
        return redirect(url_for("password_page"))
    return render_template("proposed-GUI.html")



@app.route("/WordSearch",methods=["GET", "POST"])
def getInfo2toMeasure():
    # API.AppendtablestoSheets()
    return render_template("wordSearch.html")



@app.route('/getprojectnames/',methods=['GET'])
def getprjnamesfromTestAPI():
    progress_updates = []  # Shared list to store progress

    def generate_progressProjnames():
        yield f"data: 10\n\n"  # Initial progress
        # Call retrieveProjects and pass a callback to update the shared list
        prjnames,prjids=API.getPrjNames( progress_callback=lambda p: progress_updates.append(p))
        # Continuously yield progress updates from the shared list

        while progress_updates:
            progress = progress_updates.pop(0)  # Get the next progress value
            yield f"data: {progress}\n\n"
        # Final progress and result
        yield f"data: 80\n\n"  
        yield f"data: 100\n\n"
        result = json.dumps([prjnames,prjids])
        yield f"data: {result}\n\n"
    return Response(generate_progressProjnames(), content_type='text/event-stream')


@app.route('/getprojectparts/<jsdata>',methods=['GET'])
def getprjpartsfromTestAPI(jsdata):
    print('kkkk',jsdata)
    prjparts,partsIds=API.getprjParts(jsdata)
    return jsonify([prjparts,partsIds])

@app.route('/getprojectsections/<jsdata>',methods=['GET'])
def getprjSectionsfromTestAPI(jsdata):
    data=json.loads(jsdata)
    sections=API.getprjSections(data[0],data[1])
    return jsonify(sections)

@app.route('/getmethod/<jsdata>', methods=['GET'])
def get_javascript_data(jsdata):
    progress_updates = []  # Shared list to store progress

    def generate_progress():
        yield f"data: 5\n\n"  # Initial progress
        yield f"data: 10\n\n"  # Initial progress
        # Call retrieveProjects and pass a callback to update the shared list
        documentsToMeasure, RelevantDocuments, extracted_path = tsadropboxretrieval.retrieveProjects(
            jsdata, 
            progress_callback=lambda p: progress_updates.append(p)
        )

        # Continuously yield progress updates from the shared list
        while progress_updates:
            progress = progress_updates.pop(0)  # Get the next progress value
            yield f"data: {progress}\n\n"
        # Final progress and result
        yield f"data: 100\n\n"
        result = json.dumps([documentsToMeasure, RelevantDocuments, extracted_path])
        yield f"data: {result}\n\n"

    return Response(generate_progress(), content_type='text/event-stream')

#---------------------------------------------------------------
@app.route('/WordSearch',methods=["GET", "POST"])
def searchDocument():
    return render_template('wordSearch.html')

@app.route('/legends',methods=["GET", "POST"])
def legendDirectory():
    return render_template('legendDirectory.html')

@app.route('/searchInDocs',methods=["GET", "POST"])
def getSearchinDocs():
   arr=[]
   values = request.get_json()
   keyword=values.get('keyword')
   listpfProjs=values.get('listofprojs')
   print(keyword,listpfProjs)
   df,img_list=doc_search.search_docs(keyword,listpfProjs)
   for img in img_list:
        _, buffer = cv2.imencode('.png', img)
        arr.append(base64.b64encode(buffer).decode('utf-8'))
   return jsonify([df.to_html(index=False, escape=False),arr])

@app.route('/searchInFolder',methods=["GET", "POST"])
def getSearchinFolder():
   arr=[]
   values = request.get_json()
   keyword=values.get('keyword')
   projname=values.get('ProjectName')
   df,img_list=doc_search.slow_search(keyword=keyword,project=projname)
   for img in img_list:
        _, buffer = cv2.imencode('.png', img)
        arr.append(base64.b64encode(buffer).decode('utf-8'))
   return jsonify([df.to_html(index=False, escape=False),arr])



@app.route("/measurementConsole",methods=["GET", "POST"])
def measurementConsoleFn():
    return render_template("proposed-GUI.html")

@app.route('/RetrieveMCTNames/',methods=['GET'])
def CallAPIforMCTNames():
    # print(jsdata)
    DictionaryOfTemplates=MC_Templates_API.RetrieveMC_Templates_API()
    # jsdata=jsdata.replace('"', '')
    print('here')
    return jsonify(DictionaryOfTemplates) #[str(jsdata).lower()]

#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
#Hex value to RGB value
def hexRGB(color):
  color=color.lstrip('#')
  color= tuple(int(color[i:i+2], 16) for i in (0, 2, 4)) #hex to rgb
  color=list(color) #rgb to bgr 
  return color
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
@app.route('/updatepreviewimg/<jsdata>',methods=["GET", "POST"])
def getfromdropboxImg(jsdata):
    # vv = eval(request.form.get('pdflink'))
    pdfpath=''
    # jsdata=jsdata.replace('"', '')
    jsdata=eval(jsdata)
    print('pdfnameeee==',jsdata)

    dbPath='/TSA JOBS/ADR Test/'+jsdata[0][0]+'/'+jsdata[0][1]+'/'+jsdata[0][2]+'/Measured Plan/'+jsdata[1]
    print(dbPath)
    dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
    md, res =dbxTeam.files_download(path=dbPath)
    data = res.content
    doc = fitz.open("pdf",data)
    page=doc[0]
    pix = page.get_pixmap()  # render page to an image
    pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
    img=np.array(pl)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    _, buffer = cv2.imencode('.png', img)
    return base64.b64encode(buffer).decode('utf-8')
    

@app.route('/savebase64Img/',methods=["POST"])
def stringToRGB():
    vv = eval(request.form.get('answers'))
    print(vv)
    if ( vv[5][2].startswith('3.2') or vv[5][2].startswith('3.3') or vv[5][2].startswith('2.7')) :
        print('3.2 section')
        pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=vv[0])
        dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
        md, res =dbxTeam.files_download(path=pdfpath)
        dataDoc = res.content
        if 'file' not in request.files:
            print('error, No file part in the request')
        else: 
            file = request.files['file']
            print('file done, measuring')
            arr=measureproject(vv,dataDoc,0,file)
            return jsonify(arr)
        
    
        
    if vv[5][2].startswith('1.0'):
        opencv_img,dataDoc = plan2img( str(vv[0]) )
        if vv[1]==220:
            imgdata = base64.b64decode(vv[6])
            img=Image.open(io.BytesIO(imgdata))
            opencv_img= cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
        arr=measureproject(vv,dataDoc,opencv_img)
       

    if vv[5][2].startswith('2.8') or vv[5][2].startswith('2.1'):
        pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF= str(vv[0]))
        dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
        md, res =dbxTeam.files_download(path=pdfpath)
        dataDoc = res.content
        doc = fitz.open("pdf",dataDoc)
        page=doc[0]
        if page.rotation!=0:
            page.set_rotation(0)
        pix = page.get_pixmap(dpi=300)  # render page to an image
        pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
        arr=measureproject(vv,dataDoc,pl)
    return jsonify(arr)

@app.route('/2.1Trial/',methods=["POST"])
def measure2_1():
    name = request.get_json()
    result=name.get('allvalues')
    arr=measureproject(result)
    return arr
#MAIN FUNCTION -- calls python code to measure the chosen plan from the interface
# @app.route('/projecttomeasure/<jsdata>',methods=["GET","POST"])
def measureproject(result,dataDoc=0,img=0,dxffile=0):
    colorarr=[]
    global pdflink
    # result = json.loads(jsdata) 
    pdfpath='/'
    #################################  -1.0- #################################
    
    for word in result[5]:
        pdfpath+=word +'/'
    arr=[]

    if result[5][2].startswith('1.0'): #section value - 1.0 substructure- pile caps
        for item in result[2]:
            # item1 ='#'+item 
            c=hexRGB(item)
            colorarr.append(c)
        print('RATIOS=',result[3], result[4])
        # cv2.imwrite(img,'imgg.png')
        # global pdflink
        imgPerimeter1,image_new1,SimilarAreaDictionary , colorsUsed,spreadsheet_url, spreadsheetId,list1, pdflink, areas_Perimeters, namepathArr =pilecaps_adr.drawAllContours(dataDoc,img,result[1],colorarr, result[3], result[4], result[0],pdfpath)
        _, buffer = cv2.imencode('.png', image_new1)
        arr=[base64.b64encode(buffer).decode('utf-8'),SimilarAreaDictionary.to_dict(),spreadsheet_url , spreadsheetId,colorsUsed,list1.to_dict(), pdflink, areas_Perimeters, namepathArr]
   #################################  -3.2- #################################
    # elif result[5][2].startswith('3.2'): #section value - 3.2 floor finishes
    #     print('IN HEREEEE 3.2')
    #     dxfpath=dxffile.read()
    #     with tempfile.NamedTemporaryFile(suffix='.dxf', delete=False) as temp_file:
    #         temp_file.write(dxfpath)
    #         temp_filename = temp_file.name
    #         print(temp_filename)
    #         doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas=dxf__omar3_2.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4] , pdfpath,result[0])
    #         dbPath='/TSA JOBS/ADR Test'+pdfpath+'Measured Plan/' 
    #         pdflink= tsadropboxretrieval.uploadanyFile(doc=doc,path=dbPath,pdfname=result[0]) #doc=doc,pdfname=path,pdfpath=pdfpath+'Measured Plan/'

    #         _, buffer = cv2.imencode('.png', outputimg)
    #         arr=[ base64.b64encode(buffer).decode('utf-8'),SimilarAreaDictionary.to_dict(), spreadsheet_url,spreadsheetId,[],list1.to_dict(),pdflink,hatched_areas,namepathArr]# , spreadsheetId, spreadsheet_url , list1.to_dict()]
   #################################  -3.3 or 3.2- #################################
    elif result[5][2].startswith('3.3') or result[5][2].startswith('3.2'): #section value - 3.2 floor finishes
            print('code of 3.3 and 3.2')
            dxfpath=dxffile.read()
            with tempfile.NamedTemporaryFile(suffix='.dxf', delete=False) as temp_file:
                temp_file.write(dxfpath)
                temp_filename = temp_file.name
                print(temp_filename)
                
                doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas=deploying_3_3.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4] , pdfpath,result[0])
                global colorsused
                colorsused=list(SimilarAreaDictionary['Color'])
                dbPath='/TSA JOBS/ADR Test'+pdfpath+'Measured Plan/'
                print(dbPath,result[0]) 
                # 
                pdflink= tsadropboxretrieval.uploadanyFile(doc=doc,path=dbPath,pdfname=result[0]) #doc=doc,pdfname=path,pdfpath=pdfpath+'Measured Plan/'

                _, buffer = cv2.imencode('.png', outputimg)
                arr=[ base64.b64encode(buffer).decode('utf-8'),SimilarAreaDictionary.to_dict(), spreadsheet_url,spreadsheetId,[],list1.to_dict(),pdflink,hatched_areas,namepathArr]#,hatched_areas,namepathArr]# , spreadsheetId, spreadsheet_url , list1.to_dict()]
    #################################  2.7- #################################
    elif result[5][2].startswith('2.7') : #section value - 2.7 floor finishes
            print('code of 2.7')
            dxfpath=dxffile.read()
            with tempfile.NamedTemporaryFile(suffix='.dxf', delete=False) as temp_file:
                temp_file.write(dxfpath)
                temp_filename = temp_file.name
                print(temp_filename)

                SearchArray=result[6]
                print(result)
                print("SearchArray = ",SearchArray)
                global hatched_areas2_7
                doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas=Code_2_7.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4],SearchArray, pdfpath,result[0])
                # global colorsused
                hatched_areas2_7=hatched_areas 
                colorsused=list(SimilarAreaDictionary['Color'])
                dbPath='/TSA JOBS/ADR Test'+pdfpath+'Measured Plan/'
                print(dbPath,result[0]) 
                # 
                pdflink= tsadropboxretrieval.uploadanyFile(doc=doc,path=dbPath,pdfname=result[0]) #doc=doc,pdfname=path,pdfpath=pdfpath+'Measured Plan/'

                _, buffer = cv2.imencode('.png', outputimg)
                arr=[ base64.b64encode(buffer).decode('utf-8'),SimilarAreaDictionary.to_dict(), spreadsheet_url,spreadsheetId,[],list1.to_dict(),pdflink,[],namepathArr]#,hatched_areas,namepathArr]# , spreadsheetId, spreadsheet_url , list1.to_dict()]
                # arr=[ base64.b64encode(buffer).decode('utf-8'),SimilarAreaDictionary.to_dict(), spreadsheet_url,spreadsheetId,[],list1.to_dict(),pdflink,hatched_areas,namepathArr]#,hatched_areas,namepathArr]# , spreadsheetId, spreadsheet_url , list1.to_dict()]
    #################################  -2.8- #################################
    elif result[5][2].startswith('2.8'): #section value - 2.8 floor finishes
            #vv[0] array of pdf names
        imgss=[] 
        dpxlinks=[]
        legendLinks=[]
        listofmarkups=[]

        pdfpathDoors,_=tsadropboxretrieval.getPathtoPDF_File(nameofPDF= str(result[6]))
        dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
        md, resDoors =dbxTeam.files_download(path=pdfpathDoors)
        dataDocDoorsSchedule = resDoors.content

        # annotatedimg,pdf_document,spreadsheet_url, list1 , df_doors=doors_fasterrcnn.main_run(img,dataDoc,'separated_classes_all.pth',result[0],pdfpath, result[4]) #single_double.pth
        annotatedimg, pdf_document , list1, repeated_labels , not_found =Doors_Schedule.mainRun(dataDocDoorsSchedule, dataDoc)
        dbPath='/TSA JOBS/ADR Test'+pdfpath+'Measured Plan/' 
        pdflink= tsadropboxretrieval.uploadanyFile(doc=pdf_document,path=dbPath,pdfname=result[0]) #doc=doc,pdfname=path,pdfpath=pdfpath+'Measured Plan/
        _, buffer = cv2.imencode('.png', annotatedimg)
        repeatedLabelsReturn=''
        NotFoundReturn=''
        if len(repeated_labels)>0:
            repeatedLabelsReturn=repeated_labels
        if len(not_found)>0:
            NotFoundReturn=not_found # , spreadsheetId, spreadsheet_url , list1.to_dict()]
        arr=[base64.b64encode(buffer).decode('utf-8') ,pdflink,list1.to_dict(),str(repeatedLabelsReturn), str(NotFoundReturn)] # , spreadsheetId, spreadsheet_url , list1.to_dict()]
    #################################  -2.1- #################################
    elif result[5][2].startswith('2.1'): #section value - 2.8 floor finishes
            #vv[0] array of pdf names
        imgss=[] 
        dpxlinks=[]
        legendLinks=[]
        listofmarkups=[]

        annotatedimg,pdf_document,spreadsheet_url, list1 , df_doors=Counting_Columns_2_1.mainfun(dataDoc,pdfpath,result[0]) 
        
        # dbPath='/TSA JOBS/ADR Test'+pdfpath+'Measured Plan/' 
        # pdflink= tsadropboxretrieval.uploadanyFile(doc=pdf_document,path=dbPath,pdfname=result[0]) #doc=doc,pdfname=path,pdfpath=pdfpath+'Measured Plan/
        # _, buffer = cv2.imencode('.png', annotatedimg)
        arr=[base64.b64encode(buffer).decode('utf-8') ,pdflink,spreadsheet_url,list1.to_dict(), df_doors.to_dict()]# , spreadsheetId, spreadsheet_url , list1.to_dict()]

    #################################  -2.2- #################################
    # elif result[5][2].startswith('2.2'): #section value - 2.2 rc slabs
        #add here python code
        #
        #link (add this to ur code)
        #   pdflink= db.dropbox_upload_file(doc=doc,pdfname=path,pdfpath=pdfpath)
        # gc,spreadsheet_service,spreadsheetId, spreadsheet_url , namepathArr= pilecaps_adr.legendGoogleSheets(df,plan ,pdfpath)
        #output img
        #  _, buffer = cv2.imencode('.png', outputimg)
        #let first result to be the img 
        #return results in arr=[base64.b64encode(buffer).decode('utf-8'),pdflink,spreadsheetId,spreadsheet_url] like the previous sections in the above lines 
    # elif result[5][2].startswith('2.1'): #section value - 2.1 frames
    #     url = tameem2_1.mainfun( result[0], pdfpath)
    #     return jsonify([url])

    return  arr
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________

@app.route("/canvaspdftoimgBackground/<jsdata>",methods=["GET", "POST"])
def pdftoimgCanvas(jsdata):
    img=plan2img(jsdata)[0]
    _, buffer = cv2.imencode('.png', img)
    arr=[base64.b64encode(buffer).decode('utf-8')  , img.shape[0],img.shape[1]]
    return jsonify(arr)


#pdf to img
def plan2img(nameofpdf):

    pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=nameofpdf)
    dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
    md, res =dbxTeam.files_download(path=pdfpath)
    data = res.content
    doc = fitz.open("pdf",data)
    page=doc[0]
    if page.rotation!=0:
        page.set_rotation(0)
    pix = page.get_pixmap(dpi=300)  # render page to an image
    pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
    img=np.array(pl)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    return img ,data
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________

#not used in 1.0
def convert2img(path):
    pdf = pdfium.PdfDocument(path)
    page = pdf.get_page(0)
    pil_image = page.render().to_pil()
    pl1=np.array(pil_image)
    img = cv2.cvtColor(pl1, cv2.COLOR_RGB2BGR)
    return img
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________

#User-made MC-T NAME - in the second part of the interface
@app.route("/mctnametoGoogleSheet/<jsdata>",methods=["GET", "POST"])
def sendmctnametoLegend(jsdata):
    result = json.loads(jsdata)
    print(result)
    global pdflink
    summaryid=google_sheet_Legend.mapnametoLegend(result,colorsused,pdflink)
    allreturns=[summaryid]
    return jsonify(allreturns)

@app.route("/getguessednamepath/",methods=["GET", "POST"])
def getguessedNames():
    guessednamesDrpdwn=google_sheet_Legend.getallguessednames()
    return jsonify(guessednamesDrpdwn)
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
#NOT USED (pixel conversion instead)
def calcRef(img):
  blk = np.ones(img.shape, dtype="uint8") * [[[np.uint8(0), np.uint8(0), np.uint8(0)]]]
  
  start_point = (50, 100)
  end_point = (120, 200)
  color = (255, 255, 255) # white BGR
  thickness = -1 # Thickness of -1 will fill the entire shape

  blk = cv2.rectangle(blk, start_point, end_point, color, thickness)
   
  blk = cv2.cvtColor(blk, cv2.COLOR_BGR2GRAY)

  contourzz, hierarchy = cv2.findContours(image=blk, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_NONE)
  for i, cnt3 in enumerate(contourzz):
    M = cv2.moments(cnt3)
    if M['m00'] != 0.0:
      x2 = int(M['m10']/M['m00'])
      y2 = int(M['m01']/M['m00'])
    area = cv2.contourArea(cnt3)
    perimeter = cv2.arcLength(cnt3, True)  
    return area,perimeter , blk

#NOT USED (pixel conversion instead)
def modifyingcalcRefDynamic(img):
  imgcopy = img.copy()

  blk = np.ones(img.shape, dtype="uint8") * [[[np.uint8(0), np.uint8(0), np.uint8(0)]]]
  
  x = 50
  y = 100
  xD = int(img.shape[1] * 0.10)
  yD = int(img.shape[0] * 0.10)
  start_point = (x, y)
  end_point = (x+xD, y+yD)
  blue = (255, 0, 0) #  BGR
  white = (255, 255, 255) #  BGR
  thickness = -1 # Thickness of -1 will fill the entire shape

  imgcopy = cv2.rectangle(imgcopy, start_point, end_point, blue, thickness)
  blk = cv2.rectangle(blk, start_point, end_point, white, thickness)
   
  blk = cv2.cvtColor(blk, cv2.COLOR_BGR2GRAY)

  contourzz, hierarchy = cv2.findContours(image=blk, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_NONE)
  for i, cnt3 in enumerate(contourzz):
    M = cv2.moments(cnt3)
    if M['m00'] != 0.0:
      x2 = int(M['m10']/M['m00'])
      y2 = int(M['m01']/M['m00'])
    area = cv2.contourArea(cnt3)
    perimeter = cv2.arcLength(cnt3, True)  
    return area, perimeter, blk , imgcopy
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________

@app.route('/get-pdf/<jsdata>')
def get_pdf(jsdata):
    print('pdfname',jsdata)
    # Download PDF from Dropbox
    pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=jsdata)
    print('pdfpath',pdfpath)
    dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
    md, res =dbxTeam.files_download(path=pdfpath) 

    doc = fitz.open("pdf",res.content)
    page=doc[0]
    # print('get pdf size::',page.mediabox)

    pdf_data = res.content  # Ensure this is your correct PDF data from Dropbox
    response = make_response(io.BytesIO(pdf_data).getvalue())
    response.headers.set('Content-Type', 'application/pdf')
    response.headers.set('Content-Disposition', 'attachment', filename='filename.pdf')  # Replace with your desired file name
    # return response
    return response


#PIXEL CONVERSION METHOD -- SAVES DOC ON DROPBOX TO BE MEASURED BY USER 
@app.route("/pixelimg/<jsdata>",methods=["GET", "POST"])
def getimg(jsdata):
    progress_updates = []  # Shared list to store progress
    jsdata=eval(jsdata)
    def generate_progressPixel():
        
        yield f"data: 10\n\n"  # Initial progress

        pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=jsdata[3], progress_callback=lambda p: progress_updates.append(p))
        dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
        md, res =dbxTeam.files_download(path=pdfpath)
        data = res.content
        print('jsdata',jsdata)
        if str(jsdata[2]).startswith('1.0'):
            doc,areaPixel,perimeterPixel=pixelconversion.drawisrotated(data,300) #  .openDrawPDF(data)
        else:
            doc,areaPixel,perimeterPixel,pdfdata=pixelconversion.drawisrotated(data) #  .openDrawPDF(data)
        yield f"data: 20\n\n"  # Initial progress
        dbPath='/TSA JOBS/ADR Test/'+jsdata[0]+'/'+jsdata[1]+'/'+jsdata[2]+'/'+'Scale Document' +'/'
        dburl=tsadropboxretrieval.uploadanyFile(doc=doc,pdfname=str(jsdata[3]) ,path=dbPath)
        # areaPixel,perimeterPixel= pixelconversion.getAreaPerimeter(dbPath, str(jsdata[3]))
        yield f"data: 40\n\n"  # Initial progress
        outputs=[areaPixel,perimeterPixel , dburl]
        while progress_updates:
            progress = progress_updates.pop(0)  # Get the next progress value
            yield f"data: {progress}\n\n"
        # Final progress and result
        yield  f"data: 80\n\n"
        yield f"data: 100\n\n"
        result = json.dumps(outputs)
        yield f"data: {result}\n\n"

    return Response(generate_progressPixel(), content_type='text/event-stream')
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
#get sunburst from doc_search
@app.route('/getsunburst',methods=["GET", "POST"])
def getSunburst():

    # df=tsadropboxretrieval.GetParquetDF()
    tree=doc_search.prepare_sunburst()
    return jsonify(tree.to_dict())
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
#DELETE MARKUPS (for comparison)
@app.route('/_submission',methods=["GET", "POST"])
def getnewlegend():
    pdfpth=''
    alljson = request.get_json()
    list1=alljson.get('dict1')
    print('list1',list1)
    # list1 = request.args.get('dict1')
    path=alljson.get('path')
    # path = request.args.get('path')
    spreadsheetId=alljson.get('spreadsheetId')
    # spreadsheetId =request.args.get('spreadsheetId')
    pdfpathpath=alljson.get('pdfpathpath')
    # pdfpathpath=request.args.get('pdfpathpath')
    print(pdfpathpath,type(pdfpathpath))
    pdfname=request.args.get('pdfname')
    for word in eval(pdfpathpath):
        pdfpth+='/' +word
    pdfpth+='/'
    dbPath='/TSA JOBS/ADR Test'+pdfpth+'Measured Plan/' 
    print(pdfpth)
    deletedrows1=google_sheet_Legend.deletemarkups(list1=list1,dbPath=dbPath,path=path) 
    arr1=[deletedrows1.to_dict()]
    print('arr,',arr1)
    return jsonify(arr1)
#---------------------------------------------------------------------------
#if user wishes to delete 
@app.route('/deletemarkupsroute',methods=["GET", "POST"])
def dltmarkupslegend():
    print('IN deletemarkupsroute')
    pdfpth=''
    alljson = request.get_json()
    SimilarAreaDictionary=alljson.get('dict')
    # SimilarAreaDictionary = request.args.get('dict')
    deletedrows=alljson.get('deletedrows')
    print('deletedrowsssssssssssssssssssssssssssssss',deletedrows)
    # deletedrows = request.args.get('deletedrows')
    path=alljson.get('path')
    # path = request.args.get('path')
    spreadsheetId=alljson.get('spreadsheetId')
    # spreadsheetId =request.args.get('spreadsheetId')
    areaPermArr=alljson.get('areaPermArr')
    print('aaaaaaaaaaaaa',areaPermArr)
    # areaPermArr=request.args.get('areaPermArr')
    section=alljson.get('section')
    # section=request.args.get('section')
    pdfpathpath=alljson.get('pdfpathpath')
    # pdfpathpath=request.args.get('pdfpathpath')
    for word in eval(pdfpathpath):
        pdfpth+='/' +word
    pdfpth+='/'
    # myDict=eval(deletedrows)
    deletedrows=pd.DataFrame(deletedrows)
    print('deletedrows',deletedrows)
    if section.startswith('2.7'):
        areaPermArr=hatched_areas2_7
    if section.startswith('1.0') or section.startswith('3.2') or section.startswith('3.3'):
        newlgnd=google_sheet_Legend.deletefromlegend(deletedrows=deletedrows,SimilarAreaDictionarycopy=SimilarAreaDictionary, section=section,areaPermArr=areaPermArr)
    elif section.startswith('2.8'):
        newlgnd=google_sheet_Legend.deletedoors(deletedrows,SimilarAreaDictionary)
        print('done wit 2.8 in deleting, didnt append yet ')
    else:
        newlgnd=google_sheet_Legend.deletefromlegend(deletedrows=deletedrows,SimilarAreaDictionarycopy=SimilarAreaDictionary, section=section)
    try:
            newlgnd=google_sheet_Legend.legendGoogleSheets(SimilarAreaDictionary=newlgnd,path=path,spreadsheetId=spreadsheetId ,pdfpath=pdfpth) #new legend
    except:

        print("An exception occurred")
        time.sleep(20)
        newlgnd=google_sheet_Legend.legendGoogleSheets(SimilarAreaDictionary=newlgnd,path=path,spreadsheetId=spreadsheetId,pdfpath=pdfpth)

    return jsonify('donee')

#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
#get pdf dropbox url after measurement is done
@app.route('/getdropboxurl/<jsdata>',methods=["GET", "POST"])
def calldropboxurl(jsdata):
    print('jsdata',jsdata)
    pdfurl=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=jsdata)[1]
    print('urll',pdfurl)
    if pdfurl and ('http' in pdfurl or 'dropbox' in pdfurl):
        if 'dl=0' in pdfurl:
            pdfurl = pdfurl.replace('dl=0', 'dl=1')
    print('urll1',pdfurl)
    # Download the PDF content from the shareable link
    response = requests.get(pdfurl)
    pdf_content = BytesIO(response.content)  # Store the content in memory
    if pdf_content is None:
        raise ValueError("No valid PDF content found.")

    # Open the PDF using PyMuPDF
    pdf_document = fitz.open(stream=pdf_content, filetype="pdf")
    pdf_bytes = BytesIO()
    pdf_document.save(pdf_bytes)
    return Response(pdf_bytes.getvalue(), content_type='application/pdf')

#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________

pdf_content = None
pageNumTextFound = 0
BASE_URL = "https://marthee-nbslink.hf.space"  # Hugging Face Spaces base URL

@app.route("/", methods=["GET", "POST"])
def thismain():
    print('ayhaga')
    return render_template("gui.html")

@app.route('/view-pdf', methods=['GET'])
def download_pdf():

    # Manually parse the query parameters
    full_query_string = request.query_string.decode()  # Get raw query string
    parsed_params = urllib.parse.parse_qs(full_query_string)  # Parse it

    # Extract pdfLink and keyword manually
    pdf_link = parsed_params.get('pdfLink', [None])[0]
    keyword = parsed_params.get('keyword', [None])[0]

    if not pdf_link or not keyword:
        return "Missing required parameters.", 400

    # Decode the extracted values
    pdf_link = urllib.parse.unquote(pdf_link)
    keyword = urllib.parse.unquote(keyword)

    # If the keyword is a JSON string, convert it back to a list
    try:
        keyword = json.loads(keyword)
    except json.JSONDecodeError:
        keyword = [keyword]  # Treat it as a single keyword if not JSON

    print("Extracted PDF Link:", pdf_link)
    print("Extracted Keywords:", keyword)
    createDF=False
    pdf_content = Find_Hyperlinking_text.annotate_text_from_pdf([pdf_link], keyword)[0]
    if pdf_content is None:
        return "PDF content not found.", 404

    pdf_bytes = BytesIO(pdf_content)
    return send_file(
        pdf_bytes,
        mimetype='application/pdf',
        as_attachment=False,
        download_name=f"annotated_page_{pageNumTextFound}.pdf"
    )

@app.route('/api/process-data', methods=['POST'])
def receive_pdf_data():
    global pdf_content, pageNumTextFound

    # Get PDF link and keyword from finddata()
    pdfLink, keyword = finddata()

    if not pdfLink or not keyword:
        return jsonify({"error": "Both 'pdfLink' and 'keyword' must be provided."}), 400

    try:
        print(pdfLink, keyword)


        pdfbytes, pdf_document , df ,tablepdfoutput= Find_Hyperlinking_text.annotate_text_from_pdf([pdfLink], keyword)
        dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')

        # Get metadata using the shared link
        metadata = dbxTeam.sharing_get_shared_link_metadata(pdfLink)
        dbPath='/TSA JOBS/ADR Test/FIND/'
        pdflink= tsadropboxretrieval.uploadanyFile(doc=pdf_document,path=dbPath,pdfname=metadata.name) #doc=doc,pdfname=path,pdfpath=pdfpath+'Measured Plan/
        print('LINKS0',pdflink)
     
        dbPath='/TSA JOBS/ADR Test/FIND/'
        tablepdfLink=tsadropboxretrieval.uploadanyFile(doc=tablepdfoutput,path=dbPath,pdfname=metadata.name.rsplit(".pdf", 1)[0] +' Markup Summary'+'.pdf')
        print(f"PDF successfully uploaded to Dropbox at")
        print('LINKS1',tablepdfLink)
        return jsonify({
            "message": "PDF processed successfully.",
            "PDF_MarkedUp": pdflink,
            'Table_PDF_Markup_Summary': tablepdfLink
        })

    except Exception as e:
        return jsonify({"error": str(e)}), 500

def finddata():
    pdfLink = 'https://www.dropbox.com/scl/fi/hnp4mqigb51a5kp89kgfa/00801-ARC-20-ZZ-S-A-0002.pdf?rlkey=45abeoebzqw4qwnslnei6dkd6&st=m4yrcjm2&dl=1'
    keyword = ['115 INTEGRATED MRI ROOM LININGS', '310 ACCURACY']
    return pdfLink, keyword


#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________

#Google sheet links
@app.route('/getdrivelinks/<jsdata>',methods=["GET", "POST"])
def getlinkscreated(jsdata):
    spreadsheet_service,drive_service,gc= google_sheet_Legend.authorizeLegend()
    ids=gc.spreadsheet_ids()
    titles=gc.spreadsheet_titles()
    allpaths=[]
    print('HEREEEEEEEEEE')
    # for title in titles:
    for i in range(0,len(titles)):
        print('titles',titles[i])
        if not (titles[i].startswith('API') or (titles[i].startswith('Dropbox')) ) :
            
            ws=gc.open(titles[i])
            path_metadata = ws.get_developer_metadata('path')
            print(path_metadata)
            allpaths.append([titles[i], ws.get_developer_metadata('path')[0].value , drive_service.files().get(fileId=ids[i],fields="createdTime, modifiedTime").execute() ,ids[i] ])
    
    return jsonify(allpaths)
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
@app.route('/getAPITables/',methods=["GET", "POST"])
def returnAPITables():
    # API.update_df()
    table1,table2,table3=API.GenerateTables()
    return  jsonify([table1.to_dict(),table2.to_dict(),table3.to_dict()])


@app.route('/refreshAPI',methods=["GET", "POST"])
def checktables():
    projectname =  request.args.get('prjname') 
    projectpart =  request.args.get('prjpart') 
    projectpartid =  request.args.get('prjpartid') 
    print('hgh',projectname , projectpart)
    table1,table2,table3=API.GenerateTables()
    url_1 = prjnamesURL.replace('/edit#gid=', '/export?format=csv&gid=')
    url_2 = prjpartsURL.replace('/edit#gid=', '/export?format=csv&gid=')
    url_3 = prjsectionsURL.replace('/edit#gid=', '/export?format=csv&gid=')
    
    table1N = pd.read_csv(url_1)
    table2N = pd.read_csv(url_2)
    table3N = pd.read_csv(url_3) 

    t1Diff=table1N[~table1N.isin(table1)].dropna()
    t1Diff1=table1[~table1.isin(table1N)].dropna()

    t2Diff=pd.DataFrame()
    t2Diff1=pd.DataFrame()

    t3Diff=pd.DataFrame()
    t3Diff1=pd.DataFrame()
    
    
    if projectname:
        print('here')
        differentParts=[]
        a = table1N.to_numpy()
        row = np.where(a == projectname)
        print(projectname,row)
        returnString=''
        t2Diff1=table2[~table2.isin(table2N)].dropna()
        t2Diff=table2N[~table2N.isin(table2)].dropna()
        
    if projectpart and projectname:
        
        a1 = table1N.to_numpy()
        row1 = np.where(a1 == projectname)
        prjid=a1[row1[0]][0][0]
        t3Diff1=table3[~table3.isin(table3N)].dropna()
        t3Diff=table3N[~table3N.isin(table3)].dropna()
     
    returnString=''
    if not t1Diff.empty  or not t1Diff1.empty :
        if not t1Diff.empty:
            newvalues= t1Diff['ProjectName'].values
        else:
            newvalues= t1Diff1['ProjectName'].values
        returnString=''
        returnString=['Changes have been made in Projects. Would you like to retrieve them in the console? By saying yes, you may need to repeat the website related processes(if any).','project',(newvalues).tolist(),table2.to_dict()]
    
    if (not t2Diff.empty)or( not t2Diff1.empty) :
        if projectname:
            for i in range(len(t2Diff1['ProjectId'].values)):           
                if int(t2Diff1['ProjectId'].values[i]) ==int(a[row[0]][0][0]):
                    differentParts.append(t2Diff1['ProjectPart'].iloc[i])
            if len(differentParts)>0:
                returnString=['Changes have been made in Parts. Would you like to retrieve them in the console?','part',differentParts]

    if (not t3Diff.empty)  or (not t3Diff1.empty) :
        differentSections=[] 
        if projectpart and projectname:           
            roww=t3Diff1.iloc[np.where((t3Diff1['ProjectId']==int(prjid)  ) & (t3Diff1['ProjectPartId']==int(projectpartid)) ) ] 
            for i in range(len(roww['ProjectId'].values)):
                differentSections.append(str(roww['ProjectSection'].iloc[i])) 
            if len(differentSections)>0:
                returnString=['Changes have been made in Sections. Would you like to retrieve them in the console?','section',differentSections]

    print(returnString)


    return jsonify(returnString)

@app.route('/refreshDropbox',methods=["GET", "POST"])
def checkdropbox():
    print('checkingggdf')
    dfFromDropbox=tsadropboxretrieval.DropboxItemstoDF("/TSA JOBS")[0]
    dfParquet=tsadropboxretrieval.GetParquetDF()

    dfParquet1 = dfParquet[['name', 'path_display', 'client_modified', 'server_modified']]

    deletedrows = pd.concat([dfFromDropbox, dfParquet1]).drop_duplicates(keep=False)
    deletedrows = deletedrows.reset_index(drop=True)
    deletedrows.columns = ['name', 'path_display', 'client_modified', 'server_modified']
    differences = deletedrows[~deletedrows.isin(dfFromDropbox)].dropna()
    if (len(differences)>0):   
        print(differences)
        dbxTeam=tsadropboxretrieval.dropbox_upload_file(dfFromDropbox)
        stringReturned= 'Updated Sucessfully.' 
    else:
        stringReturned= 'Nothing to update.' 
    return 'stringReturned'

def refreshDropboxRetrievals(extractedPath):
    dfFromDropbox = tsadropboxretrieval.DropboxItemstoDF(extractedPath)[0]
    dfParquet = tsadropboxretrieval.GetParquetDF()

    # print("Original Parquet size:", len(dfParquet))

    # Keep only relevant columns
    dfParquet = dfParquet[['name', 'path_display', 'client_modified', 'server_modified']]
    dfFromDropbox = dfFromDropbox[['name', 'path_display', 'client_modified', 'server_modified']]

    # **Drop rows that start with extractedPath**
    dfParquetUpdated = dfParquet[~dfParquet['path_display'].str.startswith(extractedPath)]
    
    # **Append new data from Dropbox**
    dfParquetUpdated = pd.concat([dfParquetUpdated, dfFromDropbox], ignore_index=True)

    # print("Updated Parquet size:", len(dfParquetUpdated),dfParquetUpdated)

    # **Save the updated Parquet file**
    tsadropboxretrieval.dropbox_upload_file(dfParquetUpdated)

    if len(dfFromDropbox) > 0:
        print("Updated entries:", dfFromDropbox)
        return 'Updated Successfully.'
    else:
        return 'Nothing to update.'





@app.route('/postdropboxprojects/<path:encoded_path>')
def handle_path(encoded_path):
    decoded_path = urllib.parse.unquote(encoded_path)  # Decode URL encoding
    extracted_path = json.loads(decoded_path)  # Convert back to Python object
    print('path to refresh',extracted_path)
    stringReturned=refreshDropboxRetrievals(extracted_path)
    print(stringReturned)
    return stringReturned



@app.route('/refreshAPIAppendNewTables',methods=["GET", "POST"])
def appendNewTables():
    # value = request.args.get('keyword')
    # print(value)
    API.AppendtablestoSheets()
    return jsonify('appended')


@app.route('/summarytoXML/<jsdata>',methods=["GET", "POST"])
def cvtSummarytoXML(jsdata):
    path='/TSA JOBS/ADR Test/'
    result = json.loads(jsdata)
    for word in result[0]:
        path+=word +'/'
    print(path)
    path=path+'XML/'
    # print(result[0])
    # print(result[1])
    xmllink=google_sheet_to_xml.create_xml(documentname=result[1],dbPath=path)
    return jsonify(xmllink)


#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________

#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________

#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
def runn():
#   app.run(host="127.0.0.1")
    from gevent.pywsgi import WSGIServer
    http_server = WSGIServer(("0.0.0.0", 7860), app)
    http_server.serve_forever()
    # serve(app, host="127.0.0.1", port=8080)


def keep_alive():
  t=Thread(target=runn)
  t.start()

dtn = datetime.datetime.now(datetime.timezone.utc)
print(dtn)
next_start = datetime.datetime(dtn.year, dtn.month, dtn.day, 21, 0, 0).astimezone(datetime.timezone.utc) #us - 2 = utc time  (21 utc is 19:00 our time and 9 is 7 our time , it needs to run 9 utc time ____ )                                                                             
print(next_start)
keep_alive()
# active_path = "/TSA Team Folder/ADR Test/Test/"
while 1:
  dtnNow = datetime.datetime.now(datetime.timezone.utc)
  print(dtnNow)
  if dtnNow >= next_start:
    next_start += datetime.timedelta(hours=12)  # 1 day
    print(next_start)
    checkdropbox()
   
  time.sleep(1800)
  
if __name__ == "__main__":
    runn()