Commit 0b095322 authored by Lee Lunn's avatar Lee Lunn Committed by GitHub

[FIX] Lookup raises in threaded environment. See note. (#688)

In threaded applications, if the user does not explictly create a new 
event loop then one does not exist.
parent f311a949
...@@ -18,7 +18,7 @@ class Twint: ...@@ -18,7 +18,7 @@ class Twint:
self.init = self.get_resume(config.Resume) self.init = self.get_resume(config.Resume)
else: else:
self.init = '-1' self.init = '-1'
self.feed = [-1] self.feed = [-1]
self.count = 0 self.count = 0
self.user_agent = "" self.user_agent = ""
...@@ -49,7 +49,7 @@ class Twint: ...@@ -49,7 +49,7 @@ class Twint:
response = await get.RequestUrl(self.config, self.init, headers=[("User-Agent", self.user_agent)]) response = await get.RequestUrl(self.config, self.init, headers=[("User-Agent", self.user_agent)])
if self.config.Debug: if self.config.Debug:
print(response, file=open("twint-last-request.log", "w", encoding="utf-8")) print(response, file=open("twint-last-request.log", "w", encoding="utf-8"))
self.feed = [] self.feed = []
try: try:
if self.config.Favorites: if self.config.Favorites:
...@@ -258,13 +258,37 @@ def Following(config): ...@@ -258,13 +258,37 @@ def Following(config):
def Lookup(config): def Lookup(config):
logme.debug(__name__+':Lookup') logme.debug(__name__+':Lookup')
if config.User_id is not None:
try:
get_event_loop()
except RuntimeError as e:
if "no current event loop" in str(e):
set_event_loop(new_event_loop())
else:
logme.exception(__name__+':Lookup:Unexpected exception while handling an expected RuntimeError.')
raise
except Exception as e:
logme.exception(__name__+':Lookup:Unexpected exception occured while attempting to get or create a new event loop.')
raise
try:
if config.User_id is not None:
logme.debug(__name__+':Twint:Lookup:user_id') logme.debug(__name__+':Twint:Lookup:user_id')
config.Username = get_event_loop().run_until_complete(get.Username(config.User_id)) config.Username = get_event_loop().run_until_complete(get.Username(config.User_id))
url = f"https://twitter.com/{config.Username}?lang=en"
get_event_loop().run_until_complete(get.User(url, config, db.Conn(config.Database))) url = f"https://twitter.com/{config.Username}?lang=en"
if config.Pandas_au: get_event_loop().run_until_complete(get.User(url, config, db.Conn(config.Database)))
storage.panda._autoget("user")
if config.Pandas_au:
storage.panda._autoget("user")
except RuntimeError as e:
if "no current event loop" in str(e):
logme.exception(__name__+':Lookup:Previous attempt to to create an event loop failed.')
raise
except Exception as e:
logme.exception(__name__+':Lookup:Unexpected exception occured.')
raise
def Profile(config): def Profile(config):
logme.debug(__name__+':Profile') logme.debug(__name__+':Profile')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment