Skip to content

muhrifqii/ParseRSS

Repository files navigation

ParseRSS

CodeFactor codebeat badge

RSS Parser for android



Simple, concise, and extensible RSS Parser in the entire coffee shop. It can capture these information from the RSS article:

  • RSS Version based on https://validator.w3.org/feed/docs/

  • RSS Namespace Checking

    • Atom
    • DC
    • Media
    • RDF
    • SY
    • Specific namespaces
  • Channel

    • Title <title>
    • Description <description>
    • Link <link>
    • Publication Date <pubDate>
    • Image <image>
    • Language <language>
    • Copyright <copyright>
    • Rights <rights>
    • Last Build Date <lastBuildDate>
    • Atom Link <atom:link>
    • TimeToLive <ttl>
    • SkipHours <skipHours>
    • SkipDays <skipDays>
    • Managing Editor <managingEditor
  • Items Element

    • Title <title>
    • Description <description>
    • Link <link>
    • Item GUId <guid>
    • Media Content (NYT) <media:content>
    • Media Credit (NYT) <media:credit>
    • Media Description (NYT) <media:description>
    • Publication Date <pubDate>
    • Author <author>
    • Categories <category>
    • Source <source>
    • Enclosure <enclosure>
    • Atom Link <atom:link>
    • DC Creator (NYT) <dc:creator>
    • Comments <comments>

ParseRSS mainly has two main objects. RSSFeedObject and RSSItemObject. You can create your own parsing strategy by implementing RSSFeed and RSSItem.

Usage

ParseRSS depends on XmlPullParser, so feed it at least once in a lifetime. First thing first, the initialization part. You can put it on your Application onCreate function.

ParseRSS.init(XmlPullParserFactory.newInstance())

Be aware that XmlPullParser could throw an exception even in initialization step.

Next, feed the RSS string into ParseRSS

val feed: RSSFeedObject = ParseRSS.parse(xml)
feed.items.forEach {
  print(it.title)
}

ParseRSS as a Converter

ParseRSS does not have its own networking mechanism. Instead, it benefits from infamous networking library such as Retrofit, and Fuel. By using ConverterFactory, ParseRSS is ready to ship without breaking your project design pattern.

Fuel

Convert Fuel Response into RSSFeed by using responseRss function.

Fuel.get(URL).responseRss<RSSFeedObject> { result ->
  result.fold({ feed ->
    feed.items.forEach {
      print(it.title)
    }
  }, { error ->
    print(error)
  })
}

Retrofit

Convert Retrofit Response into RSSFeed by using ParseRSSConverterFactory

interface MyRssService {
    @GET("rss")
    fun rss(): Call<RSSFeedObject>
}
val retrofit = Retrofit.Builder()
    .addConverterFactory(ParseRSSConverterFactory.create<RSSFeedObject>())
    .baseUrl(BASE_URL)
    .build()

val rssService = retrofit.create(MyRssService::class.java)
rssService.rss().enqueue(object : Callback<RSSFeedObject> {
    override fun onFailure(call: Call<RSSFeedObject>, t: Throwable) {
    }
    override fun onResponse(call: Call<RSSFeedObject>, response: Response<RSSFeedObject>) {
    }
})

Gradle Dependency

Add jitpack repository in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Used on Fuel

// ParseRSS as Fuel Converter Factory
implementation "com.github.muhrifqii.ParseRSS:parserss:$version"
implementation "com.github.muhrifqii.ParseRSS:fuel:$version"

Used on Retrofit

// ParseRSS as Retrofit Converter Factory
implementation "com.github.muhrifqii.ParseRSS:parserss:$version"
implementation "com.github.muhrifqii.ParseRSS:retrofit:$version"

License

Copyright (c) 2019 Muhammad Rifqi Fatchurrahman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.