Coder Social home page Coder Social logo

kayak's Introduction

kayak

A customizable Twitter client to collect tweets matching certain criteria

Setting up kayak

Twitter Application Credentials

  • Create a new application using Twitter Application Management console.
  • Note down the Consumer Key (API Key) and Consumer Secret (API Secret) credentials.
[Optional] Application's Access Level should be Read only.

Store Credentials as Environment Variables

  • Save the consumer key and consumer secret credentials as envrionment variables titled KAYAK_CONSUMER_KEY and KAYAK_CONSUMER_SECRET respectively.
  • One possible way to add environment variables is to add them to your ~/.bashrc file and source it.
KAYAK_CONSUMER_KEY="xxxxxxxx"; export KAYAK_CONSUMER_KEY
KAYAK_CONSUMER_SECRET="xxxxxxxxxxxxxxxx"; export KAYAK_CONSUMER_SECRET

Installation

For now the package isn't up there on PyPI, hence you'll have to install it manually.

$ git clone [email protected]:pravj/kayak.git
$ cd kayak
$ pip install .

It will make the package available in your local environment.

--

Using kayak

The package exports KayakClient class to use, it defines the following method.

get_tweets(search_query, minimum_retweet=1, older_tweets=True)
    Returns an iterator for Twitter API responses (tweet entities i.e. KayakTweet).

    :param (str) search_query: Search query operator to use.

    :param (int) minimum_retweet: Number of retweets (Default 1)
        a tweet should have at least.

    :param (bool) older_tweets:
        If True (default), it will return the iterator with older tweets.
        If False, it will return newer tweets on each iteration.

The class KayakTweet provides following variables of a tweet.

  • text : Tweet text
  • id : ID of the Tweet
  • retweets : Number of retweets
class KayakTweet(object):
    """
    Class to change the representation of a Tweet object.
    Currently you can access the 'text', 'id', and 'retweet count' attributes.
    """

--

Create a client instance.

from kayak import KayakClient

client = KayakClient()
Refer to the API documentation Query operators to create your custom search query.

To collect old tweets (that have been created in past)

search_query = '#custserv'

# Similarly, if you want tweets mentioning Twitter account '@potus'
# search_query = '@potus'

old_tweets_iterator = client.get_tweets(search_query, minimum_retweet=2)

for tweets in old_tweets_iterator:
	for tweet in tweets:
		print tweet.id, tweet.retweets

		# same value (str) as 'tweet.text' (using __repr__ data method)
		print tweet

To collect new tweets (as they are being created continuously)

# Ideally, to avoid raising Exception
# one should wait in between successive iterations.
# So that we don't end up having no new tweets.
import time

new_tweets_iterator = client.get_tweets('#custserv', older_tweets=False)

while True:
	new_tweets = new_tweets_iterator.next()

	for tweet in new_tweets:
		print tweet.id, tweet.retweets
		print tweet.text

	time.sleep(60)

Tweet Filteration

Custom Filter Function

External Filteration using Decorators : under exploration

--

LICENSE

Pravendra Singh โ€ข GPLv3

kayak's People

Contributors

pravj avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

munta

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.