<commit_before># jsb.plugs.common/twitter.py
#
#

""" a twitter plugin for the JSONBOT. uses tweepy oauth. """


## jsb imports

from jsb.utils.exception import handle_exception
from jsb.lib.commands import cmnds
from jsb.lib.examples import examples
from jsb.utils.pdol import Pdol
from jsb.utils.textutils import html_unescape
from jsb.utils.generic import waitforqueue, strippedtxt, splittxt
from jsb.lib.persist import PlugPersist
from jsb.utils.twitter import twitterapi, twittertoken
from jsb.lib.datadir import getdatadir
from jsb.lib.jsbimport import _import_byfile

## tweppy imports

from jsb.contrib.tweepy.auth import OAuthHandler
from jsb.contrib.tweepy.api import API
from jsb.contrib.tweepy import oauth
from jsb.contrib.tweepy.error import TweepError
from jsb.contrib.tweepy.models import Status, User
from jsb.contrib import tweepy

go = True

## basic imports

import os
import urllib2
import types
import logging

## credentials

def getcreds(datadir):
    try:
        mod = _import_byfile("credentials", datadir + os.sep + "config" + os.sep + "credentials.py")
    except (IOError, ImportError):
        logging.info("the twitter plugin needs the credentials.py file in the %s/config dir. see %s/examples" % (datadir, datadir))
        return (None, None)
    return mod.CONSUMER_KEY, mod.CONSUMER_SECRET

## defines

auth = None

def getauth(datadir):
    global auth
    if auth: return auth
    key, secret = getcreds(datadir)
    auth = OAuthHandler(key, secret)
    return auth

## functions

def postmsg(username, txt):
    result = splittxt(txt, 139)
    twitteruser = TwitterUsers("users")
    key, secret = getcreds(getdatadir())
    token = twittertoken(key, secret, twitteruser, username)
    if not token:
        raise TweepError("Can't get twitter token")
    twitter = twitterapi(key, secret, token)
    for txt in result:
        status = twitter.update_status(txt)
    logging.warn("logged %s tweets for %s" % (len(result), username))
    # BHJTW need to check status
    return len(result)
    
## classes

class TwitterUsers(PlugPersist):

    def add(self, user, token):
        user = user.strip().lower()
        self.data[user] = token
        self.save()

    def remove(self, user):
        user = user.strip().lower()
        if user in self.data:
            del self.data[user]
            self.save()

    def size(self):
        return len(self.data)

    def __contains__(self, user):
        user = user.strip().lower()
        return user in self.data

<commit_msg>lower loglevel<commit_after># jsb.plugs.common/twitter.py
#
#

""" a twitter plugin for the JSONBOT. uses tweepy oauth. """


## jsb imports

from jsb.utils.exception import handle_exception
from jsb.lib.commands import cmnds
from jsb.lib.examples import examples
from jsb.utils.pdol import Pdol
from jsb.utils.textutils import html_unescape
from jsb.utils.generic import waitforqueue, strippedtxt, splittxt
from jsb.lib.persist import PlugPersist
from jsb.utils.twitter import twitterapi, twittertoken
from jsb.lib.datadir import getdatadir
from jsb.lib.jsbimport import _import_byfile

## tweppy imports

from jsb.contrib.tweepy.auth import OAuthHandler
from jsb.contrib.tweepy.api import API
from jsb.contrib.tweepy import oauth
from jsb.contrib.tweepy.error import TweepError
from jsb.contrib.tweepy.models import Status, User
from jsb.contrib import tweepy

go = True

## basic imports

import os
import urllib2
import types
import logging

## credentials

def getcreds(datadir):
    try:
        mod = _import_byfile("credentials", datadir + os.sep + "config" + os.sep + "credentials.py")
    except (IOError, ImportError):
        logging.info("the twitter plugin needs the credentials.py file in the %s/config dir. see %s/examples" % (datadir, datadir))
        return (None, None)
    return mod.CONSUMER_KEY, mod.CONSUMER_SECRET

## defines

auth = None

def getauth(datadir):
    global auth
    if auth: return auth
    key, secret = getcreds(datadir)
    auth = OAuthHandler(key, secret)
    return auth

## functions

def postmsg(username, txt):
    result = splittxt(txt, 139)
    twitteruser = TwitterUsers("users")
    key, secret = getcreds(getdatadir())
    token = twittertoken(key, secret, twitteruser, username)
    if not token:
        raise TweepError("Can't get twitter token")
    twitter = twitterapi(key, secret, token)
    for txt in result:
        status = twitter.update_status(txt)
    logging.info("logged %s tweets for %s" % (len(result), username))
    # BHJTW need to check status
    return len(result)
    
## classes

class TwitterUsers(PlugPersist):

    def add(self, user, token):
        user = user.strip().lower()
        self.data[user] = token
        self.save()

    def remove(self, user):
        user = user.strip().lower()
        if user in self.data:
            del self.data[user]
            self.save()

    def size(self):
        return len(self.data)

    def __contains__(self, user):
        user = user.strip().lower()
        return user in self.data


{'fraction_non_alphanumeric': 0.0980353244691407, 'fraction_numerical': 0.0015876165905933718, 'mean_word_length': 3.359861591695502, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2945089', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1609, 'n_words': 471}