Esempio n. 1
0
class DB:
    """
    database connection and queries
    """
    def __init__(self, app):
        self.couch = CouchDB(app.config['COUCHDB_USER'],
                             app.config['COUCHDB_PASSWORD'],
                             url=app.config['COUCHDB_URL'],
                             connect=True,
                             auto_renew=True)
        # if not existing create needed databases
        if app.config['COUCHDB_DATABASE'] not in self.couch.all_dbs():
            self.database = self.couch.create_database(
                app.config['COUCHDB_DATABASE'])
        else:
            self.database = self.couch[app.config['COUCHDB_DATABASE']]

        # workaround to avoid error message about missing '_users' database
        if not '_users' in self.couch:
            self.couch.create_database('_users')

    def add(self, data):
        self.database.create_document(data.__dict__)

    def filter_by_type(self, filter_type):
        """
        retrieves documents filtered by type and ordered by non-document-id
        """
        result = {}
        for item in Query(self.database, selector={
                'type': filter_type
        }).result:
            item_id = item['_id'].split(f'{filter_type}-', 1)[1]
            result[item_id] = item
        return result
Esempio n. 2
0
class Database:
    def __init__(self):
        self.user = os.getenv("COUCHDB_USER")
        self.password = os.getenv("COUCHDB_PASSWORD")
        self.url = os.getenv("COUCHDB_URL")
        self.connect = True
        self.client = CouchDB(
            **{
                'user': self.user,
                'auth_token': self.password,
                'url': self.url,
                'connect': self.connect
            })

    def get_users(self):
        """ Returns a connection to the users database.
        :return:
        """
        users_db_name = 'users'
        self.client.connect()
        try:
            users_db = self.client[users_db_name]

        except KeyError as e:
            print(e)
            print(f"[CouchDB] Creating database: {users_db_name}")
            users_db = self.client.create_database(users_db_name,
                                                   partitioned=False)
        return users_db

    def get_notes(self):
        """ Returns a connection to the notes database.
        Each note SHOULD BE partitioned to the appropriate user `_id` (aka email)
        :return:
        """
        notes_db_name = 'notes'
        try:
            notes_db = self.client[notes_db_name]
        except KeyError as e:
            print(e)
            print(f"[CouchDB] Creating database: {notes_db_name}")
            notes_db = self.client.create_database(notes_db_name,
                                                   partitioned=True)
        return notes_db
Esempio n. 3
0
def addExternalWeather(date_time, city, temp_c):
    print("Saving external weather")
    client = CouchDB("admin",
                     "password",
                     url="http://10.0.0.19:5984",
                     connect=True)
    db = client.create_database("externalweather")
    data = {}
    data["temp"] = str(temp_c)
    data["format"] = "C"
    data["city"] = str(city)
    doc = db.create_document(data)
    doc.save()
    client.disconnect()
Esempio n. 4
0
def addInternalWeather(date_time, temp_c):
    print("Saving internal weather")
    client = CouchDB("admin",
                     "password",
                     url="http://10.0.0.19:5984",
                     connect=True)
    db = client.create_database("internalweather")
    data = {}
    # data[date_time.strftime("%Y-%m-%dT%H:%M:%S")]
    data["temp"] = str(temp_c)
    data["format"] = "C"
    data["datetime"] = date_time.strftime("%Y-%m-%dT%H:%M:%S")
    doc = db.create_document(data)

    doc.save()
    client.disconnect()
Esempio n. 5
0
# load env vars from .env file
load_dotenv()

COUCH_URL = os.getenv("COUCHDB_URL")
USERNAME = os.getenv("COUCHDB_USERNAME")
PASSWORD = os.getenv("COUCHDB_PASSWORD")
DB_NAME = os.getenv("COUCHDB_DB_NAME")

couch = CouchDB(USERNAME,
                PASSWORD,
                url=COUCH_URL,
                connect=True,
                auto_renew=True)

couch.create_database(DB_NAME)
couch.create_database('twitter_harvester_checkpoint')
tweets_db = couch[DB_NAME]
harvester_checkpoint = couch['twitter_harvester_checkpoint']


def export_tweet(tweet):
    print(f'saving: {tweet["id_str"]}')
    tweet['_id'] = tweet['id_str']
    tweets_db.create_document(tweet)


def load_checkpoint(city):
    id = None
    try:
        id = harvester_checkpoint[city]['id']