This repository has been archived by the owner on Apr 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
hosts.py
1057 lines (770 loc) · 31.5 KB
/
hosts.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
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
#!/usr/bin/python
# coding: utf-8
import os
import feedparser
from bs4 import BeautifulSoup as BS
from bs4 import SoupStrainer as SS
import requests
import arrow
from time import mktime
import re
# DEBUG
# from memory_profiler import profile
# Personal modules
import functions as fct
def reject(entry_title):
"""Function called by a Worker object to filter crappy entries.
It is meant to reject articles like corrigendum, erratum, etc"""
resource_dir, DATA_PATH = fct.getRightDirs()
# resource_dir = os.path.dirname(os.path.dirname(sys.executable))
# Load the regex stored in a config file, as filters
with open(os.path.join(resource_dir, 'config/regex.txt'),
'r', encoding='utf-8') as filters_file:
filters = filters_file.read().splitlines()
# Try to match the filters against the title entry
responses = [bool(re.search(regex, entry_title)) for regex in filters]
# If one filter matched, reject the entry
if True in responses:
return True
else:
return False
def refineUrl(company, journal, entry):
"""Refine an URL to avoid redirections (speeds things up)"""
url = getattr(entry, 'feedburner_origlink', entry.link)
if company == 'ACS':
id_paper = url.split('/')[-1]
url = "https://pubs.acs.org/doi/abs/10.1021/" + id_paper
elif company == 'Nature':
id_paper = url.split('/')[-1]
# Necessary. Sometimes the id has a point, sometimes not
p = re.compile(r'[0-9.]')
prefix = p.sub('', id_paper)
url = "http://www.nature.com/{}/journal/vaop/ncurrent/abs/{}.html"
url = url.format(prefix, id_paper)
elif company == 'Nature2':
id_paper = url.split('/')[-1].split('.')[0]
url = "http://www.nature.com/articles/{}".format(id_paper)
# Optimization for Wiley
elif company == 'Wiley':
doi = getDoi(company, journal, entry)
url = "https://onlinelibrary.wiley.com/doi/abs/{}"
url = url.format(doi)
return url
def updateData(company, journal, entry, care_image):
"""Function called by a Worker object when an RSS entry is
not in list_ok. The entry has so to be updated. This function
deals w/ the update, trough 2 booleans. If dl_image is True,
the Worker will try to dl the image, w/ the URL returned by this
function. If dl_page is True, the Worker will dl the article's page,
AND will try to dl the image anyway, trough the futures mechanism of
the worker."""
dl_image = True
dl_page = True
graphical_abstract = None
# NOTE:
# Some companies are not mentionned, but dl_image & dl_page are True
if company in ['Science', 'Thieme', 'PNAS']:
dl_page = False
elif company == 'RSC':
dl_page = False
if care_image:
soup = BS(entry.summary, "html.parser")
r = soup("img", align="center")
if r:
graphical_abstract = r[0]['src']
elif company == 'Wiley':
dl_page = False
if care_image:
soup = BS(entry.summary, "html.parser")
r = soup("a", attrs={"class": "figZoom"})
if r:
graphical_abstract = r[0].extract()
graphical_abstract = graphical_abstract['href']
elif company == 'ACS':
dl_page = False
if care_image:
soup = BS(entry.summary, "html.parser")
r = soup("img", alt="TOC Graphic")
if r:
graphical_abstract = r[0]['src']
elif company == 'Elsevier':
dl_page = False
if care_image:
if entry.summary != "":
soup = BS(entry.summary, "html.parser")
r = soup.find_all("img")
if r:
graphical_abstract = r[0]['src']
elif company == 'Beilstein':
dl_page = False
if care_image:
soup = BS(entry.summary, "html.parser")
r = soup.find_all("img")
if r:
graphical_abstract = r[0]['src']
elif company == 'PLOS':
dl_page = False
if care_image:
base = "http://journals.plos.org/plosone/article/figure/image?size=medium&id=info:{}.g001"
graphical_abstract = base.format(getDoi(company, journal, entry))
else:
pass
if graphical_abstract is None:
dl_image = False
return dl_page, dl_image, {'graphical_abstract': graphical_abstract}
# @profile
def getData(company, journal, entry, response=None):
"""Get the data. Starts from the data contained in the RSS page and, if
necessary, parses the website for additional information"""
url = refineUrl(company, journal, entry)
# If the journal is edited by the RSC
if company == 'RSC':
"""Graphical abstract present in RSS. Abstract incomplete
and w/out html. Title w/out html"""
title = entry.title
date = arrow.get(entry.updated).format('YYYY-MM-DD')
abstract = None
graphical_abstract = None
author = None
soup = BS(entry.summary, "html.parser")
r = soup("img", align="center")
if r:
graphical_abstract = r[0]['src']
if response.status_code is requests.codes.ok:
# # Get the title (w/ html)
# Strainer: get a soup with only the interesting part.
# Don't load the complete tree in memory. Saves RAM
strainer = SS("h2", attrs={"class": "capsule__title fixpadv--m"})
soup = BS(response.text, "html.parser", parse_only=strainer)
title = soup.h2
if title is not None:
title = title.renderContents().decode().strip()
# Get the abstrat (w/ html)
strainer = SS("p", xmlns="http://www.rsc.org/schema/rscart38")
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.p
if r is not None:
abstract = r.renderContents().decode()
if abstract == "":
abstract = None
strainer = SS("meta", attrs={"name": "citation_author"})
soup = BS(response.text, "html.parser", parse_only=strainer)
# Here, multiple tags (results) are expected, so perform
# the search, even if the tree contains only the result
r = soup("meta", attrs={"name": "citation_author"})
if r:
author = [tag['content'] for tag in r]
author = ", ".join(author)
elif company == 'Wiley':
title, date, author, abstract, graphical_abstract = parseWiley(entry, response)
elif company == 'ACS':
"""Feed only contains graphical abstract"""
title = entry.title.rstrip()
date = arrow.get(mktime(entry.published_parsed)).format('YYYY-MM-DD')
abstract = None
author = entry.author
author = entry.author.split(" and ")
if len(author) > 1:
author = ", ".join(author)
else:
author = author[0]
graphical_abstract = None
soup = BS(entry.summary, "html.parser")
r = soup("img", alt="TOC Graphic")
if r:
graphical_abstract = r[0]['src']
# If the dl went wrong, print an error
if response.status_code is requests.codes.ok:
strainer = SS("p", attrs={"class": "articleBody_abstractText"})
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.p
if r is not None:
abstract = r.renderContents().decode()
strainer = SS("h1", attrs={"class": "articleTitle"})
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.h1
if r is not None:
title = r.renderContents().decode()
elif company == 'Nature':
title = entry.title
date = entry.date
abstract = None
graphical_abstract = None
author = None
try:
if entry.authors:
author = []
for element in entry.authors:
author.append(element['name'])
author = ", ".join(author)
except AttributeError:
pass
if entry.summary:
abstract = BS(entry.summary, "html.parser")
while abstract.find_all('p'):
_ = abstract.p.extract()
try:
_ = abstract.img.extract()
except AttributeError:
pass
abstract = abstract.renderContents().decode()
if (response.status_code is requests.codes.ok or
response.status_code == 401):
strainer = SS("div", attrs={"class": "article__body serif cleared"})
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.div
try:
abstract = r.text
except AttributeError:
pass
strainer = SS("figure")
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.find_all("img", attrs={"class": "figure__image"})
if r:
# Additional verification to correctly forge the URL
graphical_abstract = "http:" + r[0]["src"]
elif company == 'Science':
title = entry.title
date = entry.date
graphical_abstract = None
if entry.author:
author = entry.author
else:
author = None
abstract = entry.summary
if not abstract:
abstract = None
elif company == 'PNAS':
title = entry.title
date = entry.prism_publicationdate
graphical_abstract = None
author = None
abstract = None
if response.status_code is requests.codes.ok:
# Get the correct title, not the one in the RSS
strainer = SS("h1", id="article-title-1")
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.find_all("h1", id="article-title-1")
if r:
title = r[0].renderContents().decode()
# Get the authors
strainer = SS("a", attrs={"class": "name-search"})
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.find_all("a", attrs={"class": "name-search"})
if r:
author = [tag.text for tag in r]
author = ", ".join(author)
# Try to get the complete abstract. Sometimes it's available,
# sometimes the article only contains an extract
strainer = SS("div", attrs={"class": "section abstract"})
soup = BS(response.text, "html.parser", parse_only=strainer)
if soup.p is not None:
abstract = soup.p.renderContents().decode()
else:
abstract = entry.summary
elif company == 'Elsevier':
title = entry.title
date = arrow.get(mktime(entry.updated_parsed)).format('YYYY-MM-DD')
graphical_abstract = None
author = None
abstract = entry.summary
if abstract:
try:
author = abstract.split("Author(s): ")[1].split("<br")[0].split("<")[0]
author = author.replace(" , ", ", ")
author = author.replace(" ", " ")
except IndexError:
author = None
soup = BS(abstract, "html.parser")
try:
# First type of abstract formatting
abstract = soup("simple-para")[0].renderContents().decode()
except IndexError:
try:
# Second type of abstract formatting
abstract = abstract.split("<br />")[3].lstrip()
except IndexError:
abstract = None
r = soup.find_all("img")
if r:
graphical_abstract = r[0]['src']
# NOTE: javascript embedded, impossible
# if response.status_code is requests.codes.ok:
# url = response.url
# print(response.url)
# # Get the abstract
# soup = BS(response.text)
# Get the correct title, no the one in the RSS
# r = soup.find_all("li", attrs={"class": "originalArticleName"})
# print(r)
# if r:
# title = r[0].renderContents().decode()
elif company == 'Thieme':
title = entry.title
date = arrow.get(entry.updated).format('YYYY-MM-DD')
abstract = None
graphical_abstract = None
author = None
try:
if entry.authors:
author = []
for element in entry.authors:
# Reverse Family name/first name
field = reversed(element['name'].split(', '))
name = " ".join(field)
author.append(name)
author = ", ".join(author)
except AttributeError:
pass
try:
if entry.summary:
abstract = entry.summary
except AttributeError:
pass
elif company == 'Beilstein':
title = entry.title
date = arrow.get(mktime(entry.published_parsed)).format('YYYY-MM-DD')
abstract = None
graphical_abstract = None
author = entry.author
author = entry.author.split(" and ")
if len(author) > 1:
author = ", ".join(author)
else:
author = author[0]
if entry.summary != "":
soup = BS(entry.summary, "html.parser")
r = soup.find_all("p")
if r:
abstract = r[1].renderContents().decode()
r = soup.find_all("img")
if r:
# This company can change the background of the GA through
# the url. If nothing is done, the bg is black, so turn it
# to white. Doesn't affect images with unchangeable bg
graphical_abstract = r[0]['src'] + '&background=FFFFFF'
elif company == 'Nature2':
title = entry.title
date = entry.date
abstract = entry.summary
graphical_abstract = None
try:
author = [dic['name'] for dic in entry.authors]
if author:
if len(author) > 1:
author = ", ".join(author)
else:
author = author[0]
else:
author = None
except AttributeError:
author = None
if response.status_code is requests.codes.ok or response.status_code == 401:
strainer = SS("h1", attrs={"class": "tighten-line-height small-space-below"})
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.h1
if r is not None:
title = r.renderContents().decode()
strainer = SS("div", attrs={"id": "abstract-content"})
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.p
if r is not None:
abstract = r.renderContents().decode()
strainer = SS("img")
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.find_all("img", attrs={"alt": "Figure 1"})
if r:
if "f1.jpg" in r[0]["src"]:
graphical_abstract = "http://www.nature.com" + r[0]["src"]
elif company == 'PLOS':
title = entry.title
date = arrow.get(mktime(entry.published_parsed)).format('YYYY-MM-DD')
author = None
try:
if entry.authors:
author = []
for element in entry.authors:
author.append(element['name'])
author = ", ".join(author)
except AttributeError:
pass
abstract = BS(entry.summary, "html.parser")
# Clean the authors' names from the abstract
r = abstract.find_all("p")
if r and str(r[0]).startswith("<p>by "):
abstract("p")[0].extract()
try:
abstract("img")[0].extract()
except IndexError:
pass
abstract = abstract.renderContents().decode().strip()
base = "http://journals.plos.org/plosone/article/figure/image?size=medium&id=info:doi/{}.g001"
graphical_abstract = base.format(getDoi(company, journal, entry))
elif company == 'Springer':
title = entry.title
date = arrow.get(mktime(entry.published_parsed)).format('YYYY-MM-DD')
graphical_abstract = None
author = None
abstract = BS(entry.summary, "html.parser")
try:
_ = abstract("h3")[0].extract()
# Remove the graphical abstract part from the abstract
_ = abstract("span", attrs={"class": "a-plus-plus figure category-standard float-no id-figa"})[0].extract()
except IndexError:
pass
abstract = abstract.renderContents().decode().strip()
if response.status_code is requests.codes.ok:
strainer = SS("div", attrs={"class": "MediaObject"})
soup = BS(response.text, "html.parser", parse_only=strainer)
# For now, it's one shot: if the dl fails for the GA, there
# won't be a retry. That's bc too little articles have GA
r = soup.find_all("img")
if r:
graphical_abstract = r[0]['src']
strainer = SS("ul", attrs={"class": "AuthorNames"})
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.find_all("span", attrs={"class": "AuthorName"})
if r:
author = [tag.text for tag in r]
author = ", ".join(author)
strainer = SS("h1", attrs={"class": "ArticleTitle"})
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.h1
if r is not None:
title = r.renderContents().decode()
elif company == 'Springer_open':
title = entry.title
date = arrow.get(mktime(entry.published_parsed)).format('YYYY-MM-DD')
graphical_abstract = None
author = None
abstract = BS(entry.summary, "html.parser")
try:
_ = abstract("h3")[0].extract()
# Remove the graphical abstract part from the abstract
_ = abstract("span", attrs={"class": "a-plus-plus figure category-standard float-no id-figa"})[0].extract()
except IndexError:
pass
abstract = abstract.renderContents().decode().strip()
if response.status_code is requests.codes.ok:
strainer = SS("div", attrs={"class": "MediaObject"})
soup = BS(response.text, "html.parser", parse_only=strainer)
# For now, it's one shot: if the dl fails for the GA, there
# won't be a retry. That's bc too little articles have GA
r = soup.find_all("img")
if r:
graphical_abstract = r[0]['src']
strainer = SS("ul", attrs={"class": "u-listReset"})
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.find_all("span", attrs={"class": "AuthorName"})
if r:
author = [tag.text for tag in r]
author = ", ".join(author)
strainer = SS("h1", attrs={"class": "ArticleTitle"})
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.h1
if r is not None:
title = r.renderContents().decode()
elif company == 'Taylor':
title = entry.title
date = arrow.get(mktime(entry.updated_parsed)).format('YYYY-MM-DD')
graphical_abstract = None
author = None
abstract = None
try:
author = []
for element in entry.authors:
author.append(element['name'])
author = ", ".join(author)
except AttributeError:
author = None
if response.status_code is requests.codes.ok:
strainer = SS("div", attrs={"class": "col-md-2-3 "})
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.span
if r is not None:
# Remove all tags attributes
for tag in r.findAll(True):
tag.attrs = None
title = r.renderContents().decode()
strainer = SS("div",
attrs={"class": "abstractSection abstractInFull"})
soup = BS(response.text, "html.parser", parse_only=strainer)
# Erase the title 'Abstract', useless
if soup("p") and soup("p")[0].text == "Abstract":
soup("p")[0].extract()
r = soup.p
if r is not None:
abstract = r.renderContents().decode()
r = soup.find_all("img")
if r:
base = "http://www.tandfonline.com{}"
graphical_abstract = base.format(r[0]['src'])
elif company == 'ChemArxiv':
title = entry.title
date = arrow.get(mktime(entry.published_parsed)).format('YYYY-MM-DD')
graphical_abstract = None
author = None
abstract = None
try:
if entry.authors:
author = []
for element in entry.authors:
author.append(element['name'])
author = ", ".join(author)
except AttributeError:
pass
try:
abstract = entry.summary
except AttributeError:
# I saw once a poster conference, w/ no abstract.
# Filter these entries if it becomes common
pass
elif company == 'ChemRxiv':
title = entry.title
date = arrow.get(mktime(entry.published_parsed)).format('YYYY-MM-DD')
graphical_abstract = None
author = None
abstract = None
try:
abstract = entry.summary
except AttributeError:
# I saw once a poster conference, w/ no abstract.
# Filter these entries if it becomes common
pass
if response.status_code is requests.codes.ok:
pass
strainer = SS("span", attrs={"class": "authors-holder"})
soup = BS(response.text, "html.parser", parse_only=strainer)
r = soup.find_all("a", attrs={"class": "normal-link author"})
if r:
author = [tag.text.strip() for tag in r]
author = ", ".join(author)
else:
return None
if title is None:
return None
topic_simple = forgeTopicSimple(title, abstract)
if abstract is None or abstract == '':
abstract = "Empty"
if graphical_abstract is None:
graphical_abstract = "Empty"
if author is None or author == '':
author = "Empty"
author_simple = None
else:
# Clean author field
author = author.replace(' ', ' ')
author = author.replace(' ,', ',')
author_simple = " " + fct.simpleChar(author) + " "
return title, date, author, abstract, graphical_abstract, url, topic_simple, author_simple
def parseWiley(entry: feedparser.util.FeedParserDict,
response: requests.models.Response):
"""
Feed compltete, no graphical abstract. No need to parse article web page
"""
title = entry.title
date = arrow.get(entry.updated).format('YYYY-MM-DD')
author = entry.author
graphical_abstract = None
abstract = None
if entry.summary:
abstract = entry.summary.strip("Abstract\n")
abstract = abstract.strip()
authors = list()
# Parsing author field, removing titles, etc
for name in author.split(","):
name = name.strip()
name = name.replace("\n", " ")
name = name.split("Dr. ")[-1]
name = name.split("Prof. ")[-1]
authors.append(name)
author = ", ".join(authors)
data = (title, date, author, abstract, graphical_abstract)
return data
def forgeTopicSimple(title: str, abstract: str) -> str:
"""
Forge topic_simple, a simplified version of the abstract, used for
sqlite queries
"""
simple_title = fct.simpleChar(title)
if abstract is not None:
simple_abstract = fct.simpleChar(BS(abstract, "html.parser").text)
topic_simple = " " + simple_abstract + " " + simple_title + " "
else:
topic_simple = " " + simple_title + " "
return topic_simple
def getDoi(company, journal, entry):
"""Get the DOI id of a post, to save time"""
if company == 'RSC':
soup = BS(entry.summary, "html.parser")
r = soup("div")
try:
doi = r[0].text.split("DOI: ")[1].split(",")[0]
except IndexError:
doi = r[1].text.split("DOI:")[1].split(",")[0]
elif company in ['Wiley', 'Nature', 'Nature2', 'Thieme', 'Taylor']:
doi = entry.prism_doi
elif company == 'ACS':
doi = entry.id.split("dx.doi.org/")[1]
elif company == 'Science':
doi = "10.1126/science." + entry.prism_endingpage[1:]
elif company == 'PNAS':
base = entry.dc_identifier
base = base.split("pnas;")[1]
doi = "10.1073/pnas." + base
# For this publisher, the doi is not given
# in the RSS flux. It's so replaced by the url
elif company == 'Elsevier':
doi = entry.id
elif company == 'Beilstein':
doi = entry.summary.split("doi:")[1].split("</p>")[0]
elif company == 'PLOS':
doi = "10.1371/" + entry.id.split('/')[-1]
elif company == 'Springer' or company == 'Springer_open':
doi = "10.1007/" + entry.id.split('/')[-1]
# Chemrxiv doesn't assign DOIs to articles. Use the id/url
elif company == 'ChemArxiv':
doi = entry.id
# Chemrxiv doesn't assign DOIs to articles. Use the id/url
elif company == 'ChemRxiv':
doi = entry.link
try:
doi = doi.replace(" ", "")
except UnboundLocalError:
print("Erreur in getDoi: {0}".format(journal))
return None
return doi
def getJournals(company, user=False):
"""Function to get the informations about all the journals of
a company. Returns the names, the URLs, the abbreviations, and also
a boolean to set the download of the graphical abstracts. If for a
journal this boolean is False, the Worker object will not try to dl
the picture. If user is true, returns only the journals on the user's
side"""
names = []
abb = []
urls = []
cares_image = []
resource_dir, DATA_PATH = fct.getRightDirs()
if not user:
with open(os.path.join(resource_dir, 'journals', '{}.ini'.
format(company)), 'r', encoding='utf-8') as config:
for line in config:
names.append(line.split(" : ")[0])
abb.append(line.split(" : ")[1].rstrip())
urls.append(line.split(" : ")[2].rstrip())
# Get a bool: care about the image when refreshing
try:
care = line.split(" : ")[3].rstrip()
if care == "False":
cares_image.append(False)
else:
cares_image.append(True)
except IndexError:
cares_image.append(True)
# If the user didn't add a journal from this company, the file
# won't exist, so exit
if not os.path.exists(os.path.join(DATA_PATH, 'journals', '{}.ini'.
format(company))):
return names, abb, urls, cares_image
with open(os.path.join(DATA_PATH, 'journals', '{}.ini'.
format(company)), 'r', encoding='utf-8') as config:
for line in config:
url = line.split(" : ")[2].rstrip()
if url not in urls:
names.append(line.split(" : ")[0])
abb.append(line.split(" : ")[1].rstrip())
urls.append(url)
# Get a bool: care about the image when refreshing
try:
care = line.split(" : ")[3].rstrip()
if care == "False":
cares_image.append(False)
else:
cares_image.append(True)
except IndexError:
cares_image.append(True)
return names, abb, urls, cares_image
def getCompanies(user: bool=False) -> list:
"""
Get a list of all the companies. Will return a list of publishers, without
.ini at the end. If user is true, returns companies on the user's side
"""
resource_dir, DATA_PATH = fct.getRightDirs()
cb_companies = []
# List companies on the program side
for company in os.listdir(os.path.join(resource_dir, 'journals')):
company = company.split('.')[0]
cb_companies.append(company)
if not user:
return list(set(cb_companies))
user_companies = []
# List companies on the user side
for company in os.listdir(os.path.join(DATA_PATH, 'journals')):
company = company.split('.')[0]
user_companies.append(company)
return list(set(cb_companies + user_companies))
if __name__ == "__main__":
# print(getJournals('Science'))
from requests_futures.sessions import FuturesSession
import functools
from pprint import pprint
import webbrowser
COMPANY = 'Wiley'
def print_result(journal, entry, future):
response = future.result()
title, date, authors, abstract, graphical_abstract, url, topic_simple, author_simple = getData(COMPANY, journal, entry, response)
# print("\n")
print("Abstract:\n", abstract)
print("Date:", date)
# print("\n")
print("Title:", title)
print("Authors:", authors)
# print("\n")
print("\n")
print(graphical_abstract)
# os.remove("graphical_abstracts/{0}".format(fct.simpleChar(graphical_abstract)))
# print("\n")
# urls_test = ["http://www.tandfonline.com/action/showFeed?type=etoc&feed=rss&jc=gsch20"]
urls_test = ["https://onlinelibrary.wiley.com/action/showFeed?jc=15222683&type=etoc&feed=rss"]
session = FuturesSession(max_workers=20)
list_urls = []
feed = feedparser.parse(urls_test[0], timeout=20)
# print(feed.entries)