Giter Site home page Giter Site logo

readability's Introduction

Readability

CI Module Version Hex Docs Total Download License Coverage Status

Readability is a tool for extracting and curating the primary readable content of a webpage.

Installation

The package can be installed as:

Add :readability to your list of dependencies in mix.exs:

def deps do
  [
    {:readability, "~> 0.12"}
  ]
end

After that, run mix deps.get.

Note: Readability requires Elixir 1.10 or higher.

Usage

Examples

Just pass a url

url = "https://medium.com/@kenmazaika/why-im-betting-on-elixir-7c8f847b58"
summary = Readability.summarize(url)

summary.title
#=> "Why I’m betting on Elixir"

summary.authors
#=> ["Ken Mazaika"]

summary.article_html
#=>
# <div><div><p id=\"3476\"><strong><em>Background: </em></strong><em>I’ve spent...
# ...
# ...button!</em></h3></div></div>

summary.article_text
#=>
# Background: I’ve spent the past 6 years building web applications in Ruby and.....
# ...
# ... value in this article, it would mean a lot to me if you hit the recommend button!

From raw html

### Extract the title.
Readability.title(html)

### Extract authors.
Readability.authors(html)

### Extract the primary content with transformed html.
html
|> Readability.article
|> Readability.readable_html

### Extract only text from the primary content.
html
|> Readability.article
|> Readability.readable_text

### you can extract the primary images with Floki
html
|> Readability.article
|> Floki.find("img")
|> Floki.attribute("src")

Options

If the result is different from your expectations, you can add options to customize it.

Example

url = "https://medium.com/@kenmazaika/why-im-betting-on-elixir-7c8f847b58"
summary = Readability.summarize(url, [clean_conditionally: false])
  • :min_text_length \\ 25
  • :remove_unlikely_candidates \\ true
  • :weight_classes \\ true
  • :clean_conditionally \\ true
  • :retry_length \\ 250

You can find other algorithm and regex options in readability.ex

Test

To run the test suite:

$ mix test

Todo

  • Extract authors
  • More configurable
  • Summarize function
  • Convert relative paths into absolute paths of img#src and a#href

Contributions are welcome!

Check out the main features milestone and features of related projects below

Contributing

  1. Fork the repo on GitHub
  2. Clone the project to your own machine
  3. Commit changes to your own branch
  4. Push your work back up to your fork
  5. Submit a Pull request so that we can review your changes

NOTE: Be sure to merge the latest from "upstream" before making a pull request!

Related and Inspired Projects

  • readability.js is a standalone version of the readability library used for Firefox Reader View.
  • newspaper is an advanced news extraction, article extraction, and content curation library for Python.
  • ruby-readability is a tool for extracting the primary readable content of a webpage.

Copyright and License

Copyright (c) 2016 Jaehyun Shin

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

readability's People

Contributors

abersager avatar adam-pog avatar carbureted avatar chingan-tsc avatar edisonywh avatar frm avatar janza avatar jbrowning avatar keepcosmos avatar kianmeng avatar namjae avatar oldhammade avatar scorphus avatar simonbowen avatar sionide21 avatar valian avatar zpeters avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

readability's Issues

Relative image urls are broken in extracted content.

First of all thank you for this library, it's excellent!

Right now there's an issue for pages where the image url is set relative to the url. For example:

<img src="/content//images/2015/04/Hard_disk_Western_Digital_WD1000_1_-dark1-.jpg">

Can there be a way for the images to be set using the full domain path?

Readability.article(raw_html) encounter error

v: 0.6.1

** (FunctionClauseError) no function clause matching in Floki.DeepText.get_text/3
    lib/floki/deep_text.ex:28: Floki.DeepText.get_text(nil, "", "")
    lib/readability/helper.ex:75: Readability.Helper.text_length/1
    lib/readability/article_builder.ex:38: Readability.ArticleBuilder.build/2

Multiple title matches are concatenated

Given the following HTML...

<html>
  <head>
    <meta property='og:title' content='og title 1'>
    <meta property='og:title' content='og title 2'>
  </head>
<body>
...
</body>
</html>

... the tag finder will return "og title 1og title 2". This also occurs for the title tag finder. We should instead only extract the first matching tag.

PR is incoming. 😃

Floki Dependency

May it be possible for you to update Floki to the latest version or are there issues with it? I'd also send a PR if you want, incrementing the number.

function Floki.Selector.match?/2 is undefined or private.

{:readability, "~> 0.8.0"}


x = Readability.summarize("https://sergiotapia.me/pluralizing-strings-in-javascript-es6-b5d4d651d403")

