from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.executors.pool import ProcessPoolExecutor
from db_manager import DatabaseManager
import multiprocessing as mp
import Queue as queue
from time import sleep
import time
import datetime
import forexconnect as fx
import settings as s
from event import LiveDataEvent
import re
import logging
from logging import DEBUG

class LiveDataMiner(object):
    """
    Creates time based event for collecting FXCM data.
    
    Information on this API
    https://apscheduler.readthedocs.io/en/latest/
    https://github.com/agronholm/apscheduler/
    """
    def __init__(self, live_queue):
        """
        """
        self.live_queue = live_queue

    def _fxc_login(self):
        """
        Logs into ForexConnectClient and returns a session. 
        """
        while 1:
            try:
                fxc = fx.ForexConnectClient(s.FX_USER, s.FX_PASS,
                                            s.FX_ENVR, s.URL
                )
                if fxc.is_connected() == True:
                    break
            except RuntimeError:
                sleep(0.1)

        return fxc

    def _get_live(self, event, fxc, live_offers):
        """
        """
        time_frame = event.time_frame
        if live_offers != []:
            for offer in live_offers:
                db_date = DatabaseManager().return_date(offer, time_frame) 
                fm_date = db_date + datetime.timedelta(minutes = 1)
                tdn = datetime.datetime.now()
                to_date = tdn.replace(second=00, microsecond=00)
                data = fxc.get_historical_prices(str(offer), fm_date,
                       to_date, str(time_frame)
                )
                data = [d.__getstate__()[0] for d in data]
                data = [x for x in data if db_date not in x.values()]
                if data != []:
                    self.live_queue.put(LiveDataEvent(
                    data, offer, time_frame))

                del data

    def _live_data_session(self, fxc):
        """
        """
        live_offers = []
        while True:
            try:
                event = self.live_queue.get(False)
            except queue.Empty:
                sleep(0.1)            
            else:
                if event.type == 'LIVEDATA':
                    mp.Process(target=DatabaseManager(
                    ).write_data, args=(event,)).start()
                elif event.type == 'GETLIVE':
                    mp.Process(self._get_live(event, fxc, live_offers)).start()
                elif event.type == 'LIVEREADY':
                    if event.offer not in live_offers:
                        print("[oo] Live Started %s" % event.offer)
                        live_offers.append(event.offer)
                    
    def live_mine(self):
        self._live_data_session(self._fxc_login())

@@ -1,129 +1,4 @@
-from apscheduler.schedulers.background import BackgroundScheduler%0Afrom apscheduler.executors.pool import ProcessPoolExecutor%0A
 from
@@ -265,16 +265,17 @@
  DEBUG%0A%0A
+%0A
 class Li
@@ -1063,29 +1063,24 @@
 self, event,
- fxc,
  live_offers
@@ -1118,110 +1118,45 @@
     
-time_frame = event.time_frame%0A        if live_offers != %5B%5D:%0A            for offer in live_offers:%0A    
+def getdata(offer, time_frame, fxc):%0A
     
@@ -1235,20 +1235,16 @@
         
-    
 fm_date 
@@ -1299,20 +1299,16 @@
         
-  
-  
 tdn = da
@@ -1329,20 +1329,16 @@
 e.now()%0A
-    
         
@@ -1398,28 +1398,24 @@
             
-    
 data = fxc.g
@@ -1471,28 +1471,24 @@
             
-  
-  
 to_date, str
@@ -1508,34 +1508,26 @@
             
-    )%0A    
+)%0A
             
@@ -1573,36 +1573,32 @@
 ta%5D%0A            
-  
-  
 data = %5Bx for x 
@@ -1635,16 +1635,655 @@
 lues()%5D%0A
+            return data%0A%0A        fxc = self._fxc_login()%0A        missed_offers = %5B%5D%0A        time_frame = event.time_frame%0A        if live_offers != %5B%5D:%0A            for offer in live_offers:                %0A                data = getdata(offer, time_frame, fxc)%0A                if data != %5B%5D:%0A                    self.live_queue.put(LiveDataEvent(%0A                    data, offer, time_frame))%0A                else:%0A                    missed_offers.append(offer)%0A%0A                del data%0A%0A        if missed_offers != %5B%5D:%0A            sleep(30)%0A            for offer in missed_offers:%0A                data = getdata(offer, time_frame, fxc)%0A
         
@@ -2932,16 +2932,23 @@
 Process(
+target=
 self._ge
@@ -2957,20 +2957,22 @@
 live
+, args=
 (event,
- fxc,
  liv
@@ -2979,16 +2979,17 @@
 e_offers
+,
 )).start
@@ -3218,36 +3218,16 @@
 .offer)%0A
-                    
 %0A    def

Update live.py