Skip to content

Commit 88e56f2

Browse files
committed
Added heuristics to process existing photo files for a larger percentage of person records than earlier. Changed to checking all person records, not only those with roles. Added a summary of photo files not handled at the end. This reduced the number of unhandled files from ~350 to less than 10, and all the unhandled ones seems to belong to persons for which photos have been found.
- Legacy-Id: 11262
1 parent 8a4d0b3 commit 88e56f2

2 files changed

Lines changed: 218 additions & 121 deletions

File tree

ietf/bin/2016-05-25-collect-photos

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
#!/usr/bin/env python
2+
3+
import os, sys, shutil, pathlib
4+
5+
# boilerplate
6+
basedir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../"))
7+
sys.path = [ basedir ] + sys.path
8+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ietf.settings")
9+
10+
import django
11+
django.setup()
12+
13+
import debug
14+
15+
from ietf.group.models import Role, Person
16+
17+
old_images_dir = django.conf.settings.OLD_PHOTOS_DIR
18+
new_images_dir = django.conf.settings.PHOTOS_DIR
19+
20+
if not os.path.exists(old_images_dir):
21+
print("Old images directory does not exist: %s" % old_images_dir)
22+
sys.exit(1)
23+
if not os.path.exists(new_images_dir):
24+
print("New images directory does not exist: %s" % new_images_dir)
25+
sys.exit(1)
26+
27+
old_image_files = []
28+
29+
30+
for (dirpath, dirnames, filenames) in os.walk(old_images_dir):
31+
if len(filenames) == 0:
32+
print("No image files found in %s" % old_images_dir)
33+
sys.exit(2)
34+
old_image_files.extend(filenames)
35+
break # Only interested in the files in the top directory
36+
37+
old_image_files = [ f.name.decode('utf8') for f in pathlib.Path(old_images_dir).iterdir() if f.is_file() and not f.suffix.lower() in ['.lck'] ]
38+
39+
interesting_persons = set()
40+
41+
interesting_persons.update(list(Person.objects.all()))
42+
43+
name_alias = {
44+
"andy": ["andrew", ],
45+
"ben": ["benjamin", ],
46+
"bill": ["william", ],
47+
"bob": ["robert", ],
48+
"chris": ["christopher", "christian"],
49+
"dan": ["daniel", ],
50+
"dave": ["david", ],
51+
"dick": ["richard", ],
52+
"fred": ["alfred", ],
53+
"geoff": ["geoffrey", ],
54+
"jake": ["jacob", ],
55+
"jerry": ["gerald", ],
56+
"jim": ["james", ],
57+
"joe": ["joseph", ],
58+
"jon": ["jonathan", ],
59+
"mike": ["michael", ],
60+
"ned": ["edward", ],
61+
"pete": ["peter", ],
62+
"ron": ["ronald", ],
63+
"russ": ["russel", ],
64+
"steve": ["stephen", ],
65+
"ted": ["edward", ],
66+
"terry": ["terence", ],
67+
"tom": ["thomas", ],
68+
"wes": ["wesley", ],
69+
"will": ["william", ],
70+
71+
"beth": ["elizabeth", ],
72+
"liz": ["elizabeth", ],
73+
"lynn": ["carolyn", ],
74+
"pat": ["patricia", "patrick", ],
75+
"sue": ["susan", ],
76+
}
77+
# Add lookups from long to short, from the initial set
78+
for key,value in name_alias.items():
79+
for item in value:
80+
if item in name_alias:
81+
name_alias[item] += [ key ];
82+
else:
83+
name_alias[item] = [ key ];
84+
85+
exceptions = {
86+
'Aboba' : 'aboba-bernard',
87+
'Bernardos' : 'cano-carlos',
88+
'Bormann' : 'bormann-carsten',
89+
'Hinden' : 'hinden-bob',
90+
'Hutton' : 'hutton-andy',
91+
'Narten' : 'narten-thomas', # but there's no picture of him
92+
'O\'Donoghue' : 'odonoghue-karen',
93+
'Przygienda' : 'przygienda-antoni',
94+
'Salowey' : 'salowey-joe',
95+
'Gunter Van de Velde' : 'vandevelde-gunter',
96+
'Eric Vyncke' : 'vynke-eric',
97+
'Zuniga' : 'zuniga-carlos-juan',
98+
'Zhen Cao' : 'zhen-cao',
99+
'Jamal Hadi Salim': 'hadi-salim-jamal',
100+
}
101+
102+
# Manually copied Bo Burman and Thubert Pascal from wg/photos/
103+
# Manually copied Victor Pascual (main image, not thumb) from wg/
104+
# Manually copied Eric Vync?ke (main image, not thumb) from wg/photos/
105+
# Manually copied Danial King (main image, not thumb) from wg/photos/
106+
# Manually copied the thumb (not labelled as such) for Tianran Zhou as both the main and thumb image from wg/photos/
107+
108+
processed_files = []
109+
110+
for person in sorted(list(interesting_persons),key=lambda x:x.last_name()+x.ascii):
111+
substr_pattern = None
112+
for exception in exceptions:
113+
if exception in person.ascii:
114+
substr_pattern = exceptions[exception]
115+
break
116+
if not person.ascii.strip():
117+
print(" Setting person.ascii for %s" % person.name)
118+
person.ascii = person.name.encode('ascii', errors='replace')
119+
debug.show('person.ascii')
120+
name_parts = person.ascii.lower().split()
121+
if not substr_pattern:
122+
substr_pattern = u'-'.join(name_parts[-1:]+name_parts[0:1])
123+
124+
candidates = [x for x in old_image_files if x.lower().startswith(substr_pattern)]
125+
# If no joy, try a short name
126+
if not candidates and name_parts[0] in name_alias:
127+
for alias in name_alias[name_parts[0]]:
128+
substr_pattern = u'-'.join(name_parts[-1:]+[alias])
129+
candidates += [x for x in old_image_files if x.lower().startswith(substr_pattern)]
130+
if candidates:
131+
print(" Used '%s %s' instead of '%s %s'" % (alias, name_parts[-1], name_parts[0], name_parts[-1], ))
132+
# If still no joy, reverse the name order (necessary for Deng Hui, for instance)
133+
if not candidates:
134+
substr_pattern = u'-'.join(name_parts[0:1]+name_parts[-1:])
135+
candidates = [x for x in old_image_files if x.lower().startswith(substr_pattern)]
136+
if candidates:
137+
print(" Used '%s %s' instead of '%s %s'" % (name_parts[-1], name_parts[0], name_parts[0], name_parts[-1], ))
138+
# If still no joy, try with Person.plain_name() (necessary for Donald Eastlake)
139+
if not candidates:
140+
name_parts = person.plain_name().lower().split()
141+
substr_pattern = u'-'.join(name_parts[-1:]+name_parts[0:1])
142+
candidates = [x for x in old_image_files if x.lower().startswith(substr_pattern)]
143+
# If no joy, try a short name
144+
if not candidates and name_parts[0] in name_alias:
145+
for alias in name_alias[name_parts[0]]:
146+
substr_pattern = u'-'.join(name_parts[-1:]+[alias])
147+
candidates += [x for x in old_image_files if x.lower().startswith(substr_pattern)]
148+
if candidates:
149+
print(" Used '%s %s' instead of '%s %s'" % (alias, name_parts[-1], name_parts[0], name_parts[-1], ))
150+
151+
# Fixup for other exceptional cases
152+
153+
if person.ascii=="David Oran":
154+
candidates = ['oran-dave-th.jpg','oran-david.jpg']
155+
156+
if person.ascii=="Susan Hares":
157+
candidates = ['hares-sue-th.jpg','hares-susan.JPG']
158+
159+
if person.ascii=="Mahesh Jethanandani":
160+
candidates = ['Mahesh-Jethanandani-th.jpg','Jethanandani-Mahesh.jpg']
161+
162+
processed_files += [ c for c in candidates ]
163+
164+
if len(candidates) not in [0,1,2]:
165+
candidates = [x for x in candidates if not '00' in x]
166+
167+
if len(candidates) == 1:
168+
candidates = candidates + candidates
169+
170+
if len(candidates) not in [0,2]:
171+
thumb = [ c for c in candidates if '-th.' in c ][0]
172+
photo = [ c for c in candidates if '-th.' not in c ][0]
173+
trunc = [thumb, photo]
174+
print(" Truncating %s to %s" % (candidates, trunc))
175+
candidates = trunc
176+
177+
if candidates and '-th' in candidates[1]:
178+
candidates.reverse()
179+
180+
181+
# At this point we either have no candidates or two. If two, the first will be the thumb
182+
183+
def copy(old, new):
184+
if not os.path.exists(new):
185+
print("Copying "+old+" to "+new)
186+
shutil.copy(old, new)
187+
shutil.copystat(old, new)
188+
189+
if len(candidates)==2:
190+
old_name = candidates[1]
191+
old_thumb_name = candidates[0]
192+
old_name_ext = os.path.splitext(old_name)[1]
193+
old_thumb_name_ext = os.path.splitext(old_thumb_name)[1]
194+
195+
new_name = person.photo_name(thumb=False)+old_name_ext.lower()
196+
new_thumb_name = person.photo_name(thumb=True)+old_thumb_name_ext.lower()
197+
198+
copy( os.path.join(old_images_dir,old_name), os.path.join(new_images_dir,new_name) )
199+
200+
#
201+
copy( os.path.join(old_images_dir,old_thumb_name), os.path.join(new_images_dir,new_thumb_name) )
202+
203+
print("")
204+
not_processed = 0
205+
for file in pathlib.Path(old_images_dir).iterdir():
206+
if ( file.is_file()
207+
and not file.suffix.lower() in ['.txt', '.lck', '.html',]
208+
and not file.name.startswith('index.')
209+
and not file.name.startswith('milestoneupdate')
210+
and not file.name.startswith('nopicture')
211+
and not file.name.startswith('robots.txt')
212+
):
213+
if not file.name.decode('utf8') in processed_files:
214+
not_processed += 1
215+
print(u"Not processed: "+file.name.decode('utf8'))
216+
print("")
217+
print("")
218+
print("Not processed: %s files" % not_processed)

ietf/bin/2016-05-25-collect-photos.py

Lines changed: 0 additions & 121 deletions
This file was deleted.

0 commit comments

Comments
 (0)