-
Notifications
You must be signed in to change notification settings - Fork 4
/
harvest_uustaffpages_to_ricgraph.py
720 lines (641 loc) · 33.2 KB
/
harvest_uustaffpages_to_ricgraph.py
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
# ########################################################################
#
# Ricgraph - Research in context graph
#
# ########################################################################
#
# MIT License
#
# Copyright (c) 2023 Rik D.T. Janssen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# ########################################################################
#
# This file contains example code for Ricgraph.
#
# With this code, you can harvest the Utrecht University (UU) staff pages.
# You have to set some parameters in ricgraph.ini.
# Also, you can set a number of parameters in the code following the "import" statements below.
#
# Original version Rik D.T. Janssen, March 2023.
# Updated Rik D.T. Janssen, April, November 2023.
#
# ########################################################################
#
# Usage
# harvest_uustaffpages_to_ricgraph.py [options]
#
# Options:
# --empty_ricgraph <yes|no>
# 'yes': Ricgraph will be emptied before harvesting.
# 'no': Ricgraph will not be emptied before harvesting.
# If this option is not present, the script will prompt the user
# what to do.
#
# ########################################################################
import os.path
import sys
import re
import pandas
from typing import Union
import requests
import pathlib
import configparser
import ricgraph as rcg
global UUSTAFF_URL
# ######################################################
# Parameters for harvesting from UU staff pages
# ######################################################
UUSTAFF_MAX_FACULTY_NR = 25
UUSTAFF_CONNECTDATA_FROM_FILE = False
UUSTAFF_HARVEST_FROM_FILE = False
UUSTAFF_HARVEST_FILENAME = 'uustaff_harvest.json'
UUSTAFF_DATA_FILENAME = 'uustaff_data.csv'
UUSTAFF_CONNECT_FILENAME = 'uustaff_connect.csv'
UUSTAFF_MAX_RECS_TO_HARVEST = 0 # 0 = all records
# We can harvest many fields from the UU staff pages. For now,
# we only need a few.
UUSTAFF_FIELDS_TO_HARVEST = [
# 'ContactDetails',
'Email',
'Expertises',
# 'Faculties', # Here 'Positions' is used.
'FocusAreas',
'Id',
# 'Images',
# 'KeyPublications',
# 'LastUpdate',
# 'Links',
'LinksSocialMedia',
# 'Name',
'NameShort',
'Organisation',
'PhotoUrl',
'Positions',
# 'Prizes',
# 'Profile',
# 'Projects',
# 'ProjectsCompleted',
# 'Publications',
'Skills'
]
UU_WEBSITE = 'https://www.uu.nl'
UUSTAFF_FACULTY_ENDPOINT = '/Public/GetEmployeesOrganogram?f='
UUSTAFF_EMPLOYEE_ENDPOINT = '/Public/getEmployeeData?page='
UUSTAFF_SOLISID_ENDPOINT = '/RestApi/getmedewerkers?selectie=solisid:'
UUSTAFF_PHOTO_ENDPOINT = '/Public/GetImage?Employee='
# ######################################################
# Parsing
# ######################################################
def parse_uustaff_persons(harvest: list) -> pandas.DataFrame:
"""Parse the harvested persons from the UU staff pages.
:param harvest: the harvest.
:return: the harvested persons in a DataFrame.
"""
parse_result = pandas.DataFrame()
parse_chunk = [] # list of dictionaries
print('There are ' + str(len(harvest)) + ' person records ('
+ rcg.timestamp() + '), parsing record: 0 ', end='')
count = 0
for harvest_item in harvest:
count += 1
if count % 1000 == 0:
print(count, ' ', end='', flush=True)
if count % 10000 == 0:
print('(' + rcg.timestamp() + ')\n', end='', flush=True)
if 'Employee_Url' in harvest_item:
path = pathlib.PurePath(harvest_item['Employee_Url'])
uustaff_page_id = str(path.name)
parse_line = {'UUSTAFF_PAGE_ID': uustaff_page_id,
'UUSTAFF_PAGE_URL': str(harvest_item['Employee_Url'])}
# Sometimes EmployeeUrl has 'https:/www...' instead of 'https://www...', repair.
parse_line['UUSTAFF_PAGE_URL'] = re.sub(pattern=r'https:/www',
repl='https://www',
string=parse_line['UUSTAFF_PAGE_URL'])
parse_chunk.append(parse_line)
else:
# There must be an Employee_Url (UUSTAFF_PAGE_ID), otherwise skip.
continue
if 'Id' in harvest_item:
parse_line = {'UUSTAFF_PAGE_ID': uustaff_page_id,
'UUSTAFF_ID_PERS': str(harvest_item['Id'])}
parse_chunk.append(parse_line)
else:
# There must be an Id (UUSTAFF_ID_PERS), otherwise skip.
continue
if 'NameShort' in harvest_item:
parse_line = {'UUSTAFF_PAGE_ID': uustaff_page_id,
'FULL_NAME': str(harvest_item['NameShort'])}
parse_chunk.append(parse_line)
if 'Email' in harvest_item:
parse_line = {'UUSTAFF_PAGE_ID': uustaff_page_id,
'EMAIL': str(harvest_item['Email'])}
parse_chunk.append(parse_line)
if 'PhotoUrl' in harvest_item:
# 'PhotoUrl' has the form '/Public/GetImage?Employee=12&_t=34567890&t='
# The values after '_t=' and 't=' are not relevant, the value after 'Employee=' is.
# Probably it is UUSTAFF_ID_PERS, but I'd rather use the 'Employee' value to be sure.
# Remove everything before 'Employee=' and after '&'.
employee_id = str(harvest_item['PhotoUrl']).lower()
employee_id = re.sub(pattern=r'^.*employee=', repl='', string=employee_id)
employee_id = re.sub(pattern=r'&.*', repl='', string=employee_id)
if employee_id == '':
continue
parse_line = {'UUSTAFF_PAGE_ID': uustaff_page_id,
'PHOTO_ID': str(employee_id)}
parse_line['PHOTO_URL'] = UUSTAFF_URL + UUSTAFF_PHOTO_ENDPOINT + parse_line['PHOTO_ID']
parse_chunk.append(parse_line)
if 'LinksSocialMedia' in harvest_item:
for links in harvest_item['LinksSocialMedia']:
if 'Name' in links and 'Url' in links:
if links['Name'] is None or links['Url'] is None:
continue
parse_line = {'UUSTAFF_PAGE_ID': uustaff_page_id}
name_identifier = str(links['Name'].lower())
value_identifier = str(links['Url'])
path = pathlib.PurePath(value_identifier)
path_name = str(path.name)
if 'orcid' in name_identifier:
parse_line['ORCID'] = path_name
elif 'linkedin' in name_identifier or 'linked-in' in name_identifier:
parse_line['LINKEDIN'] = path_name
elif 'github' in name_identifier:
parse_line['GITHUB'] = path_name
elif 'twitter' in name_identifier:
parse_line['TWITTER'] = path_name
else:
parse_line[links['Name']] = value_identifier
parse_chunk.append(parse_line)
if 'Positions' in harvest_item:
# This might be improved by including department, institute, etc.
org_name = ''
for stafforg in harvest_item['Positions']:
if 'Level1' in stafforg:
# We take the first organization that is not 'Universiteit Utrecht'
if stafforg['Level1'] == '' \
or stafforg['Level1'] == 'Universiteit Utrecht':
continue
else:
org_name = str(stafforg['Level1'])
break
if org_name != '':
parse_line = {'UUSTAFF_PAGE_ID': uustaff_page_id,
'FACULTY': org_name}
parse_chunk.append(parse_line)
if 'Expertises' in harvest_item:
for expertise in harvest_item['Expertises']:
if 'Name' in expertise and 'Url' in expertise:
parse_line = {'UUSTAFF_PAGE_ID': uustaff_page_id,
'EXPERTISE_AREA_NAME': str(expertise['Name']),
'EXPERTISE_AREA_URL': UU_WEBSITE + str(expertise['Url'])}
parse_chunk.append(parse_line)
if 'FocusAreas' in harvest_item:
for focusarea in harvest_item['FocusAreas']:
if 'Name' in focusarea and 'Url' in focusarea:
# Focus areas are called 'Research areas' on the UU website.
parse_line = {'UUSTAFF_PAGE_ID': uustaff_page_id,
'RESEARCH_AREA_NAME': str(focusarea['Name']),
'RESEARCH_AREA_URL': UU_WEBSITE + str(focusarea['Url'])}
parse_chunk.append(parse_line)
if 'Skills' in harvest_item:
for skill in harvest_item['Skills']:
if 'Name' in skill and 'Url' in skill:
parse_line = {'UUSTAFF_PAGE_ID': uustaff_page_id,
'SKILL_NAME': str(skill['Name']),
'SKILL_URL': UU_WEBSITE + str(skill['Url'])}
parse_chunk.append(parse_line)
print(count, '(' + rcg.timestamp() + ')\n', end='', flush=True)
parse_chunk_df = pandas.DataFrame(parse_chunk)
parse_result = pandas.concat([parse_result, parse_chunk_df], ignore_index=True)
# dropna(how='all'): drop row if all row values contain NaN
parse_result.dropna(axis=0, how='all', inplace=True)
parse_result.drop_duplicates(keep='first', inplace=True, ignore_index=True)
return parse_result
# ######################################################
# Harvesting and parsing
# ######################################################
def harvest_json_uustaffpages(url: str, max_recs_to_harvest: int = 0) -> list:
"""Harvest json data from a file.
:param url: URL to harvest.
:param max_recs_to_harvest: maximum records to harvest.
:return: list of records in json format, or empty list if nothing found.
"""
print('Harvesting json data from ' + url + '.')
print('Getting data...')
all_records = 9999999999 # a large number
if max_recs_to_harvest == 0:
max_recs_to_harvest = all_records
json_data = []
count = 0
for faculty_nr in range(UUSTAFF_MAX_FACULTY_NR):
if count >= max_recs_to_harvest:
break
print('[faculty nr ' + str(faculty_nr) + ' at ' + rcg.timestamp() + ']')
# 'l-EN' ensures that phone numbers are preceded with "+31".
# 'fullresult=true' or '=false' only differ in 'Guid' field value.
faculty_url = url + UUSTAFF_FACULTY_ENDPOINT + str(faculty_nr) + '&l=EN&fullresult=true'
faculty_response = requests.get(faculty_url)
if faculty_response.status_code != requests.codes.ok:
print('harvest_json_uustaffpages(): error during harvest faculties.')
print('Status code: ' + str(faculty_response.status_code))
print('Url: ' + faculty_response.url)
print('Error: ' + faculty_response.text)
exit(1)
faculty_page = faculty_response.json()
if 'Employees' not in faculty_page:
# Empty faculty.
continue
if len(faculty_page['Employees']) == 0:
# Empty faculty.
continue
df_employees = pandas.DataFrame(faculty_page['Employees'])
df_employees_url = df_employees['Url']
df_employees_url.dropna(axis=0, how='any', inplace=True)
if df_employees_url is None:
# Nothing found.
continue
employees_of_faculty = list(df_employees_url)
for employee_id in employees_of_faculty:
if count >= max_recs_to_harvest:
break
employee_url = url + UUSTAFF_EMPLOYEE_ENDPOINT + employee_id + '&l=EN'
employee_response = requests.get(employee_url)
if employee_response.status_code != requests.codes.ok:
print('harvest_json_uustaffpages(): error during harvest employees.')
print('Status code: ' + str(employee_response.status_code))
print('Url: ' + employee_response.url)
print('Error: ' + employee_response.text)
exit(1)
employee_page = employee_response.json()
if 'Employee' in employee_page:
parse = {}
path = pathlib.PurePath(url)
parse['Employee_Url'] = str(path.parent) + '/' + employee_id
for element in UUSTAFF_FIELDS_TO_HARVEST:
if element in employee_page['Employee']:
tmp = employee_page['Employee'][element]
if isinstance(tmp, list) and len(tmp) == 0:
continue
if tmp is not None:
parse[element] = tmp
json_data.append(parse)
count += 1
if count % 50 == 0:
print(count, '(' + rcg.timestamp() + ') ', end='', flush=True)
if count % 500 == 0:
print('\n', end='', flush=True)
print(' Done at ' + rcg.timestamp() + '.\n')
return json_data
def harvest_json_and_write_to_file_uustaffpages(filename: str,
url: str,
max_recs_to_harvest: int = 0) -> list:
"""Harvest json data and write the data found to a file.
This data is a list of records in json format. If no records are harvested, nothing is written.
:param filename: filename of the file to use for writing.
:param url: URL to harvest.
:param max_recs_to_harvest: maximum records to harvest.
:return: list of records in json format, or empty list if nothing found.
"""
json_data = harvest_json_uustaffpages(url=url,
max_recs_to_harvest=max_recs_to_harvest)
if len(json_data) == 0:
return []
rcg.write_json_to_file(json_data=json_data,
filename=filename)
return json_data
def harvest_and_parse_uustaffpages_data(url: str,
harvest_filename: str) -> Union[pandas.DataFrame, None]:
"""Harvest and parse data from UU staff pages.
:param url: API link to UU staff pages.
:param harvest_filename: filename to write harvest results to.
:return: the DataFrame harvested, or None if nothing harvested.
"""
print('Harvesting UU staff pages...')
if not UUSTAFF_HARVEST_FROM_FILE:
retval = harvest_json_and_write_to_file_uustaffpages(filename=harvest_filename,
url=url,
max_recs_to_harvest=UUSTAFF_MAX_RECS_TO_HARVEST)
if len(retval) == 0:
# Nothing found.
return None
harvest_data = rcg.read_json_from_file(filename=harvest_filename)
parse = parse_uustaff_persons(harvest=harvest_data)
print('The harvested records are:')
print(parse)
return parse
# ######################################################
# Parsed results to Ricgraph
# ######################################################
def parsed_uustaff_persons_to_ricgraph(parsed_content: pandas.DataFrame) -> None:
"""Insert the parsed persons in Ricgraph.
:param parsed_content: The records to insert in Ricgraph, if not present yet.
:return: None.
"""
timestamp = rcg.datetimestamp()
print('Inserting persons from UU staff pages in Ricgraph at '
+ timestamp + '...')
history_event = 'Source: Harvest UU staff pages persons at ' + timestamp + '.'
# The order of the columns in the DataFrame below is not random.
# A good choice is to have in the first two columns:
# a. the identifier that appears the most in the system we harvest.
# b. the identifier(s) that is already present in Ricgraph from previous harvests,
# since new identifiers from this harvest will be linked to an already existing
# person-root.
# If you have 2 of type (b), use these as the first 2 columns.
#
# Below, we chose UUSTAFF_PAGE_ID as first identifier, because this is the identifier
# we used to link SolisID to in the previous step
# (in parsed_pure_uustaffpages_to_ricgraph()).
# ####### Insert persons.
person_identifiers = parsed_content[['UUSTAFF_PAGE_ID', 'ORCID',
'UUSTAFF_ID_PERS', 'FULL_NAME',
'EMAIL', 'PHOTO_ID',
'TWITTER', 'LINKEDIN',
'GITHUB']].copy(deep=True)
# dropna(how='all'): drop row if all row values contain NaN
person_identifiers.dropna(axis=0, how='all', inplace=True)
person_identifiers.drop_duplicates(keep='first', inplace=True, ignore_index=True)
print('The following persons from UU staff pages will be inserted in Ricgraph at '
+ rcg.timestamp() + ':')
print(person_identifiers)
rcg.unify_personal_identifiers(personal_identifiers=person_identifiers,
source_event='UU staff pages',
history_event=history_event)
# ####### Add weblinks (by using 'url_main') to nodes we have inserted above.
nodes_to_update = parsed_content[['UUSTAFF_PAGE_ID', 'UUSTAFF_PAGE_URL']].copy(deep=True)
nodes_to_update.rename(columns={'UUSTAFF_PAGE_ID': 'value',
'UUSTAFF_PAGE_URL': 'url_main'}, inplace=True)
nodes_to_update_columns = {'name': 'UUSTAFF_PAGE_ID',
'category': 'person'}
nodes_to_update = nodes_to_update.assign(**nodes_to_update_columns)
nodes_to_update = nodes_to_update[['name', 'category', 'value', 'url_main']]
print('\nThe following page nodes will be updated in Ricgraph at '
+ rcg.timestamp() + ':')
print(nodes_to_update)
rcg.update_nodes_df(nodes=nodes_to_update)
# ####### Add weblinks (by using 'url_main') to nodes we have inserted above.
nodes_to_update = parsed_content[['PHOTO_ID', 'PHOTO_URL']].copy(deep=True)
nodes_to_update.rename(columns={'PHOTO_ID': 'value',
'PHOTO_URL': 'url_main'}, inplace=True)
nodes_to_update_columns = {'name': 'PHOTO_ID',
'category': 'person'}
nodes_to_update = nodes_to_update.assign(**nodes_to_update_columns)
nodes_to_update = nodes_to_update[['name', 'category', 'value', 'url_main']]
print('\nThe following photo nodes will be updated in Ricgraph at '
+ rcg.timestamp() + ':')
print(nodes_to_update)
rcg.update_nodes_df(nodes=nodes_to_update)
# ####### Insert organizations (faculties only).
organizations = parsed_content[['UUSTAFF_PAGE_ID', 'FACULTY']].copy(deep=True)
organizations.dropna(axis=0, how='any', inplace=True)
organizations.drop_duplicates(keep='first', inplace=True, ignore_index=True)
organizations.rename(columns={'UUSTAFF_PAGE_ID': 'value1',
'FACULTY': 'value2'}, inplace=True)
new_organization_columns = {'name1': 'UUSTAFF_PAGE_ID',
'category1': 'person',
'name2': 'ORGANIZATION_NAME',
'category2': 'organization',
'source_event2': 'UU staff pages',
'history_event2': history_event}
organizations = organizations.assign(**new_organization_columns)
organizations = organizations[['name1', 'category1', 'value1',
'name2', 'category2', 'value2',
'source_event2', 'history_event2']]
print('The following organizations from UU staff pages will be inserted in Ricgraph at '
+ rcg.timestamp() + ':')
print(organizations)
rcg.create_nodepairs_and_edges_df(left_and_right_nodepairs=organizations)
# ####### Insert organizations (connect all to Utrecht University, all staff is part of UU).
organizations.drop(columns={'value2'}, inplace=True)
organizations['value2'] = 'Utrecht University'
organizations = organizations[['name1', 'category1', 'value1',
'name2', 'category2', 'value2',
'source_event2', 'history_event2']]
print('"Utrecht University" will be connected to anyone from UU staff pages in Ricgraph at '
+ rcg.timestamp() + ':')
print(organizations)
rcg.create_nodepairs_and_edges_df(left_and_right_nodepairs=organizations)
# ####### Insert expertises.
expertises = parsed_content[['UUSTAFF_PAGE_ID', 'EXPERTISE_AREA_NAME',
'EXPERTISE_AREA_URL']].copy(deep=True)
expertises.dropna(axis=0, how='any', inplace=True)
expertises.drop_duplicates(keep='first', inplace=True, ignore_index=True)
expertises.rename(columns={'UUSTAFF_PAGE_ID': 'value1',
'EXPERTISE_AREA_NAME': 'value2',
'EXPERTISE_AREA_URL': 'url_main2'}, inplace=True)
new_expertises_columns = {'name1': 'UUSTAFF_PAGE_ID',
'category1': 'person',
'name2': 'EXPERTISE_AREA',
'category2': 'competence',
'source_event2': 'UU staff pages',
'history_event2': history_event}
expertises = expertises.assign(**new_expertises_columns)
expertises = expertises[['name1', 'category1', 'value1',
'name2', 'category2', 'value2',
'url_main2', 'source_event2', 'history_event2']]
print('The following expertises from UU staff pages will be inserted in Ricgraph at '
+ rcg.timestamp() + ':')
print(expertises)
rcg.create_nodepairs_and_edges_df(left_and_right_nodepairs=expertises)
# ####### Insert research areas.
research_areas = parsed_content[['UUSTAFF_PAGE_ID', 'RESEARCH_AREA_NAME',
'RESEARCH_AREA_URL']].copy(deep=True)
research_areas.dropna(axis=0, how='any', inplace=True)
research_areas.drop_duplicates(keep='first', inplace=True, ignore_index=True)
research_areas.rename(columns={'UUSTAFF_PAGE_ID': 'value1',
'RESEARCH_AREA_NAME': 'value2',
'RESEARCH_AREA_URL': 'url_main2'}, inplace=True)
new_research_areas_columns = {'name1': 'UUSTAFF_PAGE_ID',
'category1': 'person',
'name2': 'RESEARCH_AREA',
'category2': 'competence',
'source_event2': 'UU staff pages',
'history_event2': history_event}
research_areas = research_areas.assign(**new_research_areas_columns)
research_areas = research_areas[['name1', 'category1', 'value1',
'name2', 'category2', 'value2',
'url_main2', 'source_event2', 'history_event2']]
print('The following research areas from UU staff pages will be inserted in Ricgraph at '
+ rcg.timestamp() + ':')
print(research_areas)
rcg.create_nodepairs_and_edges_df(left_and_right_nodepairs=research_areas)
# ####### Insert skills.
skills = parsed_content[['UUSTAFF_PAGE_ID', 'SKILL_NAME', 'SKILL_URL']].copy(deep=True)
skills.dropna(axis=0, how='any', inplace=True)
skills.drop_duplicates(keep='first', inplace=True, ignore_index=True)
skills.rename(columns={'UUSTAFF_PAGE_ID': 'value1',
'SKILL_NAME': 'value2',
'SKILL_URL': 'url_main2'}, inplace=True)
new_skills_columns = {'name1': 'UUSTAFF_PAGE_ID',
'category1': 'person',
'name2': 'SKILL',
'category2': 'competence',
'source_event2': 'UU staff pages',
'history_event2': history_event}
skills = skills.assign(**new_skills_columns)
skills = skills[['name1', 'category1', 'value1',
'name2', 'category2', 'value2',
'url_main2', 'source_event2', 'history_event2']]
print('The following skills from UU staff pages will be inserted in Ricgraph at '
+ rcg.timestamp() + ':')
print(skills)
rcg.create_nodepairs_and_edges_df(left_and_right_nodepairs=skills)
print('\nDone at ' + rcg.timestamp() + '.\n')
return
def connect_pure_with_uustaffpages(url: str) -> Union[pandas.DataFrame, None]:
"""Connect Pure with the UU staff pages.
Get SolisID from Ricgraph and harvest the corresponding data from the
UU staff pages.
:param url: url to the UU staff pages.
:return: the DataFrame harvested, or None if nothing harvested.
"""
print('Connect Pure SolisIDs with corresponding persons from UU staff pages at '
+ rcg.datetimestamp() + '...')
nodes_with_solisid = rcg.read_all_nodes(name='EMPLOYEE_ID')
print('There are ' + str(len(nodes_with_solisid)) + ' SolisID records, parsing record: 0 ', end='')
parse_result = pandas.DataFrame()
parse_chunk = [] # list of dictionaries
count = 0
for node in nodes_with_solisid:
count += 1
if count % 50 == 0:
print(count, ' ', end='', flush=True)
if count % 500 == 0:
print('(' + rcg.timestamp() + ')\n', end='', flush=True)
solis_id = node['value']
solis_url = url + UUSTAFF_SOLISID_ENDPOINT + solis_id
response = requests.get(solis_url)
if response.status_code != requests.codes.ok:
print('connect_pure_with_uustaffpages(): error during harvest solisID.')
print('Status code: ' + str(response.status_code))
print('Url: ' + response.url)
print('Error: ' + response.text)
exit(1)
page = response.json()
if len(page) == 0:
continue
if 'UrlEN' in page[0]:
uustaff_page_url = str(page[0]['UrlEN'])
elif 'UrlNL' in page:
uustaff_page_url = str(page[0]['UrlNL'])
else:
# Link to staff page not present, continue.
continue
path = pathlib.PurePath(uustaff_page_url)
parse_line = {'EMPLOYEE_ID': str(node['value']),
'UUSTAFF_PAGE_ID': str(path.name)}
parse_chunk.append(parse_line)
print(count, '\n', end='', flush=True)
print('Done at ' + rcg.timestamp() + '.\n')
parse_chunk_df = pandas.DataFrame(parse_chunk)
parse_result = pandas.concat([parse_result, parse_chunk_df], ignore_index=True)
parse_result.dropna(axis=0, how='all', inplace=True)
parse_result.drop_duplicates(keep='first', inplace=True, ignore_index=True)
return parse_result
def parsed_pure_uustaffpages_to_ricgraph(parsed_content: pandas.DataFrame) -> None:
"""Insert the parsed Pure SolisIDs and UU staff data in Ricgraph.
:param parsed_content: The records to insert in Ricgraph, if not present yet.
:return: None.
"""
timestamp = rcg.datetimestamp()
history_event = 'Source: Harvest UU staff pages connect EMPLOYEE_ID and UUSTAFF_PAGE_ID at ' + timestamp + '.'
solisids_staffids = parsed_content[['EMPLOYEE_ID', 'UUSTAFF_PAGE_ID']].copy(deep=True)
solisids_staffids.rename(columns={'EMPLOYEE_ID': 'value1',
'UUSTAFF_PAGE_ID': 'value2'}, inplace=True)
new_solisids_staffids_columns = {'name1': 'EMPLOYEE_ID',
'category1': 'person',
'name2': 'UUSTAFF_PAGE_ID',
'category2': 'person',
'source_event2': 'UU staff pages',
'history_event2': history_event}
solisids_staffids = solisids_staffids.assign(**new_solisids_staffids_columns)
solisids_staffids = solisids_staffids[['name1', 'category1', 'value1',
'name2', 'category2', 'value2',
'source_event2', 'history_event2']]
print('The following Pure SolisIDs and corresponding persons from UU staff pages will be inserted in Ricgraph at '
+ rcg.timestamp() + ':')
print(solisids_staffids)
rcg.create_nodepairs_and_edges_df(left_and_right_nodepairs=solisids_staffids)
print('Done at ' + rcg.timestamp() + '.\n')
return
# ############################################
# ################### main ###################
# ############################################
rcg.print_commandline_arguments(argument_list=sys.argv)
config = configparser.ConfigParser()
config.read(rcg.get_ricgraph_ini_file())
try:
UUSTAFF_URL = config['UU_staff_pages_harvesting']['uustaff_url']
if UUSTAFF_URL == '':
print('Ricgraph initialization: error, uustaff_url empty in Ricgraph ini file, exiting.')
exit(1)
except KeyError:
print('Ricgraph initialization: error, uustaff_url not found in Ricgraph ini file, exiting.')
exit(1)
rcg.open_ricgraph()
empty_graph = rcg.get_commandline_argument(argument='--empty_ricgraph',
argument_list=sys.argv)
if empty_graph == '':
# Empty Ricgraph, choose one of the following.
# rcg.empty_ricgraph(answer='yes')
# rcg.empty_ricgraph(answer='no')
rcg.empty_ricgraph()
else:
rcg.empty_ricgraph(answer=empty_graph)
rcg.graphdb_nr_accesses_print()
# ########## Code block A ##########
# You can use 'True' or 'False' depending on your needs to harvest.
# This might be handy if you are testing your parsing.
# if False: # Uncomment this line to comment out code block A
if True: # Comment this line to comment out code block A
print('\nNote: If this script hangs, just run it again.')
print('This is probably due to a time-out of the server that is hosting the UU staff pages.\n')
if not UUSTAFF_CONNECTDATA_FROM_FILE:
parsed_results = connect_pure_with_uustaffpages(url=UUSTAFF_URL)
if parsed_results is None or parsed_results.empty:
print('There are no Pure SolisIDs to connect to UU staff pages.\n')
else:
rcg.write_dataframe_to_csv(filename=UUSTAFF_CONNECT_FILENAME,
df=parsed_results)
parsed_pure_uustaffpages_to_ricgraph(parsed_content=parsed_results)
else:
parsed_results = rcg.read_dataframe_from_csv(filename=UUSTAFF_CONNECT_FILENAME)
parsed_pure_uustaffpages_to_ricgraph(parsed_content=parsed_results)
rcg.graphdb_nr_accesses_print()
# ########## End of code block A ##########
# if False:
if True:
print('\nNote: If this script hangs from this point on, do the following:')
print('1. Edit the python code, comment out code block A (i.e. make sure it does not get executed).')
print('2. Rerun this script.')
print('This is probably due to a time-out of the server that is hosting the UU staff pages.\n')
parse_uustaff = harvest_and_parse_uustaffpages_data(url=UUSTAFF_URL,
harvest_filename=UUSTAFF_HARVEST_FILENAME)
if parse_uustaff is None or parse_uustaff.empty:
print('There are no UU staff data to harvest.\n')
exit(0)
rcg.write_dataframe_to_csv(filename=UUSTAFF_DATA_FILENAME,
df=parse_uustaff)
# Harvesting from UU staff pages could be improved by better
# parsing for UU sub organizations and UU research output.
# For inspiration see harvest_pure_to_ricgraph.py.
parsed_uustaff_persons_to_ricgraph(parsed_content=parse_uustaff)
rcg.graphdb_nr_accesses_print()
rcg.close_ricgraph()