Error:
15:40:05 web.1  |     ** (UndefinedFunctionError) function Floki.Selector.match?/2 is undefined or private. Did you mean one of:
15:40:05 web.1  |
15:40:05 web.1  |       * match?/3
15:40:05 web.1  |
15:40:05 web.1  |         (floki) Floki.Selector.match?({"html", [], [{"head", [], [{"title", [], ["302 Found"]}]}, {"body", [{"bgcolor", "white"}], [{"center", [], [{"h1", [], ["302 Found"]}]}, {"hr", [], []}, {"center", [], ["nginx"]}]}]}, [%Floki.Selector{attributes: [], classes: [], combinator: nil, id: nil, namespace: nil, pseudo_classes: [], type: "p"}])
15:40:05 web.1  |         (readability) lib/readability/helper.ex:84: anonymous fn/2 in Readability.Helper.candidate_tag?/1
15:40:05 web.1  |         (elixir) lib/enum.ex:2732: Enum.do_any?/2
15:40:05 web.1  |         (readability) lib/readability/candidate_finder.ex:54: Readability.CandidateFinder.candidate?/2
15:40:05 web.1  |         (readability) lib/readability/candidate_finder.ex:27: Readability.CandidateFinder.find/3
15:40:05 web.1  |         (readability) lib/readability/article_builder.ex:31: Readability.ArticleBuilder.build/2
15:40:05 web.1  |         (readability) lib/readability.ex:79: Readability.summarize/2
15:40:05 web.1  |         (myapp) lib/news/news.ex:30: MyApp.News.download_content/1

It looks like a 302 redirect error that Readability isn't capturing properly.

XML version tag seems to break summarize

iex(4)> require Readability
Readability

iex(5)> url = "http://www.ncbi.nlm.nih.gov/pubmed/22188593"
"http://www.ncbi.nlm.nih.gov/pubmed/22188593"

