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()
Beispiel #2
0
def getLastSavedDbState():
    from cloudant.client import Cloudant
    from cloudant.result import Result
    from cloudant.adapters import Replay429Adapter
    from credentials import getUserName, getPassword, getUrl

    #Establish a connection to the cloudant database by providing the credentials necessary
    client = Cloudant(getUserName(),
                      getPassword(),
                      url=getUrl(),
                      adapter=Replay429Adapter(retries=10,
                                               initialBackoff=0.01))
    client.connect()

    #Connect to the "news_articles" database remotely
    db = client.get("news_articles", remote=True)
    #Get all records from the database
    resultset = Result(db.all_docs, include_docs=True)

    news_articles_list = list()
    #Loop through the database records
    for result in resultset:
        #Add all article titles to a list.
        #The news article titles are unique and will be used to check if an article already exists in the database
        news_articles_list.append(result["doc"]["article_title"])
    client.disconnect()

    return news_articles_list
Beispiel #3
0
def main(args):
    # Connect Cloudant
    db_client = Cloudant(args["USERNAME"],
                         args["PASSWORD"],
                         url=args["URL"],
                         adapter=Replay429Adapter(retries=10,
                                                  initialBackoff=0.01))
    db_client.connect()
    freee_tokens = db_client['freee_tokens']
    doc_id = args['TOKEN_DOC_ID']

    with Document(freee_tokens, doc_id) as document:
        refresh_token = document['refresh_token']
        payload = {
            'grant_type': 'refresh_token',
            'refresh_token': refresh_token,
            'client_id': args['CLIENT_ID'],
            'client_secret': args['CLIENT_SECRET'],
            'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob'
        }
        response = requests.post(
            'https://accounts.secure.freee.co.jp/public_api/token',
            data=payload).json()
        print(response)
        document['access_token'] = response['access_token']
        document['expires_in'] = response['expires_in']
        document['refresh_token'] = response['refresh_token']
        document['created_at'] = response['created_at']
    db_client.disconnect()
    return {'result': 'OK!'}
Beispiel #4
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
Beispiel #5
0
def create():

    # vcap_config = os.environ.get('VCAP_SERVICES')
    # decoded_config = json.loads(vcap_config)
    # for key in decoded_config.keys():
    #   if key.startswith('cloudant'):
    #       cloudant_creds = decoded_config[key][0]['credentials']
    # cloudant_user = str(cloudant_creds['username'])
    # cloudant_pass = str(cloudant_creds['password'])
    # cloudant_url = str(cloudant_creds['url'])
    # cloudant_user = "******"
    # cloudant_pass = "******"
    # cloudant_url = "https://*****:*****@e65ea042-3f91-4e24-86a1-91719ece201e-bluemix.cloudant.com"
    # #print(cloudant_creds)
    # client = Cloudant(cloudant_user, cloudant_pass, url=cloudant_url, connect=True, auto_renew=True)
    # my_database = client['users']

    global client
    if client:
        return client

    cloudant_user = "******"
    cloudant_pass = "******"
    cloudant_url = "https://*****:*****@e65ea042-3f91-4e24-86a1-91719ece201e-bluemix.cloudant.com"
    client = Cloudant(cloudant_user,
                      cloudant_pass,
                      url=cloudant_url,
                      connect=True,
                      auto_renew=True,
                      adapter=Replay429Adapter(retries=10,
                                               initialBackoff=0.01))
    return client
