# Path: constants/exceptions.py
# class invalidArgumentsException(Exception):
# class loginFailedException(Exception):
# class userBannedException(Exception):
# class userLockedException(Exception):
# class noBanchoSessionException(Exception):
# class osuApiFailException(Exception):
# class fileNotFoundException(Exception):
# class invalidBeatmapException(Exception):
# class unsupportedGameModeException(Exception):
# class beatmapTooLongException(Exception):
# class need2FAException(Exception):
# class noAPIDataError(Exception):
# class scoreNotFoundError(Exception):
# class ppCalcException(Exception):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, handler):
# 	def __init__(self, handler, f):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, exception):
# 
# Path: helpers/osuapiHelper.py
# def osuApiRequest(request, params, getFirst=True):
# def getOsuFileFromName(fileName):
# def getOsuFileFromID(beatmapID):
# 		URL = "{}/web/maps/{}".format(glob.conf["OSU_API_URL"], quote(fileName))
# 		URL = "{}/osu/{}".format(glob.conf["OSU_API_URL"], beatmapID)


import tornado.gen
import tornado.web
from common.log import logUtils as log
from common.web import requestsManager
from constants import exceptions
from helpers import osuapiHelper
from common.sentry import sentry


class handler(requestsManager.asyncRequestHandler):
	MODULE_NAME = "osu_download"

	@tornado.web.asynchronous
	@tornado.gen.engine
	@sentry.captureTornado
	def asyncGet(self, fileName = None):
		try:
			# Check arguments
			if fileName is None:
				raise exceptions.invalidArgumentsException(self.MODULE_NAME)
			if fileName == "":
				raise exceptions.invalidArgumentsException(self.MODULE_NAME)

			fileNameShort = fileName[:32]+"..." if len(fileName) > 32 else fileName[:-4]
			log.info("Requested .osu file {}".format(fileNameShort))

			# Get .osu file from osu! server

# Path: constants/exceptions.py
# class invalidArgumentsException(Exception):
# class loginFailedException(Exception):
# class userBannedException(Exception):
# class userLockedException(Exception):
# class noBanchoSessionException(Exception):
# class osuApiFailException(Exception):
# class fileNotFoundException(Exception):
# class invalidBeatmapException(Exception):
# class unsupportedGameModeException(Exception):
# class beatmapTooLongException(Exception):
# class need2FAException(Exception):
# class noAPIDataError(Exception):
# class scoreNotFoundError(Exception):
# class ppCalcException(Exception):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, handler):
# 	def __init__(self, handler, f):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, exception):
# 
# Path: helpers/osuapiHelper.py
# def osuApiRequest(request, params, getFirst=True):
# def getOsuFileFromName(fileName):
# def getOsuFileFromID(beatmapID):
# 		URL = "{}/web/maps/{}".format(glob.conf["OSU_API_URL"], quote(fileName))
# 		URL = "{}/osu/{}".format(glob.conf["OSU_API_URL"], beatmapID)


# Path: handlers/mapsHandler.py
import tornado.gen
import tornado.web
from common.log import logUtils as log
from common.web import requestsManager
from constants import exceptions
from helpers import osuapiHelper
from common.sentry import sentry



class handler(requestsManager.asyncRequestHandler):
	MODULE_NAME = "osu_download"

	@tornado.web.asynchronous
	@tornado.gen.engine
	@sentry.captureTornado
	def asyncGet(self, fileName = None):
		try:
			# Check arguments
			if fileName is None:
				raise exceptions.invalidArgumentsException(self.MODULE_NAME)
			if fileName == "":
				raise exceptions.invalidArgumentsException(self.MODULE_NAME)

			fileNameShort = fileName[:32]+"..." if len(fileName) > 32 else fileName[:-4]
			log.info("Requested .osu file {}".format(fileNameShort))

			# Get .osu file from osu! server

			fileContent = osuapiHelper.getOsuFileFromName(fileName)