-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDblpPython.py
130 lines (96 loc) · 4 KB
/
DblpPython.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
from __future__ import absolute_import, division, print_function, unicode_literals
import requests
import xmltodict, json
from difflib import SequenceMatcher
_DBLPSEARCH = 'http://dblp.uni-trier.de'
_AUTHSEARCH = '/search/author?xauthor='
_FULLSEARCH = '/pers/xx/'
_COAUTHSEARCH = '/pers/xc/'
_SESSION = requests.Session()
_HEADERS = {
'accept-language': 'pt-BR,pt',
'User-Agent': 'Mozilla/5.0',
'accept': 'text/html,application/xhtml+xml,application/xml',
}
class Author(object):
def __init__(self, __data):
self.urlPt = __data['@urlpt']
self.text = __data['#text']
self._filled = False
def fill(self):
resp_full_url = _SESSION.get(_DBLPSEARCH + _FULLSEARCH + self.urlPt, headers=_HEADERS)
resp_coauth_url = _SESSION.get(_DBLPSEARCH + _COAUTHSEARCH + self.urlPt, headers=_HEADERS)
author_dic = xmltodict.parse(resp_full_url.text)
coauthor_dic = xmltodict.parse(resp_coauth_url.text)
coauthor_dic = dict(coauthor_dic)
self.name = author_dic['dblpperson']['@name']
self.publications = list()
self.coauthors = list()
self.pubCount = len(author_dic['dblpperson']['r'])
self.coauthCount = len(author_dic['dblpperson']['coauthors']['co'])
publications = author_dic['dblpperson']['r']
for publication in publications:
pub = Publication(publication)
self.publications.append(pub)
coauthors = coauthor_dic['coauthors']['author']
for coauthor in coauthors:
coauth = CoAuthor(coauthor)
self.coauthors.append(coauth)
self.coauthors.sort(key=lambda x: x.count, reverse=True)
self._filled = True
return self
def __str__(self):
d = dict(self.__dict__)
if len(self.publications) > 0:
del d['publications']
if len(self.coauthors) > 0:
del d['coauthors']
return json.dumps(d, sort_keys=True, indent=4, separators=(',', ': '))
class Publication(object):
def __init__(self, __data):
for key in __data:
self.type = key
self.key = __data[key].get('@key', None)
self.date = __data[key].get('@mdate', None)
self.authors = list()
self.title = __data[key].get('@title', None)
self.pages = __data[key].get('pages', None)
self.year = __data[key].get('year', None)
self.volume = __data[key].get('volume', None)
self.journal = __data[key].get('journal', None)
self.booktitle = __data[key].get('booktitle', None)
self.url = __data[key].get('ee', None)
authors = __data[key]['author']
for author in authors:
self.authors.append(author)
def __str__(self):
d = dict(self.__dict__)
return json.dumps(d, sort_keys=True, indent=4, separators=(',', ': '))
def __repr__(self):
return json.dumps(self.__dict__, sort_keys=True, indent=4, separators=(',', ': '))
class CoAuthor(object):
def __init__(self, __data):
self.name = __data['#text']
self.count = int( __data['@count'])
self.urlpt = __data['@urlpt']
def __str__(self):
d = dict(self.__dict__)
return json.dumps(d, sort_keys=True, indent=4, separators=(',', ': '))
def __repr__(self):
return json.dumps(self.__dict__, sort_keys=True, indent=4, separators=(',', ': '))
def similar(a, b):
return SequenceMatcher(None, a, b).ratio()
def fill_author(name):
resp_url = _SESSION.get(_DBLPSEARCH+_AUTHSEARCH+name, headers=_HEADERS)
author_dic = xmltodict.parse(resp_url.content)
authors = author_dic['authors']['author']
selected_author = authors[0]
best_similarity = 0.0
for author in authors:
similarity = similar(author['#text'],name)
if similarity > best_similarity:
best_similarity = similarity
selected_author = author
selected_author = Author(selected_author)
selected_author.fill()
return selected_author