iex(6)> Readability.summarize(url)
** (FunctionClauseError) no function clause matching in Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:52: Readability.Helper.remove_tag({"version", "1.0"}, #Function<0.72746186/1 in Readability.ArticleBuilder.build/2>)
    (readability) lib/readability/helper.ex:55: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:66: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:55: Readability.Helper.remove_tag/2
    (readability) lib/readability/article_builder.ex:24: Readability.ArticleBuilder.build/2
    (readability) lib/readability.ex:77: Readability.summarize/2

iex(6)> url = "https://medium.com/@kenmazaika/why-im-betting-on-elixir-7c8f847b58"
"https://medium.com/@kenmazaika/why-im-betting-on-elixir-7c8f847b58"

iex(7)> Readability.summarize(url)
%Readability.Summary{article_html: "<div><div><p id=\"3476\"><strong><em>Background: </em></strong><em>I’ve spent the past 6 years building web applications in Ruby and the Rails framework. I’ve flirted with new programming languages as they came out, but Elixir is the first language that has been able to captivate me.</em> etc etc etc}

The main difference that I see is the <?xml version="1.0" encoding="utf-8"?> tag at the top of the pubmed link.

I'm pretty new to elixir, but if you have any pointers for fixing this, I'm happy to help!

Thanks.

Bug when extracting article from HTML

Example URL: "http://www.techhive.com/article/3158435/home-tech/43-off-tp-link-smart-led-wi-fi-light-bulb-dimmable-and-alexa-compatible-deal-alert.html#tk.rss_smartappliance"

Can summarize it:

iex(6)> Readability.summarize("http://www.techhive.com/article/3158435/home-tech/43-off-tp-link-smart-led-wi-fi-light-bulb-dimmable-and-alexa-compatible-deal-alert.html#tk.rss_smartappliance")        %Readability.Summary{article_html: "<div><div id=\"drr-container\"><p>TP-Link has discounted its 50W smart bulb <a href=\"https://www.amazon.com/TP-Link-Dimmable-Equivalent-Amazon-LB100/dp/B01HXM8XF6?psc=1&SubscriptionId=AKIAIRZJHSP2SKQIWVZA&tag=techconnect00-20&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B01HXM8XF6\" rel=\"nofollow\">43% to just $19.99</a>. Use the Kasa app to turn on/off or dim from anywhere in the world. Set up a schedule, set the mood, and even control with your voice via an Alexa-enabled device such as Echo or Dot. Reviewers rate 4 out of 5 stars (see reviews) on Amazon, where you can get yourself one (or more) for just $20, a good deal considering it typically lists north of $20 and sometimes $30 with various online retailers. <a href=\"https://www.amazon.com/TP-Link-Dimmable-Equivalent-Amazon-LB100/dp/B01HXM8XF6?psc=1&SubscriptionId=AKIAIRZJHSP2SKQIWVZA&tag=techconnect00-20&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B01HXM8XF6\" rel=\"nofollow\">See the discounted TP-Link smart LED bulb on Amazon</a>.</p><p>This story, \"43% off TP-Link Smart LED Wi-Fi Light Bulb, 50W Dimmable and Alexa Compatible - Deal Alert\" was originally published by\n<span><span><a href=\"http://www.techconnect.com/\" rel=\"nofollow\">TechConnect</a></span></span>.</p><div><div id=\"\">To comment on this article and other TechHive content, visit our <a href=\"https://www.facebook.com/techhivemedia/\">Facebook</a> page or our <a href=\"https://twitter.com/techhive\">Twitter</a> feed. </div></div></div></div>",
 article_text: "TP-Link has discounted its 50W smart bulb 43% to just $19.99. Use the Kasa app to turn on/off or dim from anywhere in the world. Set up a schedule, set the mood, and even control with your voice via an Alexa-enabled device such as Echo or Dot. Reviewers rate 4 out of 5 stars (see reviews) on Amazon, where you can get yourself one (or more) for just $20, a good deal considering it typically lists north of $20 and sometimes $30 with various online retailers. See the discounted TP-Link smart LED bulb on Amazon.\nThis story, \"43% off TP-Link Smart LED Wi-Fi Light Bulb, 50W Dimmable and Alexa Compatible - Deal Alert\" was originally published by\nTechConnect.\nTo comment on this article and other TechHive content, visit our Facebook page or our Twitter feed.",
 authors: ["DealPost Team"],
 title: "43% off TP-Link Smart LED Wi-Fi Light Bulb, 50W Dimmable and Alexa Compatible - Deal Alert"}

Extracting article directly from HTML fails:

iex(6)> Readability.article(HTTPoison.get!("http://www.techhive.com/article/3158435/home-tech/43-off-tp-link-smart-led-wi-fi-light-bulb-dimmable-and-alexa-compatible-deal-alert.html#tk.rss_smartappliance").body)
** (FunctionClauseError) no function clause matching in Floki.HTMLTree.build/1
          (floki) lib/floki/html_tree.ex:14: Floki.HTMLTree.build(nil)
          (floki) lib/floki/finder.ex:48: Floki.Finder.find_selectors/2
          (floki) lib/floki/filter_out.ex:17: Floki.FilterOut.filter_out/2
          (floki) lib/floki.ex:210: Floki.text/2
    (readability) lib/readability/helper.ex:75: Readability.Helper.text_length/1
    (readability) lib/readability/article_builder.ex:32: Readability.ArticleBuilder.build/2

This is because in the second case the "clean_conditionally: true" option is passed to the Sanitizer. Using different options is a bit surprising, nevertheless it should not crash.

Dependency versions too old

{:httpoison, "~> 0.13.0"},

This forces everyone with an updated codebase to use override: true

{:httpoison, "~> 1.5", override: true}

I know it's hard to keep the lib updated, but at least make the dependencies more open for updates

(FunctionClauseError) no function clause matching in Floki.HTMLTree.build/1

Hi I am having this error while trying to parse this URL http://www.alaskalegislativedigest.com/?p=577, you can reproduce it using

url = "http://www.alaskalegislativedigest.com/?p=577"
with %{status_code: 200, body: raw_html} <- HTTPoison.get!(url) do
    raw_html |> Readability.article() |> Readability.readable_text()
end

Now I know the error thrown is in the Floki library but it seemed like Readability is sending the HTML tree as nil to Floki parser.

(FunctionClauseError) no function clause matching in Readability.Helper.remove_tag/2

iex(2)> Readability.summarize("https://www.wired.com/2016/10/review-logitech-keys-go/")
** (FunctionClauseError) no function clause matching in Readability.Helper.remove_tag/2

    The following arguments were given to Readability.Helper.remove_tag/2:

        # 1
        {"echo", "echo"}

        # 2
        #Function<0.78353828/1 in Readability.ArticleBuilder.build/2>

    Attempted function clauses (showing 4 out of 4):

        def remove_tag(content, _) when is_binary(content)
        def remove_tag([], _)
        def remove_tag([h | t], fun)
        def remove_tag({tag, attrs, inner_tree} = html_tree, fun)

    (readability) lib/readability/helper.ex:52: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:55: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:66: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:55: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:66: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:55: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:59: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:66: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:55: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:59: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:66: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:55: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:66: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:55: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:59: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:66: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:55: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:59: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:66: Readability.Helper.remove_tag/2
    (readability) lib/readability/helper.ex:55: Readability.Helper.remove_tag/2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.