Example #1
0
def test_is_singlton():
    """
    Checking to make sure that MongoDB is really a Singleton
    """
    a = mongodb.MongoDB()
    b = mongodb.MongoDB()
    assert a is b
Example #2
0
def stream_submissions(*args):
    mongo = mongodb.MongoDB(db, coll, topic)
    reddit = praw.Reddit(client_id=client_id,
                         client_secret=client_secret,
                         user_agent=user_agent)
    keywords = []
    subreddits = []

    if len(args[0]) > 3:
        raise Exception('Too Many Subreddits to Stream')

    for s in args[0]:
        subreddits.append(s)
    subreddits = "+".join(subreddits)

    for k in args[1]:
        keywords.append(k)

    try:
        while True:
            for submission in reddit.subreddit(
                    subreddits).stream.submissions():
                print(submission.id)
                if re.compile("|".join(keywords),
                              re.IGNORECASE).search(submission.title):
                    print("Accepted Submission: {} From {}".format(
                        submission.id, submission.subreddit))
                    mongo.insert_into_collection(
                        submission.id, submission.subreddit, submission.title,
                        submission.author, submission.created_utc,
                        submission.score, submission.num_comments)
                    time.sleep(1)
    except KeyboardInterrupt:
        pass
Example #3
0
def start_database_status_check(db, coll, topic):
    mongo = mongodb.MongoDB(db, coll, topic)
    schedule.every(10).seconds.do(mongo.check_post_maturity)
    while True:
        try:
            schedule.run_pending()
        except KeyboardInterrupt:
            break
Example #4
0
 def __init__(self):
     self.mdb = mongodb.MongoDB()
     self.collection = 'bugContent'
     self.cm = common.Common()
     self.titles = [
         u'缺陷编号', u'缺陷标题', u'缺陷描述', u'重现步骤', u'缺陷状态', u'所属终端', u'所属项目',
         u'开发人员', u'测试人员', u'创建日期'
     ]
Example #5
0
def test_write_to_db_mixed():
    db = mongodb.MongoDB()
    secret_id = ObjectId(
        db.create_secret("ultra_mega_секрет", "strong_password"))

    all_secrets = db._instance.db.secrets
    secret = all_secrets.find_one({"_id": secret_id})

    if "strong_password" == secret["code_phrase"]:
        assert "ultra_mega_секрет" == b64decode(
            secret["secret"]).decode("UTF-8")
Example #6
0
def test_write_to_db_cyrillic():
    db = mongodb.MongoDB()
    secret_id = ObjectId(
        db.create_secret("ультра мега секрет", "сильный пароль"))

    all_secrets = db._instance.db.secrets
    secret = all_secrets.find_one({"_id": secret_id})

    if "сильный пароль" == secret["code_phrase"]:
        assert "ультра мега секрет" == b64decode(
            secret["secret"]).decode("UTF-8")
Example #7
0
def test_only_once_read():
    """
    Checking to see if reading is really only available once.
    """
    db = mongodb.MongoDB()
    secret_id = db.create_secret("ultra_mega_секрет", "strong_password")

    secret = db.get_secret(secret_id, "strong_password")

    if secret != "ultra_mega_секрет":
        assert False

    assert db._instance.db.secrets.find_one({"_id": ObjectId(secret_id)
                                             }) is None
Example #8
0
def test_write_to_db():
    """
    Checking the DB entry.
    """
    db = mongodb.MongoDB()
    secret_id = ObjectId(
        db.create_secret("ultra_mega_secret", "strong_password"))

    all_secrets = db._instance.db.secrets
    secret = all_secrets.find_one({"_id": secret_id})

    if "strong_password" == secret["code_phrase"]:
        assert "ultra_mega_secret" == b64decode(
            secret["secret"]).decode("UTF-8")
Example #9
0
'''
Author: Thomas Theisen

Objective: Test program for postgresdb.py functionality

'''

import mongodb
import options

db = options.mongodb_database_healthcare
coll = options.mongodb_collection_healthcare
mongo = mongodb.MongoDB(db, coll, 'topic')
print(mongo.return_post_author('b332ad'))
Example #10
0
# Python Modules
#-----------------------------------------------------------------------------#
import pickle

# Interal Modules
#-----------------------------------------------------------------------------#
from kafka import KafkaProducer
import mongodb
import options

db = options.mongodb_database_healthcare
coll = options.mongodb_collection_healthcare
producer = KafkaProducer(bootstrap_servers=['localhost:9092'])

mongo = mongodb.MongoDB(db, coll, None)


def insertion(connections, postid, parent, child, child_author):

    if not bool(connections):
        current_postid = None
    else:
        current_postid = next(iter(connections))

    if not bool(connections):
        print('New Post Incoming')
        post_author = mongo.return_post_author(postid)

        #Create pointer between id and author
        connections[postid] = {}
Example #11
0
"""
Tests para comprobar el funcionamiento de las diferentes rutas encargadas de
obtener y mostrar un informe estadístico en particular de los tres existentes.

@author: Lidia Sánchez Mérida
"""
import sys
sys.path.append("src/estadisticas")
import estadisticas_rest
app = estadisticas_rest.app.test_client()
sys.path.append("src")
import os
import mongodb
import estadisticas
"""Creamos un objeto de la base de datos y lo configuramos."""
bd = mongodb.MongoDB(os.environ.get("MONGODB_URI"), 'PetfinderBD', 'estadisticas')
"""Objeto que nos conecta con la clase Mascotas."""
estd = estadisticas.Estadisticas(bd)