Beispiel #6
0
    def init_db(dbname='suppliers'):
        """
        Initialized Coundant database connection
        """
        opts = {}
        # Try and get VCAP from the environment
        if 'VCAP_SERVICES' in os.environ:
            Supplier.logger.info('Found Cloud Foundry VCAP_SERVICES bindings')
            vcap_services = json.loads(os.environ['VCAP_SERVICES'])
            # Look for Cloudant in VCAP_SERVICES
            for service in vcap_services:
                if service.startswith('cloudantNoSQLDB'):
                    opts = vcap_services[service][0]['credentials']

        # If Cloudant not found in VCAP_SERVICES
        # get it from the CLOUDANT_xxx environment variables
        if not opts:
            Supplier.logger.info(
                'VCAP_SERVICES and BINDING_CLOUDANT undefined.')
            opts = {
                "username": CLOUDANT_USERNAME,
                "password": CLOUDANT_PASSWORD,
                "host": CLOUDANT_HOST,
                "port": 5984,
                "url": "http://" + CLOUDANT_HOST + ":5984/"
            }

        if any(k not in opts
               for k in ('host', 'username', 'password', 'port', 'url')):
            raise DatabaseConnectionError('Error - Failed to retrieve options. ' \
                             'Check that app is bound to a Cloudant service.')

        Supplier.logger.info('Cloudant Endpoint: %s', opts['url'])
        try:
            if ADMIN_PARTY:
                Supplier.logger.info('Running in Admin Party Mode...')
            Supplier.client = Cloudant(opts['username'],
                                       opts['password'],
                                       url=opts['url'],
                                       connect=True,
                                       auto_renew=True,
                                       admin_party=ADMIN_PARTY,
                                       adapter=Replay429Adapter(
                                           retries=10, initialBackoff=0.01))

        except ConnectionError:
            raise DatabaseConnectionError(
                'Cloudant service could not be reached')

        # Create database if it doesn't exist
        try:
            Supplier.database = Supplier.client[dbname]
        except KeyError:
            # Create a database using an initialized client
            Supplier.database = Supplier.client.create_database(dbname)
        # check for success
        if not Supplier.database.exists():
            raise DatabaseConnectionError(
                'Database [{}] could not be obtained'.format(dbname))
 def test_backoff_arg_Replay429Adapter(self):
     """
     Test constructing a new Replay429Adapter with a configured initial backoff.
     """
     self.client = CouchDB(self.user,
                           self.pwd,
                           url=self.url,
                           adapter=Replay429Adapter(initialBackoff=0.1))
 def test_new_Replay429Adapter(self):
     """
     Test that a new Replay429Adapter is accepted as a parameter for a client.
     """
     self.client = CouchDB(self.user,
                           self.pwd,
                           url=self.url,
                           adapter=Replay429Adapter())
 def test_retries_arg_Replay429Adapter(self):
     """
     Test constructing a new Replay429Adapter with a configured number of retries.
     """
     self.client = CouchDB(self.user,
                           self.pwd,
                           url=self.url,
                           adapter=Replay429Adapter(retries=10))
 def test_args_Replay429Adapter(self):
     """
     Test constructing a new Replay429Adapter with configured retries and initial backoff.
     """
     self.client = CouchDB(self.user,
                           self.pwd,
                           url=self.url,
                           adapter=Replay429Adapter(retries=10,
                                                    initialBackoff=0.01))
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()
Beispiel #12
0
def dbLoadNewArticle(article):
    from cloudant.client import Cloudant
    from cloudant.adapters import Replay429Adapter
    from credentials import getUserName, getPassword, getUrl

    client = Cloudant(getUserName(),
                      getPassword(),
                      url=getUrl(),
                      adapter=Replay429Adapter(retries=10,
                                               initialBackoff=0.01))
    client.connect()

    db = client.get("news_articles", remote=True)
    #Create the database record
    new_doc = db.create_document(article)
    #Verify that the record was successfully added
    if new_doc.exists():
        print("Article was successfully added")

    client.disconnect()
