<commit_before>#!/usr/bin/env python

import zlib, json, os
from util import tweet_text, now_tweettime
import couchdb

class DBWrapper(object):
    def __init__(self, app_path, screen_name, srvuri, dbname):
        srv = couchdb.Server(srvuri)
        try:
            self.db = srv.create(dbname)
        except couchdb.http.PreconditionFailed:
            self.db = srv[dbname]

        js = open(os.path.join(app_path, 'undulatus.js')).read()
        revision = json.loads(js)["revision"]
        try:
            design = self.db['_design/undulatus']
            db_revision = None
            try:
                db_revision = design['revision']
            except KeyError:
                pass
            if db_revision != revision:
                print('Note: updating javascript design document')
                del self.db['_design/undulatus']
                self.db['_design/undulatus'] = js
        except couchdb.http.ResourceNotFound:
            self.db['_design/undulatus'] = js

    def configuration(self):
        try:
            doc = self.db['help_configuration']
            return doc
        except couchdb.http.ResourceNotFound:
            return None

    def save_configuration(self, new_config):
        try:
            doc = self.db['help_configuration']
        except couchdb.http.ResourceNotFound:
            doc = {}
        doc['configuration'] = new_config
        doc['updated'] = now_tweettime()
        self.db['help_configuration'] = doc
        return doc

    def tokens(self):
        try:
            doc = self.db['oauth_tokens']
            return doc['token'], doc['secret']
        except couchdb.http.ResourceNotFound:
            return None, None

    def add_tokens(self, oauth_token, oauth_token_secret):
        self.db['oauth_tokens'] = {'token': oauth_token, 'secret': oauth_token_secret}

    def get_by_status_id(self, status_id):
        try:
            return self.db[str(status_id)]
        except couchdb.http.ResourceNotFound:
            return None

    def get_replies_to_status_id(self, status_id):
        return [ self.get_by_status_id(row.id) for row in self.db.view('undulatus/replies')[status_id] ]

    # will probably break when twitter hits 63 bit status IDs..
    def get_recent(self, n):
        return [ self.get_by_status_id(row.id) for row in self.db.view('undulatus/byid', limit=n, descending=True) ]

    def make(self, tweet):
        doc = self.get_by_status_id(tweet['id_str'])
        if doc is None:
            self.db[str(tweet['id_str'])] = tweet
<commit_msg>prime cache in correct order<commit_after>#!/usr/bin/env python

import zlib, json, os
from util import tweet_text, now_tweettime
import couchdb

class DBWrapper(object):
    def __init__(self, app_path, screen_name, srvuri, dbname):
        srv = couchdb.Server(srvuri)
        try:
            self.db = srv.create(dbname)
        except couchdb.http.PreconditionFailed:
            self.db = srv[dbname]

        js = open(os.path.join(app_path, 'undulatus.js')).read()
        revision = json.loads(js)["revision"]
        try:
            design = self.db['_design/undulatus']
            db_revision = None
            try:
                db_revision = design['revision']
            except KeyError:
                pass
            if db_revision != revision:
                print('Note: updating javascript design document')
                del self.db['_design/undulatus']
                self.db['_design/undulatus'] = js
        except couchdb.http.ResourceNotFound:
            self.db['_design/undulatus'] = js

    def configuration(self):
        try:
            doc = self.db['help_configuration']
            return doc
        except couchdb.http.ResourceNotFound:
            return None

    def save_configuration(self, new_config):
        try:
            doc = self.db['help_configuration']
        except couchdb.http.ResourceNotFound:
            doc = {}
        doc['configuration'] = new_config
        doc['updated'] = now_tweettime()
        self.db['help_configuration'] = doc
        return doc

    def tokens(self):
        try:
            doc = self.db['oauth_tokens']
            return doc['token'], doc['secret']
        except couchdb.http.ResourceNotFound:
            return None, None

    def add_tokens(self, oauth_token, oauth_token_secret):
        self.db['oauth_tokens'] = {'token': oauth_token, 'secret': oauth_token_secret}

    def get_by_status_id(self, status_id):
        try:
            return self.db[str(status_id)]
        except couchdb.http.ResourceNotFound:
            return None

    def get_replies_to_status_id(self, status_id):
        return [ self.get_by_status_id(row.id) for row in self.db.view('undulatus/replies')[status_id] ]

    # will probably break when twitter hits 63 bit status IDs..
    def get_recent(self, n):
        rv = [ self.get_by_status_id(row.id) for row in self.db.view('undulatus/byid', limit=n, descending=True) ]
        rv.reverse()
        return rv

    def make(self, tweet):
        doc = self.get_by_status_id(tweet['id_str'])
        if doc is None:
            self.db[str(tweet['id_str'])] = tweet

{'fraction_non_alphanumeric': 0.10797213622291021, 'fraction_numerical': 0.0007739938080495357, 'mean_word_length': 2.206575682382134, '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': '4569156', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1541, 'n_words': 333}