Code Monkey home page Code Monkey logo

tweedle's Introduction

Tweedle

v2 build version

Library is currently in alpha and the API might change as the Twitter v2 API changes

Tweedle is an Android library built around the Twitter v2 API built fully in Kotlin using Kotlin Coroutines

Usage

ViewModel

class MainViewModel: ViewModel() {

    val oauth2 = OAuth2(token)
    private val _tweetLookup:TweetsLookup = TweetsLookup(oauth2)
    
    fun getTweet(tweetId:Long):LiveData<Response<SingleTweetPayload?>>{
            val liveData:MutableLiveData<Response<SingleTweetPayload?>> = MutableLiveData<Response<SingleTweetPayload?>>()
            viewModelScope.launch{
                val response = _tweetLookup.getTweet(tweetId)
                liveData.postValue(response)
            }
            return liveData
        }
}

Activity

_viewModel.getTweet(1299418846990921728).observe(this, Observer {
    when(it){
        is Response.Error -> {it.exception}
        is Response.Success -> it.data
    }
})

Streaming

Tweedle also supports streaming in real time of current tweets based on filters/rules applied

Creating a filter

You can easily create a filter with the filter builder

Say we want to stream tweets that come in for the hashtag #SundayMorning and only in english. That filter would look like this

val filters:MutableList<Add> = mutableListOf()
        val filter: Filter = Filter.Builder()
            .addOperator("#SundayMorning")
            .and()
            .setLanguage(Filter.ENGLISH)
            .build()

See additional examples here as well as the twiter documentation about creating filters here

Adding a filter/rule

(Streaming only supports OAuth2)

val oAuth2 = OAuth2(token)
val _tweetStream = TweetsStream(oAuth2)
val addRule = Add(filter.filter, "Sunday Morning")
filters.add(addRule)
val rule = Rule(filters)

_tweetStream.addRules(token, rule)

Currently Twitter only supports 25 filters/rules per API key

To start streaming tweets call the startTweetStream endpoint

_tweetStream.startTweetStream().collect {
    when(it){
         is Response.Success -> Log.d("TWEET", it.data.data.text)
    }
}

Collect is called every time a new tweet is received

Authentication

To read about how to authenticate with the api see authentication

To start using Tweedle, include the dependency in your build.gradle

Common

implementation("io.github.tyczj:tweedle:{tweedle_version}")

Android

android{
    buildTypes {
        debug { matchingFallbacks = ['release'] } 
    }
}

implementation 'io.github.tyczj:tweedle-android:{tweedle_version}'

tweedle's People

Contributors

tyczj avatar

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.