From dedc66bb9de4b4fb26b77c06d771bba253ac1e5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Oudin?= Date: Mon, 26 Feb 2018 17:12:19 +0100 Subject: [PATCH] Bug fix --- histories/models.py | 18 +++++++++++------- histories/tor_runner.py | 9 ++++++--- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/histories/models.py b/histories/models.py index 1df5d89..c45a626 100644 --- a/histories/models.py +++ b/histories/models.py @@ -5,6 +5,7 @@ interests, keywords... from collections import namedtuple import random +import asyncio from math import floor from xml.etree import ElementTree as ET from datetime import datetime @@ -107,15 +108,18 @@ class History(models.Model): output += str(entry) + '\n' return output - async def play_histories(self): + async def _handler(self): + runner = await TorInstance.create(self.return_history(), self.user.browser_fingerprint.serialize_headers()) + await runner.run() + self.played = True + self.save() + + def play_histories(self): """ Actually plays the history. """ - self.played = True - runner = await TorInstance.create( - self.return_history(), - self.user.browser_fingerprint.serialize_headers()) - runner.run() - self.save() + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + loop.run_until_complete(asyncio.wait([self._handler()])) def to_xml(self, xml_root=None): ''' Exports the current history to xml ''' diff --git a/histories/tor_runner.py b/histories/tor_runner.py index 5b9b878..08db74f 100644 --- a/histories/tor_runner.py +++ b/histories/tor_runner.py @@ -58,7 +58,9 @@ class TorInstance(): async def run(self): """ Runs the Tor Instance on the history. """ - while (self.history[0][1] - dt.datetime.now()).total_seconds >= 10: + while (self.history) and (dt.datetime.combine(self.history[0][1], + dt.datetime.min.time()) - + dt.datetime.now()).total_seconds() >= 10: print("Sleeping") sleep(10) while self.history: @@ -66,8 +68,9 @@ class TorInstance(): async with async_timeout.timeout(30): await(self.query(item[0])) now = dt.datetime.now() - if now <= self.history[0][1]: - sleep((self.history[0][1] - now).total_seconds()) + print(self.history[0]) + if now <= dt.datetime.combine(self.history[0][1], dt.datetime.min.time()): + sleep((dt.datetime.combine(self.history[0][1], dt.datetime.min.time()) - now).total_seconds()) def create_session(self):