Skip to main content

Python library for GNIP.

Project description

https://badge.fury.io/py/gnippy.svg https://img.shields.io/pypi/dm/gnippy.svg https://travis-ci.org/abh1nav/gnippy.svg?branch=master

gnippy provides an easy way to access the Power Track stream provided by GNIP. You can also use gnippy to programatically add rules to your Power Track stream.

Install

pip install gnippy

Quickstart

Create a .gnippy file and place it in your home directory. It should contain the following:

[Credentials]
username = user@company.com
password = mypassword

[PowerTrack]
url = https://my.gnip.powertrack/url.json
rules_url = https://api.gnip.powertrack/rules.json

Fire up the client:

#!/usr/bin/env python
import time
from gnippy import PowerTrackClient

# Define a callback
def callback(activity):
    print activity

# Create the client
client = PowerTrackClient(callback)
client.connect()

# Wait for 2 minutes and then disconnect
time.sleep(120)
client.disconnect()

Configuration

If you don’t want to create a config file or you want it put it in another location:

client = PowerTrackClient(callback, config_file_path="/etc/gnippy")
# OR ... provide the url and authentication credentials to override any config files
client = PowerTrackClient(callback, url="http://my.gnip.powertrack/url.json", auth=("uname", "pwd"))

You can also configure gnippy using environment variables:

GNIPPY_URL
GNIPPY_RULES_URL
GNIPPY_AUTH_USERNAME
GNIPPY_AUTH_PASSWORD

Adding PowerTrack Rules

If you want to add rules to your PowerTrack:

from gnippy import rules
from gnippy.errors import RuleAddFailedException

# Synchronously add rules
try:
    rules.add_rule('(Hello OR World OR "this is a test") lang:en', tag="MyRule")
    rules.add_rule('Rule without a tag')
except RuleAddFailedException:
    pass

# OR ... synchronously add multiple rules at once
rule_list = []
rule_list.append(rules.build("Hello World", tag="asdf"))
rule_list.append(rules.build("Rule Without a Tag"))
try:
    rules.add_rules(rule_list)
except RuleAddFailedException:
    pass

# OR ... manually pass in params - overrides any config files
rules.add_rule("My Rule String", tag="mytag", rules_url="https://api.gnip.powertrack/rules.json", \
               auth=("uname", "pwd"))

Listing Active PowerTrack Rules

from gnippy import rules
from gnippy.errors import RulesGetFailedException

try:
    rules_list = rules.get_rules()
    # rules_list is in the format:
    # [
    #    { "value": "(Hello OR World) AND lang:en" },
    #    { "value": "Hello", "tag": "mytag" }
    # ]
except RulesGetFailedException:
    pass

Deleting PowerTrack Rules

from gnippy import rules
from gnippy.errors import RuleDeleteFailedException, RulesGetFailedException

try:
    rules_list = rules.get_rules()
    # Suppose I want to delete the first rule in the list
    rules.delete_rule(rules_list[0])
    # OR ... I want to delete ALL rules
    rules.delete_rules(rules_list)

except RuleDeleteFailedException, RulesGetFailedException:
    pass

Source available on GitHub: http://github.com/abh1nav/gnippy/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gnippy-0.6.1.tar.gz (7.6 kB view details)

Uploaded Source

File details

Details for the file gnippy-0.6.1.tar.gz.

File metadata

  • Download URL: gnippy-0.6.1.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for gnippy-0.6.1.tar.gz
Algorithm Hash digest
SHA256 7b3d9927753fa0457cd1cfe90b247a9a2b94cea0afef161f9578cd3b35a9b406
MD5 de3147ae7f1ef110c1e552e44e18ebf3
BLAKE2b-256 5d2283192a270b5a0bbb0a561447a2716ed7d8a7c541c70d3cc572db68772ce5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page