Example #1
0
    def connection_to_database(self):
        if os.path.isfile('../resources/vcap-local.txt'):
            with open('../resources/vcap-local.txt') as f:
                vcap = json.load(f)
                print('Found local VCAP_SERVICES')
                creds = vcap['services']['cloudantNoSQLDB'][0]['credentials']
                user = creds['username']
                password = creds['password']
                url = 'https://' + creds['host']
                client = Cloudant(user,
                                  password,
                                  url=url,
                                  connect=True,
                                  auto_renew=True,
                                  adapter=Replay429Adapter(
                                      retries=10, initialBackoff=0.01))

        session = client.session()
        print('Username: {0}'.format(session['userCtx']['name']))
        print('Databases: {0}'.format(client.all_dbs()))

        my_database = client['batya_db']
        self.db = my_database
        my_document = my_database.client['batya_db']
        # crops4years= self.get4Years(self, 2018, self.db)
        return my_database
class CouchDB():
    def __init__(self, cfg):
        self._cfg = cfg
        self._client = Cloudant(self._cfg['user'],
                                self._cfg['password'],
                                url=self._cfg['host'])
        self._client.connect()
        self._db = self._get_db()

    def _get_db(self):
        databases = self._client.all_dbs()
        db_name = self._cfg[self._name]
        if not db_name in databases:
            self._client.create_database(db_name)
        return self._client[db_name]

    def update_document(self, document_id, attributes_dict):
        if attributes_dict is None:
            return False

        document = self._db[document_id]
        for key in attributes_dict.keys():
            document[key] = attributes_dict[key]
        document.save()
        return True

    def reconnect(self):
        try:
            self._client.disconnect()
            self._client.connect()
        except Exceptions as e:
            print('Eror: {}'.format(e))
Example #3
0
def update_all_to_db():
    adapter = Replay429Adapter(200, 0.25)
    client = Cloudant(api_user, api_pass, url=api_url, adapter=adapter)
    # or using url
    # client = Cloudant(USERNAME, PASSWORD, url='https://acct.cloudant.com')

    # Connect to the server
    client.connect()

    # Perform client tasks...
    session = client.session()
    print('Username: {0}'.format(session['userCtx']['name']))
    print('Databases: {0}'.format(client.all_dbs()))

    courses_db = client['purdue_courses']

    file_path = "CourseInfo.json"
    f = open(file_path, 'r')
    text = f.read()
    f.close()
    deep_caches = json.loads(text)

    cache = dict()
    for cache_type, cache in deep_caches.items():
        cache.update(cache)

    bulk_update(cache, courses_db)

    # Disconnect from the server
    client.disconnect()
Example #4
0
class DBCloudant:
    def __init__(self):
        self.client = ""
        self.session = ""
        self.dataBase = ""
        self.document = ""
        self.query = ""

    def connect(self):
        self.client = Cloudant(conf.username,
                               conf.password,
                               url=conf.url,
                               connect=True)
        self.session = self.client.session()
        if self.session['userCtx']['name']:
            print("Coneccion realizada correctamente")
        else:
            print("Error al conectar con la BD")

    def disconnect(self):
        self.client.disconnect()

    def createDB(self, name):
        try:
            self.dataBase = self.client.create_database(name)
            if self.dataBase.exists():
                print("DB creada correctamente")
        except Exception as error:
            print("DB ya existente")

    def getDataBases(self):
        return self.client.all_dbs()

    def deleteDataBase(self, name):
        return self.client.delete_database(name)

    def openDB(self, name):
        self.dataBase = self.client[name]

    def createDocument(self, data):
        self.document = self.dataBase.create_document(data)
        if self.document.exists():
            print("Se creo el documento correctamente")

    def getDocument(self, name):
        self.document = self.dataBase[name]
        return self.document

    def getAllDocuments(self, name):
        return self.dataBase

    def getDocumentBySensor(self, sensor):
        self.query = cl.query.Query(self.dataBase)
        with self.query.custom_result(selector={"sensor": sensor}) as rst:
            return rst
Example #5
0
    def test_change_credentials_basic(self, m_req):
        """
        Test changing credentials when using basic access authentication.
        """
        # mock 200
        m_response_ok = mock.MagicMock()
        m_response_ok.json.return_value = ['animaldb']

        # mock 401
        m_response_bad = mock.MagicMock()
        m_response_bad.raise_for_status.side_effect = HTTPError('401 Unauthorized')

        m_req.side_effect = [m_response_bad, m_response_ok]

        client = Cloudant('foo', 'bar', url=self.url, use_basic_auth=True)
        client.connect()
        self.assertIsInstance(client.r_session, BasicSession)

        with self.assertRaises(HTTPError):
            client.all_dbs()  # expected 401

        m_req.assert_called_with(
            'GET',
            self.url + '/_all_dbs',
            allow_redirects=True,
            auth=('foo', 'bar'),  # uses HTTP Basic Auth
            timeout=None
        )

        # use valid credentials
        client.change_credentials('baz', 'qux')
        all_dbs = client.all_dbs()

        m_req.assert_called_with(
            'GET',
            self.url + '/_all_dbs',
            allow_redirects=True,
            auth=('baz', 'qux'),  # uses HTTP Basic Auth
            timeout=None
        )
        self.assertEquals(all_dbs, ['animaldb'])
