Skip to content

Commit 2608e43

Browse files
author
Xavier Grangier
committed
grangier#188 - move tweet extraction to TweetExtractor class
1 parent 12dfda5 commit 2608e43

3 files changed

Lines changed: 50 additions & 16 deletions

File tree

goose/crawler.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from goose.extractors.title import TitleExtractor
3131
from goose.extractors.images import ImageExtractor
3232
from goose.extractors.links import LinksExtractor
33+
from goose.extractors.tweets import TweetsExtractor
3334
from goose.cleaners import StandardDocumentCleaner
3435
from goose.outputformatters import StandardOutputFormatter
3536

@@ -66,6 +67,9 @@ def __init__(self, config):
6667
# init the output formatter
6768
self.formatter = self.get_formatter()
6869

70+
# tweets extractor
71+
self.tweets_extractor = self.get_tweets_extractor()
72+
6973
# links extractor
7074
self.links_extractor = self.get_links_extractor()
7175

@@ -138,7 +142,7 @@ def crawl(self, crawl_candidate):
138142
self.article.links = self.links_extractor.extract()
139143

140144
# tweets
141-
self.article.tweets = self.extractor.extract_tweets()
145+
self.article.tweets = self.tweets_extractor.extract()
142146

143147
# video handling
144148
self.video_extractor.get_videos()
@@ -183,6 +187,9 @@ def get_html(self, crawl_candidate, parsing_candidate):
183187
})
184188
return html
185189

190+
def get_tweets_extractor(self):
191+
return TweetsExtractor(self.config, self.article)
192+
186193
def get_links_extractor(self):
187194
return LinksExtractor(self.config, self.article)
188195

goose/extractors/content.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -192,21 +192,6 @@ def extract_opengraph(self):
192192
opengraph_dict.update({attr.split(":")[1]: value})
193193
return opengraph_dict
194194

195-
def extract_tweets(self):
196-
tweets = []
197-
items = self.parser.getElementsByTag(
198-
self.article.top_node,
199-
tag='blockquote',
200-
attr="class",
201-
value="twitter-tweet")
202-
203-
for i in items:
204-
for attr in ['gravityScore', 'gravityNodes']:
205-
self.parser.delAttribute(i, attr)
206-
tweets.append(self.parser.nodeToString(i))
207-
208-
return tweets
209-
210195
def extract_authors(self):
211196
authors = []
212197
author_nodes = self.parser.getElementsByTag(

goose/extractors/tweets.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# -*- coding: utf-8 -*-
2+
"""\
3+
This is a python port of "Goose" orignialy licensed to Gravity.com
4+
under one or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership.
7+
8+
Python port was written by Xavier Grangier for Recrutae
9+
10+
Gravity.com licenses this file
11+
to you under the Apache License, Version 2.0 (the "License");
12+
you may not use this file except in compliance
13+
with the License. You may obtain a copy of the License at
14+
15+
http://www.apache.org/licenses/LICENSE-2.0
16+
17+
Unless required by applicable law or agreed to in writing, software
18+
distributed under the License is distributed on an "AS IS" BASIS,
19+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
See the License for the specific language governing permissions and
21+
limitations under the License.
22+
"""
23+
24+
from goose.extractors import BaseExtractor
25+
26+
27+
class TweetsExtractor(BaseExtractor):
28+
29+
def extract(self):
30+
tweets = []
31+
items = self.parser.getElementsByTag(
32+
self.article.top_node,
33+
tag='blockquote',
34+
attr="class",
35+
value="twitter-tweet")
36+
37+
for i in items:
38+
for attr in ['gravityScore', 'gravityNodes']:
39+
self.parser.delAttribute(i, attr)
40+
tweets.append(self.parser.nodeToString(i))
41+
42+
return tweets

0 commit comments

Comments
 (0)