def __init__(self): # connect to couchdb try: auth = Session() auth.name = db_admin auth.password = db_password self.couch = couchdb.Server(couchdb_uri, session=auth) except Exception as e: print(e) return
def save(self, database, record): # locate database try: # check whether the database is created in Server if database in self.couch: db = self.couch[database] else: db = self.couch.create(database) except couchdb.http.ResourceNotFound: print("No database: " + database) print("try to create database on: " + str(couchdb_uri)) try: auth = Session() auth.name = input("Enter your couchdb username: "******"Enter your couchdb password: "******"ERROR: unauthorized couchdb access") exit(-1) except ConnectionRefusedError: print("ERROR: cannot not connect to remote couchdb Server") exit(-1) except Exception as e: print(e) exit(-1) #prevent duplication if db.get(record["_id"]) is None: # save into couchdb try: if record["topic"] == "Bad Tweet": return else: db.save(record) print(record) except couchdb.HTTPError as e: print("ERROR: duplicate") except Exception as e: # ouput other exceptions print(e) else: print("ignore duplicate tweets") return
def feedConsumer(): """ 'feedConsumer()' will connect and continuously listen to the changes feed for any docs with a 'state' value of 'unsent'. Finding one; it will parse the data into email headers and body, send the email, and update the doc's 'state' value to either 'sent' or 'error'. If the latter, it will also add a errormsg field with the contents of the message for debugging purposes. """ global auth, data, sender, recipients, toaddr, ccs, bccs, fromaddr, toaddrs, headers, msgformat # TODO:FIXME """ change to couchdb:couchdb user:group combination""" auth = Session() auth.name = '' auth.password = '' s1 = Server('http://localhost:5984/', session=auth) db1 = s1['mailspool'] ch = db1.changes(feed='continuous',heartbeat='1000',include_docs=True,filter='spooler/unsent') for line in ch: """ reset all vars to preclude any pollution between lines """ data = '' sender = {} recipients = {} fromaddr = '' toaddrs = [] toaddr = [] ccs = [] bccs = [] headers = '' msgformat = '' """ now grab the document object included in the feed """ data = line['doc'] """ assign some vars to populate the actual email fields """ sender = data['sender'] recipients = data['recipients'] msgformat = data['msgformat'] message = data['message'] subject = data['subject'] """ begin parsing """ parseChange() sendEmail(toaddr, ccs, bccs, message, subject) updateState(data)
# # Copyright © 2012 blurrcat <*****@*****.**> """ A manage script for couchDB. Functions are: - Create new jobs; - Reset job status; """ from couchdb import Server, Session dbname = 'soup' session = Session() session.username = '******' session.password = '******' server = Server(session=session) db = server[dbname] def job(key, action): return { '_id': action[0]+key, 'key': key, 'type': 'Job', 'action': action, 'status': 'idle', 'next_page': 1, 'max_page': None }
# -*- coding: utf-8 -*- # # Copyright © 2012 blurrcat <*****@*****.**> """ A manage script for couchDB. Functions are: - Create new jobs; - Reset job status; """ from couchdb import Server, Session dbname = 'soup' session = Session() session.username = '******' session.password = '******' server = Server(session=session) db = server[dbname] def job(key, action): return { '_id': action[0] + key, 'key': key, 'type': 'Job', 'action': action, 'status': 'idle', 'next_page': 1, 'max_page': None }
from couchdb import Server, Session from io import BytesIO auth = Session() auth.name = "rohan" auth.password = "******" couchserver = Server('http://localhost:5984/', session=auth) db=couchserver['sync'] ''' For creating a document from uuid import uuid4 doc_id = uuid4().hex db[doc_id] = {'type': 'person', 'name': 'John Doe'} ''' sync_folders = ["DroidA","DroidB"] ''' Make this a class ''' def store_file(f,folder): file_name=folder+"/"+f.filename doc={'name':file_name} # f=open(file_name,'r') for doc_id in db: document=db.get(doc_id,attachments=True) doc_name=document['name'] if doc_name == file_name: db.delete_attachment(document,filename=file_name) db.delete(document)