def ListFiles():
	client = Cloudant("accesS_key","secret key", 	url='url')

	client.connect()
	session = client.session()
	print 'Username: {0}'.format(session['userCtx']['name'])
	print 'Databases: {0}'.format(client.all_dbs())
	my_database = client['test']
	## Get all of the documents from my_database
	t = HTML.Table(header_row=['File Name', 'File Version', 'Last Modified Date'])
	for document in my_database:
		t.rows.append([document['name'],document['version'],document['Last_Modified_Date']])
	htmlcode = str(t)
    	return htmlcode
Example #7
0
def connect(url):
    try:
        global cc, all_dbs
        cc = Cloudant(os.environ['COUCHUSER'],
                      os.environ['COUCHPASS'],
                      url=url,
                      connect=True)
        all_dbs = cc.all_dbs()
    except KeyError:
        logging.error(
            'please configure the COUCHUSER and COUCHPASS environment variables'
        )
        exit(1)
    except Exception as e:
        logging.error('unable to connect to CouchdB', e)
        exit(1)
Example #8
0
def consultarCloudant():
    # Authenticate using an IAM API key
    client = Cloudant(CLOUDANT_USERNAME,
                      CLOUDANT_PASSWORD,
                      url=CLOUDANT_URL,
                      connect=True)

    session = client.session()
    print('Username: {0}'.format(session['userCtx']['name']))
    print('Databases: {0}'.format(client.all_dbs()))

    result_collection = Result(client["prueba"].all_docs, include_docs=True)
    print("Retrieved minimal document:\n{0}\n".format(result_collection[0]))
    # Disconnect from the server
    client.disconnect()

    return result_collection[0]
Example #9
0
def main():
    client = Cloudant(config.username, config.password, account=config.account)

    client.connect()

    dbs = client.all_dbs()

    output = []
    for db in dbs:
    	print 'Retrieving stats for {0}...'.format(db)
        db = CloudantDatabase(client, db)
        print "db: " + json.dumps(db)
        output.append(db.doc_count())

	print json.dumps(output, indent=4)
	print json.dumps(sort(output), indent=4)

    client.disconnect()
Example #10
0
def main():
    client = Cloudant(config.username, config.password, account=config.account)

    client.connect()

    dbs = client.all_dbs()

    output = []
    for db in dbs:
        print 'Retrieving stats for {0}...'.format(db)
        db = CloudantDatabase(client, db)
        print "db: " + json.dumps(db)
        output.append(db.doc_count())

        print json.dumps(output, indent=4)
        print json.dumps(sort(output), indent=4)

    client.disconnect()
Example #11
0
def update_lookup_tables():
    adapter = Replay429Adapter(200, 0.25)
    client = Cloudant(api_user, api_pass, url=api_url, adapter=adapter)
    # or using url
    # client = Cloudant(USERNAME, PASSWORD, url='https://acct.cloudant.com')

    # Connect to the server
    client.connect()

    # Perform client tasks...
    session = client.session()
    print('Username: {0}'.format(session['userCtx']['name']))
    print('Databases: {0}'.format(client.all_dbs()))

    file_path = "CourseInfo.json"
    f = open(file_path, 'r')
    text = f.read()
    f.close()
    deep_caches = json.loads(text)

    query_table_db = client['query_table']
    query_table = __make_query_table__(deep_caches['Subjects'],
                                       deep_caches['Courses'])
    bulk_update(query_table, query_table_db)

    api_class_lookup_table_db = client['api_class_lookup_table']
    api_class_lookup_table = \
        __make_lookup_table__(deep_caches['Classes'], 'CourseId')
    bulk_update(api_class_lookup_table, api_class_lookup_table_db)

    section_lookup_table_db = client['section_lookup_table']
    section_lookup_table = \
        __make_lookup_table__(deep_caches['Sections'], 'ClassId')
    bulk_update(section_lookup_table, section_lookup_table_db)

    meeting_lookup_table_db = client['meeting_lookup_table']
    meeting_lookup_table = \
        __make_lookup_table__(deep_caches['Meetings'], 'SectionId')
    bulk_update(meeting_lookup_table, meeting_lookup_table_db)

    # Disconnect from the server
    client.disconnect()
Example #12
0
def query_from_db(odata_id):
    client = Cloudant(api_user, api_pass, url=api_url)
    print("Quering all meeting times")

    # Connect to the server
    client.connect()

    # Perform client tasks...
    session = client.session()
    print('Username: {0}'.format(session['userCtx']['name']))
    print('Databases: {0}'.format(client.all_dbs()))

    courses_db = client['testing_db']

    if odata_id not in courses_db:
        out = None
    else:
        out = courses_db[odata_id]

    client.disconnect()
    return out
Example #13
0
    def test_session_basic(self, m_req):
        """
        Test using basic access authentication.
        """
        m_response_ok = mock.MagicMock()
        type(m_response_ok).status_code = mock.PropertyMock(return_value=200)
        m_response_ok.json.return_value = ['animaldb']
        m_req.return_value = m_response_ok

        client = Cloudant('foo', 'bar', url=self.url, use_basic_auth=True)
        client.connect()
        self.assertIsInstance(client.r_session, BasicSession)

        all_dbs = client.all_dbs()

        m_req.assert_called_once_with(
            'GET',
            self.url + '/_all_dbs',
            allow_redirects=True,
            auth=('foo', 'bar'),  # uses HTTP Basic Auth
            timeout=None)

        self.assertEquals(all_dbs, ['animaldb'])
