skander101 commited on
Commit
ac179d1
·
1 Parent(s): 678734d

fix: use trafilatura.extract() for text + bare_extraction(with_metadata=True) for date/image

Browse files
Files changed (1) hide show
  1. src/extractor.py +12 -7
src/extractor.py CHANGED
@@ -33,14 +33,19 @@ class ArticleExtractor:
33
  downloaded = self._trafilatura.fetch_url(url)
34
  if not downloaded:
35
  return None
36
- result = self._trafilatura.extract(downloaded, output_format='python')
37
- if not result or not result.get('text'):
38
  return None
39
- text = result['text']
40
- title = result.get('title') or self._extract_title_meta(downloaded) or ""
41
- soup = BeautifulSoup(downloaded, "html.parser")
42
- image = self._extract_og_image_soup(soup)
43
- published, published_iso = self._parse_trafilatura_date(result.get('date'))
 
 
 
 
 
44
  return Article(
45
  url=url, title=title, text=text, source_domain=domain,
46
  image_url=image, published=published, published_iso=published_iso,
 
33
  downloaded = self._trafilatura.fetch_url(url)
34
  if not downloaded:
35
  return None
36
+ text = self._trafilatura.extract(downloaded)
37
+ if not text:
38
  return None
39
+ doc = self._trafilatura.bare_extraction(downloaded, with_metadata=True)
40
+ title = (doc.title if doc else None) or self._extract_title_meta(downloaded) or ""
41
+ image = ""
42
+ published, published_iso = ("", "")
43
+ if doc:
44
+ image = doc.image or ""
45
+ published, published_iso = self._parse_trafilatura_date(doc.date)
46
+ if not image:
47
+ soup = BeautifulSoup(downloaded, "html.parser")
48
+ image = self._extract_og_image_soup(soup)
49
  return Article(
50
  url=url, title=title, text=text, source_domain=domain,
51
  image_url=image, published=published, published_iso=published_iso,