venue.py 5.73 KB
Newer Older
Sebastian Baltes's avatar
Sebastian Baltes committed
1
import logging
Sebastian Baltes's avatar
Sebastian Baltes committed
2
3
import re

Sebastian Baltes's avatar
Sebastian Baltes committed
4
5
import requests

6
from lxml import html
Sebastian Baltes's avatar
Sebastian Baltes committed
7
8
9
10
11
12
from dblp.paper import Paper

logger = logging.getLogger("dblp-retriever_logger")


class Venue(object):
13
    """ A venue on DBLP. """
Sebastian Baltes's avatar
Sebastian Baltes committed
14
15
16
17
18

    def __init__(self, name, year, identifier):
        self.name = str(name)
        self.year = str(year)
        self.identifier = str(identifier)
19

Sebastian Baltes's avatar
Sebastian Baltes committed
20
21
22
23
24
25
26
27
28
29
30
31
32
        self.uri = "https://dblp.org/db/" + self.identifier + ".html"

        self.papers = []

        # session for data retrieval
        self.session = requests.Session()

    def retrieve_papers(self):
        try:
            # retrieve data
            response = self.session.get(self.uri)

            if response.ok:
33
                logger.info("Successfully retrieved TOC of venue: " + str(self))
Sebastian Baltes's avatar
Sebastian Baltes committed
34
35
36
37
38
39
40
41

                tree = html.fromstring(response.content)
                items = tree.xpath('//header[not(@class)]/h2 | //header[not(@class)]/h3 | //ul[@class="publ-list"]/li')

                current_heading = ""

                for item in items:
                    if item.tag == "h2" or item.tag == "h3":
42
43
44
45
46
47
                        heading = item.xpath("descendant-or-self::*/text()")
                        if len(heading) > 0:
                            current_heading = re.sub(r'\s+', ' ',  # unify whitespaces/ remove newlines
                                                     str(" ".join(str(element).strip() for element in heading)))
                        else:
                            current_heading = ""
Sebastian Baltes's avatar
Sebastian Baltes committed
48
49
50
51
52
53
54
55
56
57
                    elif item.tag == "li":
                        if current_heading == "":
                            # the following only works for conferences, not for journals
                            # year = item.xpath('div[@class="data"]/span[@itemprop="datePublished"]/text()')
                            # if len(year) > 0:
                            #     year = str(year[0])
                            # else:
                            #     year = ""
                            continue

Gustavo Pinto's avatar
Gustavo Pinto committed
58
                        title = item.xpath('cite[@class="data"]/span[@itemprop="name"]/descendant-or-self::*/text()')
Sebastian Baltes's avatar
Sebastian Baltes committed
59
                        if len(title) > 0:
60
                            title = str(" ".join(str(element).strip() for element in title))
Sebastian Baltes's avatar
Sebastian Baltes committed
61
62
63
                        else:
                            title = ""

Gustavo Pinto's avatar
Gustavo Pinto committed
64
                        pages = item.xpath('cite[@class="data"]/span[@itemprop="pagination"]/text()')
Sebastian Baltes's avatar
Sebastian Baltes committed
65
66
67
68
69
70
71
                        if len(pages) > 0:
                            pages = str(pages[0])
                        else:
                            pages = ""

                        ee = item.xpath('nav[@class="publ"]/ul/li[@class="drop-down"]/div[@class="head"]/a/@href')
                        if len(ee) > 0:
72
73
74
75
76
77
78
                            # select DOI link if available, first link otherwise
                            doi_link = [link for link in ee if "doi.org" in link]
                            if len(doi_link) > 0:
                                ee = str(doi_link[0])
                            else:
                                ee = str(ee[0])

Sebastian Baltes's avatar
Sebastian Baltes committed
79
80
81
                        else:
                            ee = ""

Gustavo Pinto's avatar
Gustavo Pinto committed
82
                        authors = item.xpath('cite[@class="data"]/span[@itemprop="author"]/a/span[@itemprop="name"]'
83
                                             '/text()')
Sebastian Baltes's avatar
Sebastian Baltes committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
                        if len(authors) == 1:
                            authors = str(authors[0])
                        else:
                            authors = "; ".join(authors)

                        self.papers.append(Paper(
                            self.name,
                            self.year,
                            self.identifier,
                            current_heading,
                            title,
                            authors,
                            pages,
                            ee
                        ))

100
                logger.info("Successfully parsed TOC of venue: " + str(self))
Sebastian Baltes's avatar
Sebastian Baltes committed
101
            else:
102
                logger.error("An error occurred while retrieving TOC of venue: " + str(self))
Sebastian Baltes's avatar
Sebastian Baltes committed
103
104

        except ConnectionError:
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
            logger.error("An error occurred while retrieving TOC of venue: " + str(self))

    def validate_page_ranges(self):
        logger.info("Sorting papers of venue: " + str(self))

        self.papers.sort(key=lambda p: p.first_page)

        logger.info("Validating page ranges of venue: " + str(self))

        if len(self.papers) < 2:
            return

        previous_paper = self.papers[0]
        for i in range(1, len(self.papers)):
            current_paper = self.papers[i]

            if current_paper.page_range == "" or previous_paper.page_range == "":
                previous_paper = self.papers[i]
                continue

            if current_paper.regular_page_range and current_paper.first_page != previous_paper.last_page + 1:
                current_paper.append_comment("issue_first_page")
                previous_paper.append_comment("issue_last_page")
                logger.warning("First page of paper " + str(current_paper) + " does not match previous paper "
                               + str(previous_paper))

            elif current_paper.numbered_page_range and current_paper.article_number != previous_paper.article_number + 1:
                current_paper.append_comment("issue_article_number")
                previous_paper.append_comment("issue_article_number")
                logger.warning("Article number of paper " + str(current_paper) + " does not match previous paper "
                               + str(previous_paper))

            previous_paper = self.papers[i]
Sebastian Baltes's avatar
Sebastian Baltes committed
138
139
140
141
142
143

    def get_rows(self):
        rows = []
        for paper in self.papers:
            rows.append(paper.get_column_values())
        return rows
144
145
146

    def __str__(self):
        return str(self.identifier)