Beispiel #13
0
    def __init__(self):
        common = Common()
        global cloudantInfo,dbInfo,env,mydb,client
        appConfig=common.readJson('config')
        if 'VCAP_SERVICES' in os.environ and 'cloudantNoSQLDB' in json.loads(os.getenv('VCAP_SERVICES')):
            vcap = json.loads(os.getenv('VCAP_SERVICES'))
            cloudantInfo = vcap['cloudantNoSQLDB'][0]['credentials']
        else:
            cloudantInfo=appConfig['CLOUDANT']

        client = Cloudant(cloudantInfo['username'], cloudantInfo['password'], url=cloudantInfo['url'], adapter=Replay429Adapter(retries=20, initialBackoff=0.1),
            connect=True, auto_renew=True)
        
        dbInfo = appConfig['DATABASE']
        env = appConfig['ENVIRONMENT']
        mydb = self.getDatabase(dbInfo[env])
def main(dict):
    doc = dict

    client = Cloudant.iam("3205c8bb-12e8-4e6b-b669-fbe872bf29df-bluemix",
                          "oxXUO9-AR5XOCXcQ9EJaVtIAY9DLPx77eUFCv2yrChqL",
                          connect=True,
                          adapter=Replay429Adapter(retries=10,
                                                   initialBackoff=0.01))

    database_name = "auxilium"
    my_database = client[database_name]
    if my_database.exists():
        print('SUCCESS!!')

        slati = float(doc['latitude'])
        slongi = float(doc['longitude'])
        radius = 10000
        keyword = doc['keyword'].lower()

        URL = "https://3205c8bb-12e8-4e6b-b669-fbe872bf29df-bluemix.cloudant.com/auxilium/_design/geodd/_geo/newGeoIndex"
        PARAMS = (('lat', slati), ('lon', slongi), ('radius', radius),
                  ('limit', 20), ('relation', 'contains'), ('include_docs',
                                                            'true'))
        HEADERS = {'Content-Type': 'application/json'}

        r = requests.get(url=URL, params=PARAMS, headers=HEADERS)
        data = r.json()
        rows = data['rows']
        final = []
        result = []

        if len(rows) == 0:
            return {'message': 'no posts'}

        for row in rows:
            lati = float(row['doc']['geometry']['coordinates'][1])
            longi = float(row['doc']['geometry']['coordinates'][0])
            dist_btwn = distance((slati, slongi), (lati, longi))
            row['doc'].update({'distance': dist_btwn})

        for row in rows:
            if row['doc']['user_id'] != doc['user_id']:
                final.append(row)
            else:
                pass

        if len(final) == 0:
            return {'message': 'no result'}

        for final in final:
            if keyword in final['doc']['content']['description']:
                result.append(final)
            else:
                pass

        if len(result) == 0:
            return {'message': 'no search results'}

        return {'data': result}

    else:
        pass
Beispiel #15
0
def get_from_db_by_params(user_name, region_name, place_name, db_name, object_example, asdocs=False, dates=None, word_list=None, logic='or_logic', exact=False):
    print("get_from_db_by_params")
    print(user_name + ";" + region_name + ";" + place_name + ";" + db_name + ";" + str(type(object_example)) + ";" +
                                                                                       str(asdocs))
    get_db_client = Cloudant(serviceUsername, servicePassword, url=serviceURL, adapter=Replay429Adapter(retries=10, initialBackoff=0.01))
    try:
        our_list = []
        get_db_client.connect()
        db = get_db_client.get(db_name, remote=True)
        print("get_from_db_by_params logic is:" + logic)
        selector = generate_single_place_selector(user_name, region_name, place_name, dates, word_list, logic, exact)
        print("the selector is: " + str(selector))
        query_result = QueryResult(Query(db, selector=selector))
        if asdocs is True:
            for doc in query_result:
                our_list.append(doc)
        else:
            for doc in query_result:
                our_list.append(object_example.build_from_document(doc))
    except CloudantException as exc:
        print("CloudantException in get_from_db_by_params")
        print(exc)
        return exc
    except Exception as exc:
        print("non CloudantException exception in get_from_db_by_params")
        print(exc)
        return exc
    finally:
        get_db_client.disconnect()
        print("get_from_db_by_params id: " + str(db_name) + " len: " + str(len(our_list)))
    return our_list