def test_ver_estadistica_ninios():
    """Test 1: obtiene el primer tipo de informe donde se muestran las mascotas que
        mejor se relacionan con niños."""
    respuesta = app.get('/ver_estadistica_ninios')
    if (respuesta.status_code == 400):
        """No se ha generado el informe aún."""
        assert (respuesta.status_code == 400)
    else:
        assert (respuesta.status_code == 200 and respuesta.headers["Content-Type"] == "application/json") 
        
def test_ver_estadistica_tipos_mascotas():
    """Test 2: obtiene el segundo tipo de informe donde se muestran las mascotas
Example #12
0
import json
import mascotas
import sys
sys.path.append("src")
from excepciones import EmptyCollection, WrongPetIndex, WrongNumberSearchParameters, WrongSearchParametersValues, PetsNotFound, MaxPetfinderRequestsExceeded

import os
import mongodb

app = Flask(__name__)
"""Añadimos caché para comprobar si mejoran las prestaciones."""
app.config["CACHE_DEFAULT_TIMEOUT"] = 36000
app.config['CACHE_TYPE'] = 'simple'
cache = Cache(app)
"""Creamos un objeto de la base de datos y lo configuramos."""
bd = mongodb.MongoDB(os.environ.get("MONGODB_URI"), 'PetfinderBD', 'mascotas')
"""Objeto que nos conecta con la clase Mascotas."""
m = mascotas.Mascotas(bd)


@app.route("/")
def index():
    return Response("Microservicio REST para recopilar datos de mascotas.",
                    status=200)


@app.route("/obtener_mascotas", methods=['GET'])
@cache.cached()
def obtener_mascotas():
    """Servicio REST que obtiene los datos de todas las mascotas que préviamente
        han sido descargados por el microservicio periódico de Celery. Para ello
from bs4 import BeautifulSoup, Comment
import os
import requests
import mongodb
import logging
import threading

db = mongodb.MongoDB('localhost', 27017)
log_file = "./logger.log"
logging.basicConfig(filename=log_file, level=logging.DEBUG)


def get_html_code(url):
    headers = {
        'Connection':
        'close',
        'User-Agent':
        'MMozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0'
    }
    try:
        r = requests.get(url, headers=headers, timeout=30)
        r.encoding = 'UTF-8'
        page = r.text
        return page
    except Exception as e:
        logging.exception(e)
        return False


def parse_table(table):
    assert table is not None
Example #14
0
import json
import os

import flask
import mongodb

app = flask.Flask(__name__)

mongodb_host = os.getenv('MONGODB_HOST', 'localhost')
mongodb_port = os.getenv('MONGODB_PORT', '27017')

mongodb_database = os.getenv('MONGODB_DATABASE')
mongodb_username = os.getenv('MONGODB_USERNAME')
mongodb_password = os.getenv('MONGODB_PASSWORD')

mdb = mongodb.MongoDB(mongodb_host, mongodb_port, mongodb_database,
                      mongodb_username, mongodb_password)


@app.route('/<collection>', methods=['POST'])
def receiver(collection):
    mdb.create_or_update(collection, flask.request.get_json())
    return flask.Response("Thanks!", mimetype='text/plain')


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5001)
Example #15
0
import concurrent.futures
import numpy as np
import mongodb
import tushare_get
import pipeline
from functions import STRING_FORMAT, UPDATE_MAP

if __name__ == '__main__':
    news_source = sys.argv[
        1]  #  Note that the first argv is the name of the python script.
    choose_mode = sys.argv[2]
    # news_source = input("News source:")
    # choose_mode = input("Choose a mode(g/f/u):")
    print('Prepare to run %s - %s' % (choose_mode, news_source))
    logging.basicConfig(level=logging.WARNING)
    mongo = mongodb.MongoDB()
    pipeline = pipeline.Pipeline()
    if choose_mode != 'u':
        tushare = tushare_get.Tushare()
        date_info = {}
        with open('/home/ubuntu/Desktop/tommy/PyFina/news_update/date.json',
                  'r+') as json_file:
            date_info = json.load(json_file)
        earlist_date = (datetime.strptime(
            date_info['check_out_date'][news_source], STRING_FORMAT) -
                        timedelta(days=1)).strftime(STRING_FORMAT)
        current_date = date.today().strftime(STRING_FORMAT)

        with concurrent.futures.ThreadPoolExecutor(max_workers=6) as executor:
            if choose_mode == 'g':
                get_news = executor.submit(tushare.get_news_multi, news_source,
Example #16
0
# ecoding=utf-8
# Author: 翁彦彬 | Sven_Weng
# Email : [email protected]
import mongodb
import time
import sys
reload(sys)
sys.setdefaultencoding('utf8')

mdb = mongodb.MongoDB()


def getProjectName():
    """
	返回所有项目名称
	:return: List, 项目名称
	"""
    projectObj = mdb.dbQueryAllRecord('project')
    projects = [x['name'] for x in projectObj]
    return projects


def getBugStatus(projectName, archiveTime=time.strftime('%Y%m%d')):
    """
	根据项目名称按照日期归档bug数量
	:param projectName: str, 项目名称
	:return:
	"""
    weijiejue = 0
    yixiufu = 0
    yiyanzheng = 0