Example #14
0
# print(os.environ)
startDate = os.environ['START_DATE']
endDate = os.environ['END_DATE']
catalogOfferingType = os.environ['CATALOG_OFFERING_TYPE']

startDate = datetime.datetime.strptime(startDate, '%Y-%m-%d')
endDate = datetime.datetime.strptime(endDate, '%Y-%m-%d')

if endDate > startDate:
    print(startDate, endDate, catalogOfferingType)

client = Cloudant("admin", "admin", url="http://127.0.0.1:5984", connect=True)

session = client.session()
print('Username: {0}'.format(session['userCtx']['name']))
print('Databases: {0}'.format(client.all_dbs()))

a = "1994-01-23"
date_time_obj = datetime.datetime.strptime(a, '%Y-%m-%d')

b = "2020-01-29T07:00:42+0000"
date_time_obj2 = datetime.datetime.strptime(b, '%Y-%m-%dT%H:%M:%S+%f')

all_dbs = client.all_dbs()
docs = []
for db in all_dbs:
    if db == "blueprint_db":
        my_database = client[db]
        for document in my_database:
            docs.append(document)
print(len(docs))
Example #15
0
data = {
    "name": "zhzhzh23233",
    "eve_type": "morning",
}

for j in range(1, 11):
    for i in range(1, 11):
        data["name"] = "www" + str(i)
        data["wechat_id"] = "sdfxcsdf2sdf" + str(j)
        data["eve_time"] = int(time.time())
        time.sleep(0.1)
        test_db.create_document(data)