Beispiel #16
0
 def connect(cls,adapter=Replay429Adapter(retries=10, initialBackoff=0.01)):
     """ Connect to the server """
     cls.client.connect()
Beispiel #17
0
    def init_db(dbname: str = "pets") -> None:
        """
        Initialized Cloudant database connection
        """
        opts = {}
        vcap_services = {}
        # Try and get VCAP from the environment or a file if developing
        if "VCAP_SERVICES" in os.environ:
            Pet.logger.info("Running in Bluemix mode.")
            vcap_services = json.loads(os.environ["VCAP_SERVICES"])
        # if VCAP_SERVICES isn't found, maybe we are running on Kubernetes?
        elif "BINDING_CLOUDANT" in os.environ:
            Pet.logger.info("Found Kubernetes Bindings")
            creds = json.loads(os.environ["BINDING_CLOUDANT"])
            vcap_services = {"cloudantNoSQLDB": [{"credentials": creds}]}
        else:
            Pet.logger.info("VCAP_SERVICES and BINDING_CLOUDANT undefined.")
            creds = {
                "username": COUCHDB_USERNAME,
                "password": COUCHDB_PASSWORD,
                "host": COUCHDB_HOST,
                "port": 5984,
                "url": "http://" + COUCHDB_HOST + ":5984/",
            }
            vcap_services = {"cloudantNoSQLDB": [{"credentials": creds}]}

        # Look for Cloudant in VCAP_SERVICES
        for service in vcap_services:
            if service.startswith("cloudantNoSQLDB"):
                cloudant_service = vcap_services[service][0]
                opts["username"] = cloudant_service["credentials"]["username"]
                opts["password"] = cloudant_service["credentials"]["password"]
                opts["host"] = cloudant_service["credentials"]["host"]
                opts["port"] = cloudant_service["credentials"]["port"]
                opts["url"] = cloudant_service["credentials"]["url"]

        if any(k not in opts
               for k in ("host", "username", "password", "port", "url")):
            Pet.logger.info("Error - Failed to retrieve options. "
                            "Check that app is bound to a Cloudant service.")
            exit(-1)

        Pet.logger.info("Cloudant Endpoint: %s", opts["url"])
        try:
            if ADMIN_PARTY:
                Pet.logger.info("Running in Admin Party Mode...")
            Pet.client = Cloudant(
                opts["username"],
                opts["password"],
                url=opts["url"],
                connect=True,
                auto_renew=True,
                admin_party=ADMIN_PARTY,
                adapter=Replay429Adapter(retries=10, initialBackoff=0.01),
            )
        except ConnectionError:
            raise AssertionError("Cloudant service could not be reached")

        # Create database if it doesn't exist
        try:
            Pet.database = Pet.client[dbname]
        except KeyError:
            # Create a database using an initialized client
            Pet.database = Pet.client.create_database(dbname)
        # check for success
        if not Pet.database.exists():
            raise AssertionError(
                "Database [{}] could not be obtained".format(dbname))
import requests, json
import atexit

from . import app

CL_URL      = app.config['CL_URL']
CL_USER     = app.config['CL_USER']
CL_PASS     = app.config['CL_PASS']
CL_AUTH     = app.config['CL_AUTH']
CL_MOVIEDB  = app.config['CL_MOVIEDB']
CL_AUTHDB   = app.config['CL_AUTHDB']
CL_RATINGDB = app.config['CL_RATINGDB']

from cloudant.client import Cloudant
from cloudant.adapters import Replay429Adapter

cloudant_client = Cloudant(CL_USER, CL_PASS, url=CL_URL, adapter=Replay429Adapter(retries=10))
cloudant_client.connect()

@atexit.register
def python_shutting_down():
    print('Disconnecting cloudant client')
    cloudant_client.disconnect()