Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修一下mongoDb的连接 #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ATRIlib/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def __init__(self):
self.osuclientid = os.getenv('OSU_CLIENT_ID')
self.osuclientsecret = os.getenv('OSU_CLIENT_SECRET')
# mongoDB 应该可以直接用 url 的方式连接 ?
self.mongodb = os.getenv('MONGO_URI')
self.mongodb_url = os.getenv('MONGO_URL')

if self.osuclientid and self.osuclientsecret:
if self.osuclientid and self.osuclientsecret and self.mongodb_url:
return

self.config = configparser.ConfigParser()
Expand All @@ -19,3 +19,5 @@ def __init__(self):
self.osuclientid = self.config['OSUAPI']['client_id']
if not self.osuclientsecret:
self.osuclientsecret = self.config['OSUAPI']['client_secret']
if not self.mongodb_url:
self.mongodb_url = "mongodb://localhost:27017"
21 changes: 14 additions & 7 deletions ATRIlib/Core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ATRIlib.Rosu import Rosu
from ATRIlib.Jobs import Jobs
from ATRIlib.Mtools import Tools
from ATRIlib.Config import Config
from ATRIlib.Dtools import ResultScreen, TDBA, BeatmapRankingscreeen

import numpy as np
Expand All @@ -20,28 +21,34 @@ def __init__(self):

self.mtools = Tools()

self.jobs = Jobs()

self.ppy = PPYapiv2()
self.ppy.get_token()

self.rosu = Rosu()

self.db_user = Mongodb('localhost', 27017, 'osu',
'user') # 数据库名字为osu,表名为user
mongodb_url = Config().mongodb_url

client = Mongodb.get_client(mongodb_url)

db_name = 'osu'

# 数据库名字为osu,表名为user
self.db_user = Mongodb(client , db_name, 'user')

# 数据库名字为osu,表名为beatmap
self.db_score = Mongodb('localhost', 27017, 'osu', 'score')
self.db_score = Mongodb(client, db_name, 'score')

self.db_bind = Mongodb('localhost', 27017, 'osu', 'bind')
self.db_bind = Mongodb(client, db_name, 'bind')

self.db_group = Mongodb('localhost', 27017, 'osu', 'group')
self.db_group = Mongodb(client, db_name, 'group')

self.weight_list = [1.0, 0.95, 0.9025, 0.8573749999999998, 0.8145062499999999, 0.7737809374999998, 0.7350918906249998, 0.6983372960937497, 0.6634204312890623, 0.6302494097246091, 0.5987369392383787, 0.5688000922764597, 0.5403600876626367, 0.5133420832795048, 0.4876749791155295, 0.46329123015975304, 0.44012666865176536, 0.4181203352191771, 0.3972143184582182, 0.3773536025353073, 0.3584859224085419, 0.34056162628811476, 0.323533544973709, 0.3073568677250236, 0.2919890243387724, 0.27738957312183377, 0.26352009446574204, 0.2503440897424549, 0.23782688525533216, 0.22593554099256555, 0.21463876394293727, 0.2039068257457904, 0.19371148445850087, 0.18402591023557582, 0.174824614723797, 0.16608338398760714, 0.1577792147882268, 0.14989025404881545, 0.14239574134637467, 0.13527595427905592, 0.12851215656510312, 0.12208654873684796, 0.11598222130000556, 0.11018311023500528, 0.10467395472325501, 0.09944025698709226, 0.09446824413773763, 0.08974483193085075, 0.0852575903343082, 0.0809947108175928, 0.07694497527671315, 0.07309772651287749,
0.06944284018723361, 0.06597069817787193, 0.06267216326897833, 0.05953855510552941, 0.056561627350252934, 0.053733545982740286, 0.051046868683603266, 0.048494525249423104, 0.046069798986951946, 0.043766309037604346, 0.041577993585724136, 0.03949909390643792, 0.03752413921111602, 0.03564793225056022, 0.033865535638032206, 0.03217225885613059, 0.030563645913324066, 0.029035463617657863, 0.027583690436774964, 0.026204505914936217, 0.024894280619189402, 0.023649566588229934, 0.02246708825881844, 0.02134373384587751, 0.020276547153583634, 0.01926271979590445, 0.01829958380610923, 0.017384604615803767, 0.01651537438501358, 0.0156896056657629, 0.014905125382474753, 0.014159869113351013, 0.013451875657683464, 0.012779281874799289, 0.012140317781059324, 0.011533301892006359, 0.01095663679740604, 0.010408804957535737, 0.00988836470965895, 0.009393946474176, 0.008924249150467202, 0.00847803669294384, 0.008054134858296648, 0.007651428115381815, 0.007268856709612724, 0.006905413874132088, 0.006560143180425483, 0.006232136021404208]

self.result = ResultScreen()
self.ranking = BeatmapRankingscreeen()
self.tdba = TDBA()
self.jobs = Jobs(self.ppy, self.db_user, self.db_score)

# 数据模块-更新玩家信息
async def update_user_info(self, osuname):
Expand Down
8 changes: 4 additions & 4 deletions ATRIlib/Jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

class Jobs:

def __init__(self):
self.ppy = PPYapiv2()
def __init__(self, ppy, db_user, db_score):
self.ppy = ppy
self.ppy.get_token()

self.db_user = Mongodb('localhost', 27017, 'osu', 'user')
self.db_score = Mongodb('localhost', 27017, 'osu', 'score')
self.db_user = db_user
self.db_score = db_score

async def update_user(self, osuid, seamaphore):

Expand Down
9 changes: 6 additions & 3 deletions ATRIlib/Mongodb.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from pymongo import MongoClient


class Mongodb:
def __init__(self, host, port, db_name, collection_name):
def __init__(self, client, db_name, collection_name):

self.client = MongoClient(host, port)
self.client = client
self.db = self.client[db_name]
self.collection = self.db[collection_name]

# self.collection.create_index('userid', unique=True)
@staticmethod
def get_client(url):
print("mongo url is " + url)
return MongoClient(url)

def insert(self, data):
self.collection.insert_one(data)
Expand Down
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ services:
bot:
container_name: ATRI_SERVER
environment:
- MONGO_URL=mongodb://database:27017
- OSU_CLIENT_ID=
- OSU_CLIENT_SECRET=
build:
context: .
args:
- MONGO_URL==mongodb://database
ports:
- "9999:8008"
depends_on:
Expand Down