2016-02-13 17:06:36 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-02-22 08:49:51 -06:00
|
|
|
""" Some utilities. I no have idea how I should put these, so..."""
|
2016-02-18 17:16:43 -06:00
|
|
|
import os
|
2016-02-15 16:49:09 -06:00
|
|
|
import requests
|
2016-02-14 19:24:45 -06:00
|
|
|
import re
|
2016-05-14 20:47:10 -05:00
|
|
|
import logging
|
2018-12-06 17:34:48 -06:00
|
|
|
from sessionmanager import renderers
|
2016-05-14 20:47:10 -05:00
|
|
|
log = logging.getLogger("utils")
|
2016-02-14 19:24:45 -06:00
|
|
|
url_re = re.compile("(?:\w+://|www\.)[^ ,.?!#%=+][^ ]*")
|
|
|
|
bad_chars = '\'\\.,[](){}:;"'
|
2016-02-13 17:06:36 -06:00
|
|
|
|
|
|
|
def seconds_to_string(seconds, precision=0):
|
|
|
|
day = seconds // 86400
|
|
|
|
hour = seconds // 3600
|
|
|
|
min = (seconds // 60) % 60
|
|
|
|
sec = seconds - (hour * 3600) - (min * 60)
|
|
|
|
sec_spec = "." + str(precision) + "f"
|
|
|
|
sec_string = sec.__format__(sec_spec)
|
|
|
|
string = ""
|
|
|
|
if day == 1:
|
|
|
|
string += _(u"%d day, ") % day
|
|
|
|
elif day >= 2:
|
|
|
|
string += _(u"%d days, ") % day
|
|
|
|
if (hour == 1):
|
|
|
|
string += _(u"%d hour, ") % hour
|
|
|
|
elif (hour >= 2):
|
|
|
|
string += _("%d hours, ") % hour
|
|
|
|
if (min == 1):
|
|
|
|
string += _(u"%d minute, ") % min
|
|
|
|
elif (min >= 2):
|
|
|
|
string += _(u"%d minutes, ") % min
|
|
|
|
if sec >= 0 and sec <= 2:
|
|
|
|
string += _(u"%s second") % sec_string
|
|
|
|
else:
|
|
|
|
string += _(u"%s seconds") % sec_string
|
2016-02-14 19:24:45 -06:00
|
|
|
return string
|
|
|
|
|
|
|
|
def find_urls_in_text(text):
|
2016-02-15 16:49:09 -06:00
|
|
|
return [s.strip(bad_chars) for s in url_re.findall(text)]
|
|
|
|
|
|
|
|
def download_file(url, local_filename, window):
|
|
|
|
r = requests.get(url, stream=True)
|
|
|
|
window.change_status(_(u"Downloading {0}").format(local_filename,))
|
|
|
|
total_length = r.headers.get("content-length")
|
|
|
|
dl = 0
|
|
|
|
total_length = int(total_length)
|
|
|
|
with open(local_filename, 'wb') as f:
|
|
|
|
for chunk in r.iter_content(chunk_size=64):
|
|
|
|
if chunk: # filter out keep-alive new chunks
|
|
|
|
dl += len(chunk)
|
|
|
|
f.write(chunk)
|
|
|
|
done = int(100 * dl / total_length)
|
2016-02-18 17:16:43 -06:00
|
|
|
msg = _(u"Downloading {0} ({1}%)").format(os.path.basename(local_filename), done)
|
2016-02-15 16:49:09 -06:00
|
|
|
window.change_status(msg)
|
|
|
|
window.change_status(_(u"Ready"))
|
2016-02-22 08:49:51 -06:00
|
|
|
return local_filename
|
|
|
|
|
|
|
|
def clean_text(text):
|
|
|
|
""" Replaces all HTML entities and put the plain text equivalent if it's possible."""
|
|
|
|
text = text.replace("<br>", "\n")
|
2016-05-14 20:47:10 -05:00
|
|
|
text = text.replace("\\n", "\n")
|
2017-03-13 02:16:34 -06:00
|
|
|
return text
|
|
|
|
|
|
|
|
def add_attachment(attachment):
|
|
|
|
msg = u""
|
|
|
|
tpe = ""
|
|
|
|
if attachment["type"] == "link":
|
|
|
|
msg = u"{0}: {1}".format(attachment["link"]["title"], attachment["link"]["url"])
|
|
|
|
tpe = _(u"Link")
|
|
|
|
elif attachment["type"] == "photo":
|
|
|
|
tpe = _(u"Photo")
|
|
|
|
msg = attachment["photo"]["text"]
|
|
|
|
if msg == "":
|
|
|
|
msg = _(u"no description available")
|
|
|
|
elif attachment["type"] == "video":
|
|
|
|
msg = u"{0}".format(attachment["video"]["title"],)
|
|
|
|
tpe = _(u"Video")
|
|
|
|
elif attachment["type"] == "audio":
|
2018-12-06 17:56:22 -06:00
|
|
|
msg = u"{0}".format(" ".join(renderers.render_audio(attachment["audio"])))
|
2017-03-13 02:16:34 -06:00
|
|
|
tpe = _(u"Audio")
|
|
|
|
elif attachment["type"] == "doc":
|
2018-12-13 11:48:17 -06:00
|
|
|
msg = u"{0}".format(attachment["doc"]["title"])
|
|
|
|
tpe = _(u"{0} file").format(attachment["doc"]["ext"])
|
|
|
|
elif attachment["type"] == "audio_message":
|
|
|
|
msg = u"{0}".format(" ".join(renderers.render_audio_message(attachment["audio_message"])))
|
|
|
|
tpe = _(u"Voice message")
|
|
|
|
else:
|
|
|
|
print attachment
|
2017-03-13 02:16:34 -06:00
|
|
|
return [tpe, msg]
|
|
|
|
|