Commit 4c087348 authored by Raphael Beer's avatar Raphael Beer

Fix: mongo authentication

parent 29c7e3df
......@@ -13,7 +13,7 @@ import time
from aiohttp import web
from bs4 import BeautifulSoup
from db import connect
from db import Database
from statistics import count_sensitives
from typeahead import test as test_typeahead
......@@ -238,7 +238,7 @@ class TwitterSession:
self.overshot = 0
# count the requests that failed because of rate limiting
if self.remaining is 0:
if self.remaining == 0:
log('[rate-limit] Limit hit by ' + str(self.username) + '.')
self.overshot += 1
......@@ -522,16 +522,6 @@ async def api(request):
else:
return web.json_response(result)
async def login_accounts(accounts, cookie_dir=None):
if cookie_dir is not None and not os.path.isdir(cookie_dir):
os.mkdir(cookie_dir, 0o700)
coroutines = []
for acc in accounts:
session = TwitterSession()
coroutines.append(session.login(*acc, cookie_dir=cookie_dir))
account_sessions.append(session)
await asyncio.gather(*coroutines)
async def login_guests():
for i in range(0, guest_session_pool_size):
session = TwitterSession()
......@@ -555,6 +545,8 @@ parser.add_argument('--host', type=str, default='127.0.0.1', help='hostname/ip w
parser.add_argument('--mongo-host', type=str, default='localhost', help='hostname or IP of mongoDB service to connect to')
parser.add_argument('--mongo-port', type=int, default=27017, help='port of mongoDB service to connect to')
parser.add_argument('--mongo-db', type=str, default='tester', help='name of mongo database to use')
parser.add_argument('--mongo-username', type=str, default='', help='name of user in mongo database')
parser.add_argument('--mongo-password', type=str, default='', help='password for user in mongo database')
parser.add_argument('--twitter-auth-key', type=str, default=None, help='auth key for twitter guest session', required=True)
parser.add_argument('--cors-allow', type=str, default=None, help='value for Access-Control-Allow-Origin header')
args = parser.parse_args()
......@@ -585,9 +577,15 @@ if args.debug is not None:
def run():
global db
db = connect(host=args.mongo_host, port=args.mongo_port)
db = Database(
host=args.mongo_host,
port=args.mongo_port,
username=args.mongo_username,
password=args.mongo_password,
db=args.mongo_db
)
loop = asyncio.get_event_loop()
loop.run_until_complete(login_accounts(accounts, args.cookie_dir))
# loop.run_until_complete(login_accounts(accounts, args.cookie_dir))
loop.run_until_complete(login_guests())
app = web.Application()
app.add_routes(routes)
......
......@@ -4,7 +4,7 @@ import sys
from pymongo import MongoClient, errors as MongoErrors, DESCENDING
class Database:
def __init__(self, host=None, port=27017, db='tester'):
def __init__(self, host=None, port=27017, username='', password='', db='tester'):
# collection name definitions
RESULTS_COLLECTION = 'results'
RATELIMIT_COLLECTION = 'rate-limits'
......@@ -13,7 +13,12 @@ class Database:
print('[mongoDB] Connecting to ' + host + ':' + str(port))
print('[mongoDB] Using Database `' + db + '`')
# client and DB
self.client = MongoClient(host, port, serverSelectionTimeoutMS=3)
self.client = MongoClient(
host=host,
port=port,
username=username,
password=password,
serverSelectionTimeoutMS=3)
self.db = self.client[db]
# collections
......@@ -41,8 +46,3 @@ class Database:
def get_result_by_screen_name(self, screen_name):
return self.results.find_one({ "profile.screen_name": screen_name }, sort=[("_id", DESCENDING)], projection={"_id": False})
def connect(host=None, port=27017, db='tester'):
if host is None:
raise ValueError('[mongoDB] Database constructor needs a `host`name or ip!')
return Database(host=host, port=port, db=db)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment