# Path: lib/Utility.py
# RE_COMPILED_TYPE = type(re.compile(''))
# POE_NINJA_API = 'http://api.poe.ninja/api/Data/'
#     CONFIG_FNAME = "cfg\\app.ini"
# class MsgType(IntEnum):
# class Messenger:
# class AppException(Exception):
# class CompileException(AppException):
# class AppConfiguration:
# class NoIndent(object):
# class NoIndentEncoder(json.JSONEncoder):
# class ConfidenceLevel(IntEnum):
#     def __init__(self):
#     def send_tmsg(self, msg, log_level=logging.NOTSET, tag=None):
#     def send_msg(self, msg, log_level=logging.NOTSET, timed=False, tag=None):
#     def send_stopped(self):
#     def send_update_id(self, id):
#     def send_object(self, obj):
#     def __init__(self):
#     def load(self):
#     def load_default(self):
#     def _load_settings(self, settings=None):
#     def update(self, cfg):
#     def save(self):
# def getBytesFromURL(url, handle=None, max_attempts=1, connect_timeout=None, timeout=None, gzip=True):
# def getJsonFromURL(url, handle=None, max_attempts=1, gzip=True):
# def getDataFromUrl(url, callback, max_attempts=1):
# def str2bool(val):
# def round_up(num):
# def round_down(num):
# def dround(num, ndigits=2):
# def tmsg(msg):
# def getBaseUrl(url):
# def isAbsoluteUrl(url):
# def logexception():
# def get_verror_msg(verror, data=None):
#     def __init__(self, value):
#     def __init__(self, *args, **kwargs):
#     def default(self, o):
#     def encode(self, o):
# def utc_to_local(utc_dt):
# def namify(text):
# def normalize_id(text):


import functools
import json
import pycurl
import re
import traceback
import time
import logging
from collections import deque
from io import BytesIO
from queue import Queue, Empty, Full
from threading import Thread, Lock, Event
from lib.Utility import msgr, logexception, config

                # Check for curl objects which have terminated, and add them to the freelist
                while not self.evt_stop.is_set():
                    num_q, ok_list, err_list = self.m.info_read()
                    for c in ok_list:
                        self.m.remove_handle(c)
                        cinfo = info(c)
                        # print("Success: {} - {}: total: {:.2f}s, speed: {} KB/s, size: {} KB, "
                        #       "start-transfer: {:.2f}s, pre-transfer: {:.2f}s"
                        #       .format(c.req.req_id, c.getinfo(pycurl.HTTP_CODE), cinfo['total-time'],
                        #               round(cinfo['speed-download']/1024), round(cinfo['size-download']/1024),
                        #               cinfo['starttransfer-time'], cinfo['pretransfer-time']))

                        if cinfo['http-code'] == 200:
                            if not c.req.submitted_next:
                                next_id = c.req.peek_id()
                                if next_id:
                                    msgr.send_msg('Full peek was required for ID: {}'.format(c.req.req_id), logging.INFO)
                                    self.add_request(next_id)
                                    c.req.submitted_next = next_id
                                    c.req.peek_time = time.time() - c.req.start_time

                            if c.req.submitted_next:
                                c.req.finished = True
                            else:
                                msgr.send_msg('Request successful for ID {}, but next ID was not found. Redownloading..'
                                              .format(c.req.req_id), logging.INFO)
                        else:
                            try:
                                err_data = json.loads(c.req.buffer.getvalue().decode())['error']

# Path: lib/Utility.py
# RE_COMPILED_TYPE = type(re.compile(''))
# POE_NINJA_API = 'http://api.poe.ninja/api/Data/'
#     CONFIG_FNAME = "cfg\\app.ini"
# class MsgType(IntEnum):
# class Messenger:
# class AppException(Exception):
# class CompileException(AppException):
# class AppConfiguration:
# class NoIndent(object):
# class NoIndentEncoder(json.JSONEncoder):
# class ConfidenceLevel(IntEnum):
#     def __init__(self):
#     def send_tmsg(self, msg, log_level=logging.NOTSET, tag=None):
#     def send_msg(self, msg, log_level=logging.NOTSET, timed=False, tag=None):
#     def send_stopped(self):
#     def send_update_id(self, id):
#     def send_object(self, obj):
#     def __init__(self):
#     def load(self):
#     def load_default(self):
#     def _load_settings(self, settings=None):
#     def update(self, cfg):
#     def save(self):
# def getBytesFromURL(url, handle=None, max_attempts=1, connect_timeout=None, timeout=None, gzip=True):
# def getJsonFromURL(url, handle=None, max_attempts=1, gzip=True):
# def getDataFromUrl(url, callback, max_attempts=1):
# def str2bool(val):
# def round_up(num):
# def round_down(num):
# def dround(num, ndigits=2):
# def tmsg(msg):
# def getBaseUrl(url):
# def isAbsoluteUrl(url):
# def logexception():
# def get_verror_msg(verror, data=None):
#     def __init__(self, value):
#     def __init__(self, *args, **kwargs):
#     def default(self, o):
#     def encode(self, o):
# def utc_to_local(utc_dt):
# def namify(text):
# def normalize_id(text):


# Path: lib/Downloader.py
import functools
import json
import pycurl
import re
import traceback
import time
import logging
from collections import deque
from io import BytesIO
from queue import Queue, Empty, Full
from threading import Thread, Lock, Event
from lib.Utility import msgr, logexception, config


                # Check for curl objects which have terminated, and add them to the freelist
                while not self.evt_stop.is_set():
                    num_q, ok_list, err_list = self.m.info_read()
                    for c in ok_list:
                        self.m.remove_handle(c)
                        cinfo = info(c)
                        # print("Success: {} - {}: total: {:.2f}s, speed: {} KB/s, size: {} KB, "
                        #       "start-transfer: {:.2f}s, pre-transfer: {:.2f}s"
                        #       .format(c.req.req_id, c.getinfo(pycurl.HTTP_CODE), cinfo['total-time'],
                        #               round(cinfo['speed-download']/1024), round(cinfo['size-download']/1024),
                        #               cinfo['starttransfer-time'], cinfo['pretransfer-time']))

                        if cinfo['http-code'] == 200:
                            if not c.req.submitted_next:
                                next_id = c.req.peek_id()
                                if next_id:
                                    msgr.send_msg('Full peek was required for ID: {}'.format(c.req.req_id), logging.INFO)
                                    self.add_request(next_id)
                                    c.req.submitted_next = next_id
                                    c.req.peek_time = time.time() - c.req.start_time

                            if c.req.submitted_next:
                                c.req.finished = True
                            else:
                                msgr.send_msg('Request successful for ID {}, but next ID was not found. Redownloading..'
                                              .format(c.req.req_id), logging.INFO)
                        else:
                            try:
                                err_data = json.loads(c.req.buffer.getvalue().decode())['error']

                                msgr.send_msg('Request for ID {} failed. {} - Code: {}, Message: {}'