-
Notifications
You must be signed in to change notification settings - Fork 0
/
FamFetcher.txt
366 lines (357 loc) · 22.8 KB
/
FamFetcher.txt
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
#!/usr/bin/env python3
Usage = """
Ctf-8,oding: u end of line character is \\n, one indent is 4 spaces
Created on Mon Jul 13 12:57:47 2020
Script purpose: Retrieves the species data for fish from the family to species level from the Califorina Academy
of sciences and compiles the data into a csv file.
Version 1.01
@author: Iván Raúl López Martínez
Script improvements:
fexibility to interface between command line and input to define variables
Option or ability to read family names from a file
Usage:
FamFetcher.py 1 2 3
1 - Read the names from a file Yes/No, if No or = 0 provide input()
2 - Full path to destination directory (path), if = 0 provide input()
3 - Remove raw file Yes/No, if = 0 provide input ()
"""
import mechanicalsoup, re, os, sys, time#, gspread
from bs4 import BeautifulSoup
#from oauth2client.service_account import ServiceAccountCredentials
from datetime import date
def AuthorCorrector(AuthorGroup):
AuthorStr = 'BÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Böhlke'
return AuthorName
else:
AuthorStr = 'Rüppell'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Rüppell'
return AuthorName
else:
AuthorStr = 'Günther'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Günther'
return AuthorName
else:
AuthorStr = 'ThiolliÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Thiollière'
return AuthorName
else:
AuthorStr = 'LacepÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Lacepède'
return AuthorName
else:
AuthorStr = 'Forsskål'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Forsskål'
return AuthorName
else:
AuthorStr = 'DÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Döderlein'
return AuthorName
else:
AuthorStr = 'Liénard'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Liénard'
return AuthorName
else:
AuthorStr = 'Quéro'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Quéro'
return AuthorName
else:
AuthorStr = 'Müller'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Müller'
return AuthorName
else:
AuthorStr = 'LÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Lönnberg'
return AuthorName
else:
AuthorStr = 'LarraÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Larrañaga'
return AuthorName
else:
AuthorStr = 'KrÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Krøyer'
return AuthorName
else:
AuthorStr = 'GourÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Gourène'
return AuthorName
else:
AuthorStr = 'SchreitmÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Schreitmüller'
return AuthorName
else:
AuthorStr = 'HankÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Hankó'
return AuthorName
else:
AuthorStr = 'GüldenstÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Güldenstädt'
return AuthorName
else:
AuthorStr = 'HoÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Hoàng'
return AuthorName
else:
AuthorStr = 'Brüning'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Brüning'
return AuthorName
else:
AuthorStr = 'Määr'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Määr'
return AuthorName
else:
AuthorStr = 'BÄnÄrescu'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Bănărescu'
return AuthorName
else:
AuthorStr = 'KÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Küçük'
return AuthorName
else:
AuthorStr = 'AlmaÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Almaça'
return AuthorName
else:
AuthorStr = 'EstÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Estève'
return AuthorName
else:
AuthorStr = 'TsÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Tsü'
return AuthorName
else:
AuthorStr = 'Schäfer'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Schäfer'
return AuthorName
else:
AuthorStr = 'Guimarães'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Guimarães'
return AuthorName
else:
AuthorStr = 'GüÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Güçlü'
return AuthorName
else:
AuthorStr = 'KotlÃ'
AuthorResult = re.search(AuthorStr, AuthorGroup)
if AuthorResult:
AuthorName = 'Kotlík'
return AuthorName
else:
AuthorName = AuthorGroup
return AuthorName
def StatusFinder(CurrentStatus):#fuction to find stings in CAS format txt files
SearchStr = '^([a-z]+), ([A-Z][a-z]+)[A-z\s\.\(\)]*? ([A-Z])(.*?)([A-z\s]*) \[?.+Current status: (\w+) (\w+) (\w+) (\w+).+'
Result = re.search(SearchStr, CurrentStatus)
if Result:
Species = Result.group(1)#each captured result is saved to a variable
Genus = Result.group(2)
RawAuthor = Result.group(3) + Result.group(4) + Result.group(5)
Author = AuthorCorrector(RawAuthor)
Validity = Result.group(6)
Preposition = Result.group(7)
VGenus = Result.group(8)
VSpecies = Result.group(9)
NameStatus = Genus + '\t' + Species + '\t' + Validity + ' ' + Preposition + ' ' + VGenus + ' ' + VSpecies + '\t' + Author
return NameStatus
else:
SearchStr = '^([a-z]+), ([A-Z][a-z]+)[A-z\s\.\(\)]*? ([A-Z])(.*?)([A-z\s]*) \[?.+Status uncertain.+'
Result = re.search(SearchStr, CurrentStatus)
if Result:
Species = Result.group(1)
Genus = Result.group(2)
RawAuthor = Result.group(3) + Result.group(4) + Result.group(5)
Author = AuthorCorrector(RawAuthor)
Status = 'Status uncertain'
NameStatus = Genus + '\t' + Species + '\t' + Status + '\t' + Author
return NameStatus
else:
SearchStr = '^([a-z]+), ([A-Z][a-z]+)[A-z\s\.\(\)]*? ([A-Z])(.*?)([A-z\s]*) \[?.+species currently recognized as valid.+'
Result = re.search(SearchStr, CurrentStatus)
if Result:
Species = Result.group(1)
Genus = Result.group(2)
RawAuthor = Result.group(3) + Result.group(4) + Result.group(5)
Author = AuthorCorrector(RawAuthor)
Status = 'Species currently recognized as valid'
NameStatus = Genus + '\t' + Species + '\t' + Status + '\t' + Author
return NameStatus
else:
SearchStr = '^([a-z]+), ([A-Z][a-z]+)[A-z\s\.\(\)]*? ([A-Z])(.*?)([A-z\s]*) \[?.+Family uncertain.+'
Result = re.search(SearchStr, CurrentStatus)
if Result:
Species = Result.group(1)
Genus = Result.group(2)
RawAuthor = Result.group(3) + Result.group(4) + Result.group(5)
Author = AuthorCorrector(RawAuthor)
Status = 'Family uncertain'
NameStatus = Genus + '\t' + Species + '\t' + Status + '\t' + Author
return NameStatus
else:
SearchStr = '^([a-z]+), ([A-Z][a-z]+)[A-z\s\.\(\)]*? ([A-Z])(.*?)([A-z\s]*) \[?.+Incertae sedis.+'
Result = re.search(SearchStr, CurrentStatus)
if Result:
Species = Result.group(1)
Genus = Result.group(2)
RawAuthor = Result.group(3) + Result.group(4) + Result.group(5)
Author = AuthorCorrector(RawAuthor)
Status = 'Incertae sedis'
NameStatus = Genus + '\t' + Species + '\t' + Status + '\t' + Author
return NameStatus
else:
SearchStr = '^([a-z]+), ([A-Z][a-z]+)[A-z\s\.\(\)]*? ([A-Z])(.*?)([A-z\s]*) \[?(.+)'
Result = re.search(SearchStr, CurrentStatus)
if Result:
Species = Result.group(1)
Genus = Result.group(2)
RawAuthor = Result.group(3) + Result.group(4) + Result.group(5)
Author = AuthorCorrector(RawAuthor)
Status = Result.group(6)
NameStatus = Genus + '\t' + Species + '\t' + Status + '\t' + Author
return NameStatus
else:
CurrentStatus = "It’s time to start exploring."
return CurrentStatus
'''
if len(sys.argv)<3:
print(Usage)
else:
for MyArg in sys.argv:
print(MyArg)
ScriptName = sys.argv[0]
if sys.argv[1] == 'True':
Debug = True
else:
Debug = False
if sys.argv[2] == 'True':
WriteOutFile = True
else:
WriteOutFile = False
GoogleFile = input('Enter the name of the google sheet with the list:')#Albatross_Species_and_localities
LastFish = input('Enter the row number of the last species:')#124
Scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
Creds = ServiceAccountCredentials.from_json_keyfile_name('/Users/ivanlopez/API_Keys/REUapplication.json', Scope)#sys.argv
Client = gspread.authorize(Creds)
Sheet = Client.open(GoogleFile).sheet1 #or sys.argv[] in place of hardcoded file or input
SpeciesTotal = range(2,LastFish)#sys.argv[3] in pace of end of range number
Rownumber = 0
for Num in SpeciesTotal:
if Rownumber > 1:
print(Num)
Rownumber += 1
if sys.argv[1] == None:
Family = input('Enter the name of the family:')# apogonidae
else:
GoogleFile = input('Enter the name of the google sheet with the list:')
if sys.argv[2] == None:
PathName = input('Enter the full pathway for the working directory: ')# /Users/ivanlopez/Desktop/test/
if sys.argv[3] == None:
Keep = input('Do you want to keep the raw file(YES/NO):')
Keeper = Keep.upper()
if Keeper == 'YES':
Keeper = False
else:
Keeper = True
'''
print(Usage)
ScriptName = sys.argv[0]
LineNumber = 0
Keep = input('Do you want to keep the raw file(YES/NO):')
Keeper = Keep.upper()
if Keeper == 'YES':
Keeper = False
else:
Keeper = True
Family = input('Enter the name of the family:')# apogonidae
PathName = input('Enter the full pathway for the working directory: ')# /Users/ivanlopez/Desktop/Daily_Work/Practical_computing/test/
StartTime = time.time()
Today = date.today()
Day = Today.strftime('%b-%d-%Y')
os.chdir(PathName)
RawFileName = Family + 'raw.txt'
RawFile = open(RawFileName, 'w')
Browser = mechanicalsoup.StatefulBrowser()
Browser.open("http://researcharchive.calacademy.org/research/ichthyology/catalog/fishcatmain.asp")
Browser.select_form()
Browser.get_current_form()#.print_summary()
Browser["contains"] = Family
Response = Browser.submit_selected()
Soup = BeautifulSoup(Response.text, 'html.parser')
RawFile.write(Soup.get_text())
Lines = open(RawFileName).readlines()
open(RawFileName, 'w').writelines(Lines[87:-1])
Header = 'Genus\tSpecies\tCurrent Status(Genus Species)\tAuthor'
OutcsvName = Family + Day + '.csv'
Outcsv = open(OutcsvName, 'w')
Outcsv.write(Header + '\n')
RowNumber = 0
with open(RawFileName, mode= 'r') as LastFile:
for Row in LastFile: #Loop through each line in the file
if not len(Row.strip())==0:
Row = Row.strip('\n')# remove end of line
ElementRow = StatusFinder(Row)# applies the defined function
if ElementRow == "It’s time to start exploring.":
break
Outcsv.write(ElementRow + '\n')#Writes the line to the file
RowNumber += 1#adds a row and loops to end
Outcsv.close()
if Keeper:
os.remove(RawFileName)# removes intermediate files
sys.stderr.write("I have finished this script: %s\n" % ScriptName)
print('Elapsed: %.5f' %(time.time() - StartTime))