@@ -49,15 +49,7 @@ def handle(self, *args, **options):
4949
5050 j = json .loads (resp .text )
5151
52- # print(resp,json.dumps(j,indent=2))
53-
5452 transkribus_shortrefs = ShortRef .objects .all ().exclude (transkribus_docId = None )
55- transkribus_shortrefs = transkribus_shortrefs .exclude (name__icontains = "SSC Add Ms" )
56- # transkribus_shortrefs=transkribus_shortrefs.exclude(transkribus_docId__in=[1301052,1594526])
57-
58- # transkribus_shortrefs=transkribus_shortrefs.filter(transkribus_docId=1296568)
59-
60- # transkribus_shortrefs=transkribus_shortrefs.exclude(transkribus_docId__in=[1301052,1594526])
6153
6254 print (transkribus_shortrefs )
6355
@@ -77,7 +69,8 @@ def handle(self, *args, **options):
7769 pd [a ]= [b ]
7870
7971 for docId in pd :
80- if docId not in ['25566' , '25565' , '25564' , '25563' , '25562' , '25561' , '25560' , '25555' , '25552' , '25554' , '25553' , '25551' , '25550' , '25575' , '25557' , '25567' , '25556' , '25510' , '25509' , '25508' , '25506' , '25507' , '25505' , '25504' , '25503' , '25502' , '25501' , '25500' , '25582' , '25576' , '25577' ,'1437762' ,'1470747' ,'1470827' ,'1470863' ,'1470876' ,'1487718' ,'1487721' ,'1492986' ,'1493090' ,'1493096' ,'1494915' ,'1494931' ,'1496973' ,'1497235' ]:
72+ # if docId not in ['25566', '25565', '25564', '25563', '25562', '25561', '25560', '25555', '25552', '25554', '25553', '25551', '25550', '25575', '25557', '25567', '25556', '25510', '25509', '25508', '25506', '25507', '25505', '25504', '25503', '25502', '25501', '25500', '25582', '25576', '25577','1437762','1470747','1470827','1470863','1470876','1487718','1487721','1492986','1493090','1493096','1494915','1494931','1496973','1497235']:
73+ if docId not in ['1437762' , '1439396' , '1438141' ]:
8174 break
8275 print ('DOCID' ,docId )
8376 pages = pd [docId ]
@@ -86,8 +79,8 @@ def handle(self, *args, **options):
8679
8780 for pagepk in pages :
8881 page = Page .objects .get (id = pagepk )
82+ print (page )
8983 page .transkribus_pageid = None
90- page .transcription = None
9184 page .save ()
9285
9386 doc_url = "https://transkribus.eu/TrpServer/rest/collections/%s/%s/fulldoc" % (transkribus_collection_id ,docId )
@@ -96,67 +89,69 @@ def handle(self, *args, **options):
9689
9790 c = 1
9891 for transkribus_page in doc ['pageList' ]['pages' ]:
99- # print("PAGES",pages,'COUNT',len(pages))
100- # print("C=",c)
10192 pagetexturls = []
102- sp = Page .objects .get (id = pages [c - 1 ])
103- print (sp .source_connections .first ().source )
10493
94+ imgFileName = transkribus_page ['imgFileName' ]
10595
106- if sp .transcriptions .all () != []:
107- print ("transcription already exists" )
108- else :
109- imgFileName = transkribus_page ['imgFileName' ]
110- ## but as a backstop we can capture the pageid on the transkribus side as well
111- ## and then bump them as necessary and re-pull (i don't want to do this!!!!)
112- transkribus_pageId = transkribus_page ['pageId' ]
113- transcripts = transkribus_page ['tsList' ]['transcripts' ]
114- # if c==8:
115- # print(json.dumps(transkribus_page,indent=2))
116-
117- pagetext = ''
118- for transcript in transcripts :
119- wordcount = transcript ['nrOfCharsInLines' ]
120- if wordcount > 0 :
121- transcript_url = transcript ['url' ]
122- # print(transcript_url)
123- connection_pool = urllib3 .PoolManager ()
124- resp = connection_pool .request ('GET' ,transcript_url ,headers = auth_headers )
125- tmpfilename = "tmp/%s.xml" % docId
126- f = open (tmpfilename , 'wb' )
127- f .write (resp .data )
128- f .close ()
129- resp .release_conn ()
130- d = open (tmpfilename ,"r" )
131- t = d .read ()
132-
133- d .close ()
134- pagetree = ET .fromstring (t )
135- textlines_trees = [e for e in pagetree .iter () if e .tag == "{http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15}TextLine" ]
136- textlines = []
137-
138- os .remove (tmpfilename )
139-
140- for textline_tree in textlines_trees :
141- linetext = [e .text for e in textline_tree .iter () if e .tag == '{http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15}Unicode' and e .text is not None ]
142- textlines .append (' ' .join (linetext ))
143- pagetext += '\n ' .join (textlines )
144- # print(pagetext)
145- if re .match ('\s+' ,pagetext ,re .S ):
146- pagetext = None
147-
148- # sp.transkribus_pageid=transkribus_pageId
149- Transcription .objects .create (
150- page = sp ,
151- language_code = 'en' ,
152- text = pagetext ,
153- is_translation = False
154- )
155-
156- # sp.transcriptions.add=pagetext
157- # sp.save()
158-
159-
96+ imgFileName_stripped = re .sub ('(\.jpg|\.tif|\.tiff)' ,'' ,imgFileName )
97+
98+ skip = False
99+ try :
100+ sp = Page .objects .get (image_filename__icontains = imgFileName_stripped )
101+ print (sp .source_connections .first ().source )
102+ except :
103+ print (f"IMAGE MATCHING FILENAME DOES NOT EXIST:{ imgFileName_stripped } " )
104+ skip = True
105+ if not skip :
106+ if sp .transcriptions .all ().count () != 0 :
107+ print (sp .transcriptions .all ())
108+ print ("transcription already exists" )
109+ else :
110+ imgFileName = transkribus_page ['imgFileName' ]
111+
112+ ## but as a backstop we can capture the pageid on the transkribus side as well
113+ ## and then bump them as necessary and re-pull (i don't want to do this!!!!)
114+ transkribus_pageId = transkribus_page ['pageId' ]
115+ transcripts = transkribus_page ['tsList' ]['transcripts' ]
116+ pagetext = ''
117+ max_ts = 0
118+ transcript = transcripts [0 ]
119+
120+ if transcript is not None :
121+
122+ wordcount = transcript ['nrOfCharsInLines' ]
123+ if wordcount > 0 :
124+ transcript_url = transcript ['url' ]
125+ connection_pool = urllib3 .PoolManager ()
126+ resp = connection_pool .request ('GET' ,transcript_url ,headers = auth_headers )
127+ tmpfilename = "document/management/commands/data/%s.xml" % docId
128+ f = open (tmpfilename , 'wb' )
129+ f .write (resp .data )
130+ f .close ()
131+ resp .release_conn ()
132+ d = open (tmpfilename ,"r" )
133+ t = d .read ()
134+ d .close ()
135+ pagetree = ET .fromstring (t )
136+ textlines_trees = [e for e in pagetree .iter () if e .tag == "{http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15}TextLine" ]
137+ textlines = []
138+
139+ os .remove (tmpfilename )
140+
141+ for textline_tree in textlines_trees :
142+ linetext = [e .text for e in textline_tree .iter () if e .tag == '{http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15}Unicode' and e .text is not None ]
143+ textlines .append (' ' .join (linetext ))
144+ pagetext += '\n ' .join (textlines )
145+ print (pagetext )
146+ if re .match ('\s+' ,pagetext ,re .S ):
147+ pagetext = ""
148+
149+ Transcription .objects .create (
150+ page = sp ,
151+ language_code = 'en' ,
152+ text = pagetext ,
153+ is_translation = False
154+ )
160155
161156
162157 c += 1
0 commit comments