"""

# Perform client tasks...
session = client.session()
print 'Username: {0}'.format(session['userCtx']['name'])
print 'Databases: {0}'.format(client.all_dbs())

# Disconnect from the server
client.disconnect()
"""
{
  "username": "******",
  "password": "******",
  "host": "31a28f48-fad6-42c0-9d0b-9935659c8b1c-bluemix.cloudant.com",
  "port": 443,
  "url": "https://*****:*****@31a28f48-fad6-42c0-9d0b-9935659c8b1c-bluemix.cloudant.com"
}
"""
Example #16
0
#http://stackoverflow.com/questions/27628053/uploading-and-downloading-files-with-flask
#http://code.runnable.com/UiIdhKohv5JQAAB6/how-to-download-a-file-generated-on-the-fly-in-flask-for-python
#http://stackoverflow.com/questions/14343812/redirecting-to-url-in-flask
#http://pythoncentral.io/hashing-files-with-python/
import os,time
from flask import Flask, jsonify, request, render_template, make_response, redirect,url_for
from cloudant.client import Cloudant
import hashlib
from mimetypes import MimeTypes

USERNAME = '******'
PASSWORD = '******'
URL = "https://*****:*****@8449445a-8fcb-4470-8f88-3803ba8f7c14-bluemix.cloudant.com"
client = Cloudant(USERNAME, PASSWORD, url=URL)
client.connect()
dbs=  client.all_dbs()
print dbs
for db in dbs:
    print  str(db)
    if str(db) == 'my_database':
        temp = 1
        break
    else:
        temp =0


if temp==1:
    my_database = client['my_database']
else:
    my_database = client.create_database('my_database')
Example #17
0
class CloudantUserStore(object):
    def __init__(self, cloudant_username, cloudant_password, cloudant_url,
                 db_name):
        """
        Creates a new instance of CloudantUserStore.
        Parameters
        ----------
        cloudant_username - The username for the cloudant instance
        cloudant_password - The password for the cloudant instance
        cloudant_url - The url of the of cloudant instance to connect to
        db_name - The name of the database to use
        """
        if cloudant_url.find('@') > 0:
            prefix = cloudant_url[0:cloudant_url.find('://') + 3]
            suffix = cloudant_url[cloudant_url.find('@') + 1:]
            cloudant_url = '{}{}'.format(prefix, suffix)
        self.client = Cloudant(cloudant_username,
                               cloudant_password,
                               url=cloudant_url)
        self.db_name = db_name

    def init(self):
        """
        Creates and initializes the database.
        """
        try:
            self.client.connect()
            print('Getting user database...')
            if self.db_name not in self.client.all_dbs():
                print('Creating user database {}...'.format(self.db_name))
                self.client.create_database(self.db_name)
            else:
                print('User database {} exists.'.format(self.db_name))
        finally:
            self.client.disconnect()

    # User

    def add_user(self, user_id):
        """
        Adds a new user to Cloudant if a user with the specified ID does not already exist.
        Parameters
        ----------
        user_id - The ID of the user
        """
        try:
            self.client.connect()
            return self.client[self.db_name][user_id]
        except KeyError:
            db = self.client[self.db_name]
            doc = {'_id': user_id, 'conversation_context': {}}
            return db.create_document(doc)
        finally:
            self.client.disconnect()

    def update_user(self, user, context):
        """
        Updates the user in Cloudant with the latest Watson Conversation context.
        Parameters
        ----------
        userId - The user doc stored in Cloudant
        context - The Watson Conversation context
        """
        try:
            self.client.connect()
            db = self.client[self.db_name]
            doc = db[user['_id']]
            doc['conversation_context'] = context
            return doc.save()
        finally:
            self.client.disconnect()
Example #18
0
class CloudantDialogStore(object):
    def __init__(self, cloudant_dialogname, cloudant_password, cloudant_url,
                 db_name):
        """
        Creates a new instance of CloudantDialogStore.
        Parameters
        ----------
        cloudant_dialogname - The dialogname for the cloudant instance
        cloudant_password - The password for the cloudant instance
        cloudant_url - The url of the of cloudant instance to connect to
        db_name - The name of the database to use
        """
        if cloudant_url.find('@') > 0:
            prefix = cloudant_url[0:cloudant_url.find('://') + 3]
            suffix = cloudant_url[cloudant_url.find('@') + 1:]
            cloudant_url = '{}{}'.format(prefix, suffix)
        self.client = Cloudant(cloudant_dialogname,
                               cloudant_password,
                               url=cloudant_url)
        self.db_name = db_name

    def init(self):
        """
        Creates and initializes the database.
        """
        try:
            self.client.connect()
            print('Getting dialog database...')
            if self.db_name not in self.client.all_dbs():
                print('Creating dialog database {}...'.format(self.db_name))
                self.client.create_database(self.db_name)
            else:
                print('Dialog database {} exists.'.format(self.db_name))
        finally:
            self.client.disconnect()

    def add_conversation(self, user_id):
        """
        Adds a new conversation to Cloudant.
        Parameters
        ----------
        user_id - The ID of the user
        """
        try:
            self.client.connect()
            db = self.client[self.db_name]
            conversation_doc = {
                'userId': user_id,
                'date': int(time.time() * 1000),
                'dialogs': []
            }
            return db.create_document(conversation_doc)
        finally:
            self.client.disconnect()

    def add_dialog(self, conversation_id, dialog):
        """
        Adds a new dialog to the conversation.
        Parameters
        ----------
        conversation_id - The ID of the conversation in Cloudant
        dialog - The dialog to add to the conversation
        """
        try:
            self.client.connect()
            db = self.client[self.db_name]
            converation_doc = db[conversation_id]
            converation_doc['dialogs'].append(dialog)
            return converation_doc.save()
        finally:
            self.client.disconnect()
Example #19
0
class CloudantDB(threading.Thread):

    def _readConfig(self):
        update = False

        if not os.path.isdir(self._homeDir):
            print "Creating homeDir"
            os.makedirs(self._homeDir)

        if os.path.isfile(self._configFileName):
            self._config.read(self._configFileName)
        else:
            print "Config file not found"
            update = True

        if not self._config.has_section('REDIS'):
            print "Adding Redis part"
            update = True
            self._config.add_section("REDIS")

        if not self._config.has_option("REDIS", "ServerAddress"):
            print "No Server Address"
            update = True
            self._config.set("REDIS", "ServerAddress", "<ServerAddress>")

        if not self._config.has_option("REDIS", "ServerPort"):
            print "No Server Port"
            update = True
            self._config.set("REDIS", "ServerPort", "6379")

        if not self._config.has_section('CLOUDANT'):
            print "Adding Cloudant part"
            update = True
            self._config.add_section("CLOUDANT")

        if not self._config.has_option("CLOUDANT", "ServerAddress"):
            print "No Server Address"
            update = True
            self._config.set("CLOUDANT", "ServerAddress", "<ServerAddress>")

        if not self._config.has_option("CLOUDANT", "Username"):
            print "No Username"
            update = True
            self._config.set("CLOUDANT", "Username", "Didditulle")

        if not self._config.has_option("CLOUDANT", "Password"):
            print "No Password"
            update = True
            self._config.set("CLOUDANT", "Password", "geheim")

        if update:
            with open(self._configFileName, 'w') as f:
                self._config.write(f)

    def __init__(self):
        threading.Thread.__init__(self)
        self.setDaemon(True)
        self._homeDir        = os.path.expanduser("~/.sensomatic")
        self._configFileName = self._homeDir + '/config.ini'
        self._config         = ConfigParser.ConfigParser()
        self._readConfig()
        self._redis          = redis.StrictRedis(host=self._config.get("REDIS", "ServerAddress"), port=self._config.get("REDIS", "ServerPort"), db=0)
        self._cloudant       = Cloudant(self._config.get("CLOUDANT", "Username"), self._config.get("CLOUDANT", "Password"), url=self._config.get("CLOUDANT", "ServerAddress"))
        self.checkDB()

    def checkDB(self):
        self._cloudant.connect()
        if "usshorizon" in self._cloudant.all_dbs():
            self._database = self._cloudant['usshorizon']
            return True
        else:
            print "Create DB"
            self._database = self._cloudant.create_database('usshorizon')
            if self._database.exists():
                print "Success"
                return True
            else:
                print "Error"
                return False

    def getData(self):
        data = {'timestamp':time.time()}
        for key in self._redis.keys():
            k = key.split('/')
            l = len(k)
            w = data
            for i in range(l):
                if k[i] not in w:
                    w[k[i]] = {}
                w = w[k[i]]
                if i == l - 1:
                    w['value'] = self._redis.get(key)
        return data

    def run(self):
        while True:
            d = self.getData()
            # print d
            try:
                if self.checkDB():
                    self._database.create_document(d)
            except Exception as e:
                print e
                time.sleep(60)
            time.sleep(10)
Example #20
0
class PHP():
    def __init__(self):
        random.seed()
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.setup(12, GPIO.OUT, initial=GPIO.LOW)
        GPIO.setup(16, GPIO.OUT, initial=GPIO.LOW)
        GPIO.setup(18, GPIO.OUT, initial=GPIO.LOW)
        self._get_config()
        CF.Key.set(self._config.get("MICROSOFT", "FaceKey"))
        self._visual_recognition = VisualRecognitionV3(
            '2016-05-20', api_key=self._config.get("IBM", "visualkey"))
        self._camera = picamera.PiCamera()
        if not os.path.isdir(self._config.get("DIRS", "Capture")):
            print "Creating Capture Dir"
            os.makedirs(self._config.get("DIRS", "Capture"))
        if not os.path.isdir(self._config.get("DIRS", "Mosaic")):
            print "Creating Mosaic Dir"
            os.makedirs(self._config.get("DIRS", "Mosaic"))
        self._db = Cloudant(self._config.get("CLOUDANT", "Username"),
                            self._config.get("CLOUDANT", "Password"),
                            url=self._config.get("CLOUDANT", "Host"),
                            connect=True)

    def _generate_game_goals(self):
        self._game_goals = {}
        self._game_goals['total'] = random.randint(5, 10)
        self._game_goals['age'] = random.randint(
            15, 42) * self._game_goals['total']
        self._game_goals['male'] = random.randint(0, self._game_goals['total'])
        self._game_goals['female'] = random.randint(
            0, (self._game_goals['total'] - self._game_goals['male']))
        self._game_goals['glasses'] = random.randint(0,
                                                     self._game_goals['total'])
        self._game_goals['hair'] = random.randint(0, self._game_goals['male'])
        self._game_goals['happy'] = random.randint(0,
                                                   self._game_goals['total'])
        self._game_goals['surprised'] = random.randint(
            0, (self._game_goals['total'] - self._game_goals['happy']))

    def _get_config(self):
        update = False
        config_file = "config.txt"
        self._config = ConfigParser.ConfigParser()

        if os.path.isfile(config_file):
            self._config.read(config_file)
        else:
            print "Config file not found"
            update = True

        if not self._config.has_section('DIRS'):
            print "Adding DIRS part"
            update = True
            self._config.add_section("DIRS")

        if not self._config.has_option("DIRS", "Capture"):
            print "No Capture Directory"
            update = True
            self._config.set("DIRS", "Capture", "/tmp/php/captures")

        if not self._config.has_option("DIRS", "Mosaic"):
            print "No Mosaic Directory"
            update = True
            self._config.set("DIRS", "Mosaic", "/tmp/php/mosaic")

        if not self._config.has_section('CLOUDANT'):
            print "Adding Cloudant part"
            update = True
            self._config.add_section("CLOUDANT")

        if not self._config.has_option("CLOUDANT", "Host"):
            print "No CLOUDANT Host"
            update = True
            self._config.set("CLOUDANT", "Host", "<Host>")

        if not self._config.has_option("CLOUDANT", "Username"):
            print "No Username"
            update = True
            self._config.set("CLOUDANT", "Username", "Didditulle")

        if not self._config.has_option("CLOUDANT", "Password"):
            print "No Password"
            update = True
            self._config.set("CLOUDANT", "Password", "geheim")

        if not self._config.has_section('MICROSOFT'):
            print "Adding Microsoft part"
            update = True
            self._config.add_section("MICROSOFT")

        if not self._config.has_option("MICROSOFT", "FaceKey"):
            print "No Microsoft FaceKey"
            update = True
            self._config.set("MICROSOFT", "FaceKey", "<Facekey>")

        if not self._config.has_section('IBM'):
            print "Adding IBM part"
            update = True
            self._config.add_section("IBM")

        if not self._config.has_option("IBM", "VisualKey"):
            print "No IBM VisualKey"
            update = True
            self._config.set("IBM", "VisualKey", "<VisualKey>")

        if update:
            with open(config_file, 'w') as f:
                self._config.write(f)
            sys.exit(1)

    def _capture_picture(self):
        self._current_picture_name = "%s/%s.jpg" % (self._config.get(
            "DIRS", "Capture"), uuid.uuid4())
        self._camera.capture(self._current_picture_name)

    def _get_ms_results(self):
        # https://www.microsoft.com/cognitive-services/en-US/subscriptions
        self._ms_result = CF.face.detect(
            self._current_picture_name,
            landmarks=False,
            attributes='age,gender,smile,facialHair,glasses,emotion')

    def _get_ibm_results(self):
        with open(self._current_picture_name, 'rb') as image_file:
            #self._ibm_face_results = self._visual_recognition.detect_faces(images_file=image_file)
            self._ibm_result = self._visual_recognition.classify(
                images_file=image_file)

    def _store_in_db(self):
        if "ms_results" not in self._db.all_dbs():
            self._db.create_database('ms_results')
            print "Creating ms_results"

        if "ibm_results" not in self._db.all_dbs():
            self._db.create_database('ibm_results')
            print "Creating ibm_results"

        if self._ms_result is not None:
            ms_database = self._db['ms_results']
            j = {}
            j['result'] = self._ms_result
            j['image_name'] = self._current_picture_name
            ms_document = ms_database.create_document(j)

            if ms_document.exists():
                print 'SUCCESS MS!!'

        if self._ibm_result is not None:
            ibm_database = self._db['ibm_results']
            j = {}
            j['result'] = self._ibm_result
            j['image_name'] = self._current_picture_name
            ibm_document = ibm_database.create_document(j)

            if ibm_document.exists():
                print 'SUCCESS IBM!!'

    def _enhance_image(self):
        img = cv2.imread(self._current_picture_name)
        logo = cv2.imread("ibm-bluemix.png")
        slogo = cv2.resize(logo,
                           None,
                           fx=0.26,
                           fy=0.26,
                           interpolation=cv2.INTER_CUBIC)

        for currFace in self._ms_result:
            faceRectangle = currFace['faceRectangle']
            emotion = self._extract_emotion(
                currFace['faceAttributes']['emotion'])
            age = currFace['faceAttributes']['age']
            textToWrite = "%s - %s" % (age, emotion)
            cv2.rectangle(img, (faceRectangle['left'], faceRectangle['top']),
                          (faceRectangle['left'] + faceRectangle['width'],
                           faceRectangle['top'] + faceRectangle['height']),
                          color=(255, 255, 0),
                          thickness=3)
            cv2.putText(img, textToWrite,
                        (faceRectangle['left'], faceRectangle['top'] - 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0), 1)

        cv2.rectangle(img, (0, 0), (1680, 60), color=(0, 0, 0), thickness=60)
        textToWrite = "Goal   : Age:%03.1f / Male:%d / Female:%d / Glasses:%d / Beard:%d / Happy:%d / Surprised:%d" % (
            self._game_goals['age'], self._game_goals['male'],
            self._game_goals['female'], self._game_goals['glasses'],
            self._game_goals['hair'], self._game_goals['happy'],
            self._game_goals['surprised'])
        cv2.putText(img, textToWrite, (270, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.0,
                    (0, 255, 0), 3)
        textToWrite = "Current: Age:%03.1f / Male:%d / Female:%d / Glasses:%d / Beard:%d / Happy:%d / Surprised:%d" % (
            self._game_results['age'], self._game_results['male'],
            self._game_results['female'], self._game_results['glasses'],
            self._game_results['hair'], self._game_results['happy'],
            self._game_results['surprised'])
        cv2.putText(img, textToWrite, (270, 80), cv2.FONT_HERSHEY_SIMPLEX, 1.0,
                    (0, 0, 255), 3)
        #if self._check_game():
        #    cv2.putText(img, "You Won!", (0, 120), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 3)
        #else:
        #    cv2.putText(img, "Find more people :-)", (0, 120), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (  0, 0, 255), 3)

        rows, cols, channels = slogo.shape
        roi = img[0:rows, 0:cols]
        img2gray = cv2.cvtColor(slogo, cv2.COLOR_BGR2GRAY)
        ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
        mask_inv = cv2.bitwise_not(mask)
        img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
        img2_fg = cv2.bitwise_and(slogo, slogo, mask=mask)
        dst = cv2.add(img1_bg, img2_fg)
        img[0:rows, 0:cols] = dst

        cv2.imwrite('/tmp/test.png', img)

    def _cut_faces(self):
        img = cv2.imread(self._current_picture_name)

        for currFace in self._ms_result:
            faceRectangle = currFace['faceRectangle']
            x = faceRectangle['left']
            y = faceRectangle['top']
            w = faceRectangle['width']
            h = faceRectangle['height']

            crop_img = img[y:y + h, x:x + w]
            newfilename = "%s/%s.jpg" % (self._config.get(
                "DIRS", "Mosaic"), uuid.uuid4())
            cv2.imwrite(newfilename, crop_img)

    def _extract_emotion(self, inmotion):
        score = 0.0
        emotion = "neural"
        for e in inmotion:
            if inmotion[e] > score:
                score = inmotion[e]
                emotion = e
        return emotion

    def _extract_game_results(self):
        self._game_results = {}
        self._game_results['total'] = 0
        self._game_results['age'] = 0
        self._game_results['male'] = 0
        self._game_results['female'] = 0
        self._game_results['glasses'] = 0
        self._game_results['hair'] = 0
        self._game_results['happy'] = 0
        self._game_results['surprised'] = 0

        for currFace in self._ms_result:
            self._game_results['total'] += 1
            self._game_results['age'] += currFace['faceAttributes']['age']
            if currFace['faceAttributes']['gender'] == "male":
                self._game_results['male'] += 1
            if currFace['faceAttributes']['gender'] == "female":
                self._game_results['female'] += 1
            if currFace['faceAttributes']['glasses'] != "NoGlasses":
                self._game_results['glasses'] += 1
            if currFace['faceAttributes']['facialHair']['beard']     > 0.5 or \
               currFace['faceAttributes']['facialHair']['moustache'] > 0.5 or \
               currFace['faceAttributes']['facialHair']['sideburns'] > 0.5:
                self._game_results['hair'] += 1
            if self._extract_emotion(
                    currFace['faceAttributes']['emotion']) == "happiness":
                self._game_results['happy'] += 1
            if self._extract_emotion(
                    currFace['faceAttributes']['emotion']) == "surprise":
                self._game_results['surprised'] += 1

    def _check_game(self):
        if self._game_results['age']       >= self._game_goals['age']      and \
           self._game_results['male']      >= self._game_goals['male']     and \
           self._game_results['female']    >= self._game_goals['female']   and \
           self._game_results['glasses']   >= self._game_goals['glasses']  and \
           self._game_results['hair']      >= self._game_goals['hair']     and \
           self._game_results['happy']     >= self._game_goals['happy']    and \
           self._game_results['surprised'] >= self._game_goals['surprised']:
            GPIO.output(18, True)
            return True
        else:
            return False

    def main_loop(self):
        in_game = True
        while True:
            GPIO.output(12, False)
            GPIO.output(16, False)
            GPIO.output(18, False)
            self._generate_game_goals()
            in_game = True
            while in_game:
                while GPIO.input(3):
                    time.sleep(0.3)
                    if not GPIO.input(5):
                        in_game = False
                        break
                    print "wait"
                print "Capture"
                GPIO.output(16, True)
                GPIO.output(12, True)
                self._capture_picture()
                GPIO.output(16, False)
                self._get_ms_results()
                self._get_ibm_results()
                self._cut_faces()
                self._extract_game_results()
                self._enhance_image()
                GPIO.output(12, False)
                print json.dumps(self._ms_result,
                                 sort_keys=True,
                                 indent=4,
                                 separators=(',', ': '))
                print json.dumps(self._ibm_result,
                                 sort_keys=True,
                                 indent=4,
                                 separators=(',', ': '))
                print self._game_goals
                print self._game_results
                call(["killall", "fbi"])
                self._store_in_db()
    session = client.session()
    if session:
        print('Username: {0}'.format(session.get('userCtx', {}).get('name')))

    if args.match:
        re_match = re.compile(args.match)
        print ('Regular expresion will be used to filter databases')

    if args.exclude:
        re_exclude = re.compile(args.exclude)
        print ('Regular expresion will be used to filter databases for exclusion')

    print ("===")

    with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
        future_import = {executor.submit(process_database, database): database for database in client.all_dbs()}
        for future in concurrent.futures.as_completed(future_import):
            file = future_import[future]
            try:
                data = future.result()
            except Exception as exc:
                print('%r generated an exception: %s' % (file, exc))
            else:
                print(data)
    
    print ("Compressing DUMP folder")
    shutil.make_archive("dump", 'zip', path_dump)

    print ("all done!")
Example #22
0
from cloudant.error import CloudantException
from cloudant.result import Result, ResultByKey
from datetime import datetime
import concurrent.futures
import asyncio

client = Cloudant(
    "e8425b33-b22d-46b2-810d-99ddd319e9b9-bluemix",
    "52743ce84f5e992bf1682440998c43138b44873bda027b138b6f7cd714c10cef",
    url=
    "https://*****:*****@e8425b33-b22d-46b2-810d-99ddd319e9b9-bluemix.cloudant.com"
)
client.connect()
databaseName = "names"

print(client.all_dbs())
print(type(client.all_dbs()))

#'_id': 'julia30'+str(i), # Setting _id is optional

if databaseName in client.all_dbs():
    my_database = client[databaseName]
else:
    my_database = client.create_database(databaseName)

# for i in range(0,100):
#     data = {
#    '_id':'id'+str(i),
#     'name': 'XXXX'+str(i),
#     'lastname': 'YYYY'+str(i),
#     'date': str(datetime.now())
Example #23
0
from cloudant.client import Cloudant
import json
import random

credentials = json.load(open('userCred1.json'))
client = Cloudant(credentials['username'],
                  credentials['password'],
                  url=credentials['url'])
client.connect()
print client.all_dbs()

#userDB = client.create_database("user")
userDB = client['user']
doc = userDB['b539ab74bc9bc3e43a4b40040a66fa35']
doc['blockWebs'].append('instagram.com')
doc.save()
Example #24
0
def main():
    USERNAME = "******"
    PASSWORD = "******"
    cloudant_url = "https://*****:*****@ba62d85b-fc08-4706-8a9e-ba91bfbf6174-bluemix.cloudant.com"

    my_database_name_in_cloudant = 'abc'

    #client = Cloudant(USERNAME, PASSWORD, account=ACCOUNT_NAME)
    client = Cloudant(USERNAME, PASSWORD, url=cloudant_url)
    # or using url
    # client = Cloudant(USERNAME, PASSWORD, url='https://acct.cloudant.com')

    # Connect to the server
    client.connect()

    # Perform client tasks...
    session = client.session()
    print('Username: {0}'.format(session['userCtx']['name']))
    print('Databases: {0}'.format(client.all_dbs()))

    # Connection Established....Open My Database Now
    my_database = client[my_database_name_in_cloudant]
    # my_document = my_database['0ee5818002b8302e815f619a1521679f']
    # print(my_document)
    '''
    # Send Data document in Database by reading the input CSV file using my_database object
    csvfile = open('file1.csv', 'r')

    fieldnames = ("member_id", "product id", "date", "number of helpful feedbacks", "number of feedbacks", "rating", "title", "body")
    reader = csv.DictReader(csvfile, fieldnames, delimiter='\t')  # Create a dictionary object using the field names

    for row in reader:  # Take each row from dictionary and convert as needed and prepare another dictionary object
        my_data_doc = {'member_id': row['member_id'], 'product id': row['product id'], 'date': row['date'],
                'number of helpful feedbacks': int(row['number of helpful feedbacks']),
                'number of feedbacks': int(row['number of feedbacks']), 'rating': round(float(row['rating']), 1),'title': row['title'],
                'body': row['body']}
        time.sleep(0.7) #  Otherwise server raises Too many requests..10 per second can accept
        my_database.create_document(my_data_doc)  # Send the data to database


    #Use Get Method
    # Define the end point and parameters
    retrieve_target = '_all_docs'
    end_point = '{0}/{1}/{2}'.format(cloudant_url, my_database_name_in_cloudant, retrieve_target)
    #print(end_point)
    params = {'include_docs': 'true'}

    # Issue the request
    #response = client.r_session.get(end_point, params=params)

    # Display the response content
    #print(response.json())
'''

    # Create Index
    database_handler = cloudant.database.CloudantDatabase(
        client, my_database_name_in_cloudant)
    # index_response = database_handler.create_query_index(design_document_id='py_ddoc', index_name='py_index', index_type='json', fields=['member id'])
    # print(index_response)
    # print(database_handler.get_query_indexes(raw_result=True))
    # Disconnect from the server
    '''
    # Start Query :: Select Query # Assignment Query: 1
    selector = {'member_id': {'$eq': 'A1004AX2J2HXGL'}}
    docs_collection = database_handler.get_query_result(selector)
    # for each_doc in docs_collection:
    #    print(each_doc)

    # JSON output
    docs_collection = database_handler.get_query_result(selector, raw_result=True)
    # for each_doc in docs_collection['docs']:
    #   print(each_doc)


    # Start Query :: Select Query # Assignment Query: 3
    selector = {'rating': {'$lt': 3}}
    fields = ['product id']
    docs_collection = database_handler.get_query_result(selector, fields)
    for each_doc in docs_collection:
        print(each_doc)
'''

    # Search and Group Query:: Assignment Query 2/3
    # resp = database_handler.get_search_result(ddoc_id='py_ddoc', index_name='py_index', query='member_id:A*', group_field='member_id', counts=["member_id"])
    resp = database_handler.get_search_result(ddoc_id='py_ddoc',
                                              index_name='py_index',
                                              query='*:*',
                                              counts=["member_id"])
    print(resp['counts'])

    client.disconnect()
Example #25
0
                    my_doc.save()

    else:
        print "there"
        my_database = client.create_database('parking')
        if my_database.exists():
            print 'Table creation SUCCESS!!'
            # Create document content data
        data = commonCreation(key)
        # Create a document using the Database API
        my_document = my_database.create_document(data)


# def checkForUpdate:

if 'parking' in client.all_dbs():
    #Reading data into local dictionary
    my_daata = client['parking']
    for doc in my_daata:
        pin = str(doc['zipcode']) + str(doc['pno']) + str(doc['_id'])
        masterdict[pin] = doc['status']

while True:
    serInp = ser.readline()
    serInp = serInp.strip()
    print serInp
    inps = serInp.split("_")
    if len(inps) == 4:
        id = inps[2]
        zipcode = inps[3]
        rpino = inps[1]
Example #26
0
class CloudantDB(threading.Thread):
    def _readConfig(self):
        update = False

        if not os.path.isdir(self._homeDir):
            print "Creating homeDir"
            os.makedirs(self._homeDir)

        if os.path.isfile(self._configFileName):
            self._config.read(self._configFileName)
        else:
            print "Config file not found"
            update = True

        if not self._config.has_section('REDIS'):
            print "Adding Redis part"
            update = True
            self._config.add_section("REDIS")

        if not self._config.has_option("REDIS", "ServerAddress"):
            print "No Server Address"
            update = True
            self._config.set("REDIS", "ServerAddress", "<ServerAddress>")

        if not self._config.has_option("REDIS", "ServerPort"):
            print "No Server Port"
            update = True
            self._config.set("REDIS", "ServerPort", "6379")

        if not self._config.has_section('CLOUDANT'):
            print "Adding Cloudant part"
            update = True
            self._config.add_section("CLOUDANT")

        if not self._config.has_option("CLOUDANT", "ServerAddress"):
            print "No Server Address"
            update = True
            self._config.set("CLOUDANT", "ServerAddress", "<ServerAddress>")

        if not self._config.has_option("CLOUDANT", "Username"):
            print "No Username"
            update = True
            self._config.set("CLOUDANT", "Username", "Didditulle")

        if not self._config.has_option("CLOUDANT", "Password"):
            print "No Password"
            update = True
            self._config.set("CLOUDANT", "Password", "geheim")

        if update:
            with open(self._configFileName, 'w') as f:
                self._config.write(f)

    def __init__(self):
        threading.Thread.__init__(self)
        self.setDaemon(True)
        self._homeDir = os.path.expanduser("~/.sensomatic")
        self._configFileName = self._homeDir + '/config.ini'
        self._config = ConfigParser.ConfigParser()
        self._readConfig()
        self._redis = redis.StrictRedis(
            host=self._config.get("REDIS", "ServerAddress"),
            port=self._config.get("REDIS", "ServerPort"),
            db=0)
        self._cloudant = Cloudant(self._config.get("CLOUDANT", "Username"),
                                  self._config.get("CLOUDANT", "Password"),
                                  url=self._config.get("CLOUDANT",
                                                       "ServerAddress"))
        self.checkDB()

    def checkDB(self):
        self._cloudant.connect()
        if "usshorizon" in self._cloudant.all_dbs():
            self._database = self._cloudant['usshorizon']
            return True
        else:
            print "Create DB"
            self._database = self._cloudant.create_database('usshorizon')
            if self._database.exists():
                print "Success"
                return True
            else:
                print "Error"
                return False

    def getData(self):
        data = {'timestamp': time.time()}
        for key in self._redis.keys():
            k = key.split('/')
            l = len(k)
            w = data
            for i in range(l):
                if k[i] not in w:
                    w[k[i]] = {}
                w = w[k[i]]
                if i == l - 1:
                    w['value'] = self._redis.get(key)
        return data

    def run(self):
        while True:
            d = self.getData()
            # print d
            try:
                if self.checkDB():
                    self._database.create_document(d)
            except Exception as e:
                print e
                time.sleep(60)
            time.sleep(10)