mirror of
https://github.com/MCMi460/3DS-RPC.git
synced 2025-06-19 05:55:36 -04:00
Add rate-limiting
This commit is contained in:
parent
893a581d96
commit
f73e3ae9f1
20
client.py
20
client.py
@ -60,7 +60,7 @@ class Client():
|
|||||||
# Connect to Discord
|
# Connect to Discord
|
||||||
self.connect()
|
self.connect()
|
||||||
# Discord-related variables
|
# Discord-related variables
|
||||||
self.currentGame = None
|
self.currentGame = {'@id': None}
|
||||||
|
|
||||||
# Get from API
|
# Get from API
|
||||||
def APIget(self, route:str, content:dict = {}):
|
def APIget(self, route:str, content:dict = {}):
|
||||||
@ -76,12 +76,20 @@ class Client():
|
|||||||
self.rpc.connect()
|
self.rpc.connect()
|
||||||
|
|
||||||
def signUp(self):
|
def signUp(self):
|
||||||
r = self.APIpost('user/c/%s' % self.friendCode).json()
|
r = self.APIpost('user/c/%s' % self.friendCode)
|
||||||
|
try:
|
||||||
|
r = r.json()
|
||||||
|
except:
|
||||||
|
raise APIException(r.content)
|
||||||
if r['Exception']:
|
if r['Exception']:
|
||||||
raise APIException(r['Exception'])
|
raise APIException(r['Exception'])
|
||||||
|
|
||||||
def fetch(self):
|
def fetch(self):
|
||||||
r = self.APIget('user/%s' % self.friendCode).json()
|
r = self.APIget('user/%s' % self.friendCode)
|
||||||
|
try:
|
||||||
|
r = r.json()
|
||||||
|
except:
|
||||||
|
raise APIException(r.content)
|
||||||
if r['Exception']:
|
if r['Exception']:
|
||||||
raise APIException(r['Exception'])
|
raise APIException(r['Exception'])
|
||||||
return r
|
return r
|
||||||
@ -109,9 +117,12 @@ class Client():
|
|||||||
if not game:
|
if not game:
|
||||||
raise GameMatchError('unknown game: %s' % uid)
|
raise GameMatchError('unknown game: %s' % uid)
|
||||||
|
|
||||||
|
print('Update', end = '')
|
||||||
if self.currentGame != game:
|
if self.currentGame != game:
|
||||||
|
print(' [%s -> %s]' % (self.currentGame['@id'], game['@id']), end = '')
|
||||||
self.currentGame = game
|
self.currentGame = game
|
||||||
self.start = int(time.time())
|
self.start = int(time.time())
|
||||||
|
print()
|
||||||
self.rpc.update(
|
self.rpc.update(
|
||||||
details = game['name'],
|
details = game['name'],
|
||||||
large_image = game['icon_url'].replace('https://kanzashi-ctr.cdn.nintendo.net/i/', host + '/cdn/i/'),
|
large_image = game['icon_url'].replace('https://kanzashi-ctr.cdn.nintendo.net/i/', host + '/cdn/i/'),
|
||||||
@ -122,7 +133,8 @@ class Client():
|
|||||||
# But that's dumb so no
|
# But that's dumb so no
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self.currentGame = None
|
print('Clear [%s -> %s]' % (self.currentGame['@id'], None))
|
||||||
|
self.currentGame = {'@id': None}
|
||||||
self.rpc.clear()
|
self.rpc.clear()
|
||||||
time.sleep(30)
|
time.sleep(30)
|
||||||
|
|
||||||
|
@ -4,3 +4,4 @@ nintendoclients>0.0.3
|
|||||||
xmltodict
|
xmltodict
|
||||||
requests
|
requests
|
||||||
pypresence
|
pypresence
|
||||||
|
flask-limiter
|
||||||
|
10
server.py
10
server.py
@ -1,8 +1,15 @@
|
|||||||
from flask import Flask, make_response
|
from flask import Flask, make_response
|
||||||
|
from flask_limiter import Limiter
|
||||||
|
from flask_limiter.util import get_remote_address
|
||||||
import sqlite3, requests
|
import sqlite3, requests
|
||||||
from api import *
|
from api import *
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
limiter = Limiter(app, key_func = get_remote_address)
|
||||||
|
|
||||||
|
@app.errorhandler(429)
|
||||||
|
def ratelimit_handler(e):
|
||||||
|
return 'You have exceeded your rate-limit'
|
||||||
|
|
||||||
def accessDB():
|
def accessDB():
|
||||||
con = sqlite3.connect('sqlite/fcLibrary.db')
|
con = sqlite3.connect('sqlite/fcLibrary.db')
|
||||||
@ -11,6 +18,7 @@ def accessDB():
|
|||||||
|
|
||||||
# Grab presence from friendCode
|
# Grab presence from friendCode
|
||||||
@app.route('/user/<int:friendCode>/', methods=['GET'])
|
@app.route('/user/<int:friendCode>/', methods=['GET'])
|
||||||
|
@limiter.limit('3/minute')
|
||||||
def userPresence(friendCode:int):
|
def userPresence(friendCode:int):
|
||||||
con, cursor = accessDB()
|
con, cursor = accessDB()
|
||||||
try:
|
try:
|
||||||
@ -44,6 +52,7 @@ def userPresence(friendCode:int):
|
|||||||
|
|
||||||
# Create entry in database with friendCode
|
# Create entry in database with friendCode
|
||||||
@app.route('/user/c/<int:friendCode>/', methods=['POST'])
|
@app.route('/user/c/<int:friendCode>/', methods=['POST'])
|
||||||
|
@limiter.limit('3/minute')
|
||||||
def createUser(friendCode:int):
|
def createUser(friendCode:int):
|
||||||
con, cursor = accessDB()
|
con, cursor = accessDB()
|
||||||
try:
|
try:
|
||||||
@ -65,6 +74,7 @@ def createUser(friendCode:int):
|
|||||||
|
|
||||||
# Make Nintendo's cert a 'secure' cert
|
# Make Nintendo's cert a 'secure' cert
|
||||||
@app.route('/cdn/i/<string:file>/', methods=['GET'])
|
@app.route('/cdn/i/<string:file>/', methods=['GET'])
|
||||||
|
@limiter.limit('1/minute')
|
||||||
def cdnImage(file:str):
|
def cdnImage(file:str):
|
||||||
response = make_response(requests.get('https://kanzashi-ctr.cdn.nintendo.net/i/%s' % file, verify = False).content)
|
response = make_response(requests.get('https://kanzashi-ctr.cdn.nintendo.net/i/%s' % file, verify = False).content)
|
||||||
response.headers['Content-Type'] = 'image/jpeg'
|
response.headers['Content-Type'] = 'image/jpeg'
|
||||||
|
Loading…
Reference in New Issue
Block a user