def init_logger(self, level): console = logging.StreamHandler(sys.stdout) frm = logging.Formatter("%(asctime)s %(levelname)-8s %(message)s", "%d.%m.%Y %H:%M:%S") console.setFormatter(frm) self.log = logging.getLogger("log") # settable in config if self.config['log']['file_log']: if self.config['log']['log_rotate']: file_handler = logging.handlers.RotatingFileHandler( join(smart_text(self.config['log']['log_folder']), u'log.txt'), maxBytes=self.config['log']['log_size'] * 1024, backupCount=int(self.config['log']['log_count']), encoding="utf8", ) else: file_handler = logging.FileHandler( join(smart_text(self.config['log']['log_folder']), u'log.txt'), encoding="utf8", ) file_handler.setFormatter(frm) self.log.addHandler(file_handler) self.log.addHandler(console) #if console logging self.log.setLevel(level)
def check_file(self, check_name, description, is_folder=False): """Check whether needed files exist.""" file_created = True file_exists = True check_name = smart_text(check_name) if not exists(check_name): file_exists = False try: if is_folder: check_name = check_name.replace(u'/', smart_text(sep)) os.makedirs(check_name) else: open(check_name, "w") except Exception: file_created = False if not file_exists and not file_created: print( _("could not create %(desc)s: %(name)s") % { "desc": description, "name": check_name })
def add_package(): name = request.forms.get("add_name", "New Package").strip() queue = int(request.forms['add_dest']) links = decode(request.forms['add_links']) links = links.split("\n") pw = request.forms.get("add_password", "").strip("\n\r") try: f = request.files['add_file'] if not name or name == "New Package": name = f.name fpath = join(PYLOAD.getConfigValue("general", "download_folder"), "tmp_" + f.filename) destination = open(fpath, 'wb') copyfileobj(f.file, destination) destination.close() links.insert(0, fpath) except: pass name = smart_text(name, errors='ignore') links = list(filter(None, map(lambda link: link.strip(), links))) pack = PYLOAD.addPackage(name, links, queue) if pw: pw = smart_text(pw, errors='ignore') data = {"password": pw} PYLOAD.setPackageData(pack, data)
def xdcc_request_resume(self, bot, dcc_port, file_name, resume_position): if self.xdcc_request_time: bot_host = self.get_bot_host(bot) self.plugin.log_info( _("Requesting XDCC resume of '%s' at position %s") % (file_name, resume_position)) self.irc_sock.send( "PRIVMSG %s :\x01DCC RESUME \"%s\" %s %s\x01\r\n" % (bot, encode(file_name, 'utf-8'), dcc_port, resume_position)) start_time = time.time() while time.time() - start_time < 30: origin, command, args = self.get_irc_command() # Private message from bot to us? if origin and command and args \ and '@' in origin \ and (origin[0:len(bot)] == bot or bot_host and origin.split('@')[1] == bot_host) \ and args[0][0:len(self.nick)] == self.nick \ and command in ("PRIVMSG", "NOTICE"): try: text = smart_text(args[1]) except UnicodeDecodeError: text = smart_text(args[1], encoding='latin1', errors='replace') sender_nick = origin.split('@')[0].split('!')[0] self.plugin.log_debug( _("PrivMsg: <%s> %s") % (sender_nick, text)) m = re.match( r'\x01DCC ACCEPT .*? %s (?P<RESUME_POS>\d+)\x01' % dcc_port, text) if m: self.plugin.log_debug( _("Bot '%s' acknowledged resume at position %s") % (sender_nick, m.group('RESUME_POS'))) return int(m.group('RESUME_POS')) else: time.sleep(0.1) self.plugin.log_warning( _("Timeout while waiting for resume acknowledge, not resuming") ) else: self.plugin.log_error(_("No XDCC request pending, cannot resume")) return 0
def xdcc_get_pack_info(self, bot, pack): bot_host = self.get_bot_host(bot) self.plugin.log_info(_("Requesting pack #%s info") % pack) self.irc_sock.send("PRIVMSG %s :xdcc info #%s\r\n" % (bot, pack)) info = {} start_time = time.time() while time.time() - start_time < 90: origin, command, args = self.get_irc_command() # Private message from bot to us? if origin and command and args \ and (origin[0:len(bot)] == bot or bot_host and origin.split('@')[1] == bot_host) \ and args[0][0:len(self.nick)] == self.nick \ and command in ("PRIVMSG", "NOTICE"): try: text = smart_text(args[1]) except UnicodeDecodeError: text = smart_text(args[1], encoding='latin1', errors='replace') pack_info = text.split() if pack_info[0].lower() == "filename": self.plugin.log_debug(_("Filename: '%s'") % pack_info[1]) info.update({'status': "online", 'name': pack_info[1]}) elif pack_info[0].lower() == "filesize": self.plugin.log_debug(_("Filesize: '%s'") % pack_info[1]) info.update({'status': "online", 'size': pack_info[1]}) else: sender_nick = origin.split('@')[0].split('!')[0] self.plugin.log_debug( _("PrivMsg: <%s> %s") % (sender_nick, text)) else: if len(info) > 2: #: got both name and size break time.sleep(0.1) else: if len(info) == 0: self.plugin.log_error(_("XDCC Bot '%s' did not answer") % bot) return {'status': "offline", 'msg': "XDCC Bot did not answer"} return info
def proccess_irc_command(self, origin, command, args): bot = self.info['pattern']['BOT'] nick = self.config.get('nick') if origin is None\ or command is None\ or args is None: return # Private message from bot to us? bot_host = self.irc_client.get_bot_host(bot) if '@' not in origin \ or (origin[0:len(bot)] != bot and bot_host and origin.split('@')[1] != bot_host) \ or args[0][0:len(nick)] != nick \ or command not in ("PRIVMSG", "NOTICE"): return try: text = smart_text(args[1]) except UnicodeDecodeError: text = smart_text(args[1], encoding='latin1', errors='replace') sender_nick = origin.split('@')[0].split('!')[0] self.log_debug(_("PrivMsg: <%s> %s") % (sender_nick, text)) if text in ("You already requested that pack", "All Slots Full", "You have a DCC pending"): self.request_again = True elif "you must be on a known channel to request a pack" in text: self.log_error(_("Invalid channel")) self.fail(_("Invalid channel")) elif "Invalid Pack Number" in text: self.log_error(_("Invalid Pack Number")) self.fail(_("Invalid Pack Number")) m = re.match( '\x01DCC SEND "?(?P<NAME>.*?)"? (?P<IP>\d+) (?P<PORT>\d+)(?: (?P<SIZE>\d+))?\x01', text) #: XDCC? if m: ip = socket.inet_ntoa(struct.pack('!I', int(m.group('IP')))) self.dcc_port = int(m.group('PORT')) self.dcc_file_name = m.group('NAME') self.dcc_sender_bot = origin.split('@')[0].split('!')[0] file_size = int(m.group('SIZE') or 0) self.do_download(ip, self.dcc_port, self.dcc_file_name, file_size)
def find(cls): """ Check for ffmpeg """ if cls.CMD is not None: return True try: if IS_WINDOWS: ffmpeg = ( os.path.join(pypath, "ffmpeg.exe") if isexecutable(os.path.join(pypath, "ffmpeg.exe")) else "ffmpeg.exe" ) else: ffmpeg = "ffmpeg" cmd = which(ffmpeg) or ffmpeg p = subprocess.Popen([cmd, "-version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = (_r.strip() if _r else "" for _r in p.communicate()) except OSError: return False m = cls._RE_VERSION.search(smart_text(out)) if m is not None: cls.VERSION = m.group(1) cls.CMD = cmd return True
def find(cls): try: if IS_WINDOWS: cls.CMD = os.path.join(pypath, "RAR.exe") else: cls.CMD = "rar" p = subprocess.Popen([cls.CMD], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = (_r.strip() if _r else "" for _r in p.communicate()) # cls.__name__ = "RAR" cls.REPAIR = True except OSError: try: if IS_WINDOWS: cls.CMD = os.path.join(pypath, "UnRAR.exe") else: cls.CMD = "unrar" p = subprocess.Popen([cls.CMD], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = (_r.strip() if _r else "" for _r in p.communicate()) except OSError: return False m = cls._RE_VERSION.search(smart_text(out)) if m is not None: cls.VERSION = m.group(1) return True
def save_config(self, config, filename): """Save configuration `config` to a file with `filename` name.""" with open(filename, 'wb') as f: chmod(filename, 0o600) f.write(smart_bytes('version: {0} \n'.format(CONF_VERSION))) for section in sorted(six.iterkeys(config)): f.write( smart_bytes('\n{0} - "{1}":\n'.format( section, config[section]["desc"]))) for option, data in sorted(config[section].items(), key=lambda _x: _x[0]): if option in ("desc", "outline"): continue if isinstance(data["value"], list): value = "[ \n" for x in data["value"]: value += "\t\t" + str(x) + ",\n" value += "\t\t]\n" else: value = '{val}\n'.format(val=smart_text(data["value"])) f.write( smart_bytes(u'\t{0} {1} : "{2}" = {3}'.format( data["type"], option, data["desc"], value, )))
def send_invite_request(self, bot, chan, password): bot_host = self.get_bot_host(bot) if bot_host: self.plugin.log_info( _("Sending invite request for #%s to '%s'") % (chan, bot)) else: self.plugin.log_warning(_("Cannot send invite request")) return self.irc_sock.send("PRIVMSG %s :enter #%s %s %s\r\n" % (bot, chan, self.nick, password)) start_time = time.time() while time.time() - start_time < 30: origin, command, args = self.get_irc_command() if origin is None \ or command is None \ or args is None: return # Private message from bot to us? if '@' not in origin \ or (origin[0:len(bot)] != bot and origin.split('@')[1] != bot_host) \ or args[0][0:len(self.nick)] != self.nick \ or command not in ("PRIVMSG", "NOTICE", "INVITE"): continue try: text = smart_text(args[1]) except UnicodeDecodeError: text = smart_text(args[1], encoding='latin1', errors='replace') sender_nick = origin.split('@')[0].split('!')[0] if command == "INVITE": self.plugin.log_info(_("Got invite to #%s") % chan) else: self.plugin.log_info( _("PrivMsg: <%s> %s") % (sender_nick, text)) break else: self.plugin.log_warning( _("'%s' did not respond to the request") % bot)
def get(self, section, option): """get value""" val = self.config[section][option]["value"] try: if isinstance(val, six.binary_type): return smart_text(val) except Exception: return val return val
def decrypt_attr(data, key): """ Decrypt an encrypted attribute (usually 'a' or 'at' member of a node) """ data = MegaCrypto.base64_decode(data) k, iv, meta_mac = MegaCrypto.get_cipher_key(key) attr = smart_text(MegaCrypto.cbc_decrypt(data, k)) #: Data is padded, 0-bytes must be stripped return json.loads(re.search( r'{.+?}', attr).group(0)) if attr.startswith('MEGA{"') else False
def nickserv_identify(self, password): self.plugin.log_info(_("Authenticating nickname")) bot = "nickserv" bot_host = self.get_bot_host(bot) if not bot_host: self.plugin.log_warning( _("Server does not seems to support nickserv commands")) return self.irc_sock.send("PRIVMSG %s :identify %s\r\n" % (bot, password)) start_time = time.time() while time.time() - start_time < 30: origin, command, args = self.get_irc_command() if origin is None \ or command is None \ or args is None: return # Private message from bot to us? if '@' not in origin \ or (origin[0:len(bot)] != bot and origin.split('@')[1] != bot_host) \ or args[0][0:len(self.nick)] != self.nick \ or command not in ("PRIVMSG", "NOTICE"): continue try: text = smart_text(args[1]) except UnicodeDecodeError: text = smart_text(args[1], encoding='latin1', errors='replace') sender_nick = origin.split('@')[0].split('!')[0] self.plugin.log_info(_("PrivMsg: <%s> %s") % (sender_nick, text)) break else: self.plugin.log_warning( _("'%s' did not respond to the request") % bot)
def decode(value, encoding=None, errors='strict'): """ Encoded string (default to own system encoding) -> unicode string """ if isinstance(value, six.binary_type): res = smart_text( value, encoding=encoding or get_console_encoding(sys.stdout.encoding), errors=errors, ) elif isinstance(value, six.text_type): res = value else: res = smart_text(value) # Hotfix UnicodeDecodeError try: smart_bytes(res, encoding='ascii') except UnicodeEncodeError: return normalize(res) return res
def test_smart_text(self): value_to_expected_tuples = ( (b'testing', u'testing'), (u'testing', u'testing'), (b'A\xc3\xb1o', u'Año'), (u'Año', u'Año'), (123, u'123'), (None, u'None'), ) for value, expected in value_to_expected_tuples: result = smart_text(value) self.assertEqual(result, expected) self.assertIsInstance(result, six.text_type)
def timedtext_to_srt(timedtext): def _format_srt_time(millisec): sec, milli = divmod(millisec, 1000) m, s = divmod(int(sec), 60) h, m = divmod(m, 60) return "%02d:%02d:%02d,%s" % (h, m, s, milli) srt = "" dom = parse_xml(timedtext) body = dom.getElementsByTagName("body")[0] paras = body.getElementsByTagName("p") for i, para in enumerate(paras): srt += smart_text(i+1) + "\n" srt += _format_srt_time(int(para.attributes['t'].value)) + ' --> ' + \ _format_srt_time(int(para.attributes['t'].value) + int(para.attributes['d'].value)) + "\n" for child in para.childNodes: if child.nodeName == 'br': srt += "\n" elif child.nodeName == '#text': srt += smart_text(child.data) srt += "\n\n" return srt
def loadAccounts(self): """loads all accounts available""" if not exists("accounts.conf"): with open("accounts.conf", "wb") as f: f.write(smart_bytes('version: {0}'.format(ACC_VERSION))) with open("accounts.conf", "rb") as f: content = f.readlines() version = content[0].split(b":")[1].strip() if content else "" if not version or int(version) < ACC_VERSION: copy("accounts.conf", "accounts.backup") with open("accounts.conf", "wb") as f: f.write(smart_bytes('version: {0}'.format(ACC_VERSION))) self.core.log.warning( _("Account settings deleted, due to new config format.")) return plugin = "" name = "" for line in content[1:]: line = smart_text(line.strip()) if (not line or line.startswith('#') or line.startswith('version')): continue if line.endswith(':') and line.count(':') == 1: plugin = line[:-1] self.accounts[plugin] = {} elif line.startswith('@'): options = line[1:].split() if options: self.accounts[plugin][name]['options'][ options[0]] = options[1:] elif ':' in line: name, sep, pw = line.partition(':') self.accounts[plugin][name] = { 'password': pw, 'options': {}, 'valid': True }
def cast(self, typ, value): """cast value to given format""" if not isinstance(value, six.string_types): return value elif typ == "int": return int(value) elif typ == "bool": return value.lower() in {"1", "true", "on", "an", "yes"} elif typ == "time": if not value: value = "0:00" if ":" not in value: value += ":00" return value elif typ in {"str", "file", "folder"}: return smart_text(value) return value
def find(cls): try: if IS_WINDOWS: cls.CMD = os.path.join(pypath, "7z.exe") p = subprocess.Popen([cls.CMD], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = (_r.strip() if _r else "" for _r in p.communicate()) except OSError: return False else: m = cls._RE_VERSION.search(smart_text(out)) if m is not None: cls.VERSION = m.group(1) return True
def fixurl(url, unquote=None): old = url url = url_unquote(url) if unquote is None: unquote = url is old url = decode(url) try: url = smart_text(url, encoding='unicode-escape') except UnicodeDecodeError: pass url = html_unescape(url) url = re.sub(r'(?<!:)/{2,}', '/', url).strip().lstrip('.') if not unquote: url = url_quote(url) return url
if not ServerThread.core: if ServerThread.setup: SETUP = ServerThread.setup config = SETUP.config else: raise Exception("Could not access pyLoad Core") else: PYLOAD = ServerThread.core.api config = ServerThread.core.config from module.common.JsEngine import JsEngine JS = JsEngine() TEMPLATE = smart_text(config.get('webinterface', 'template')) DL_ROOT = smart_text(config.get('general', 'download_folder')) LOG_ROOT = smart_text(config.get('log', 'log_folder')) PREFIX = smart_text(config.get('webinterface', 'prefix')) if PREFIX: PREFIX = PREFIX.rstrip("/") if not PREFIX.startswith("/"): PREFIX = "/" + PREFIX DEBUG = config.get("general", "debug_mode") or "-d" in sys.argv or "--debug" in sys.argv bottle.debug(DEBUG) cache = join("tmp", "jinja_cache") if not exists(cache): makedirs(cache)
def blue(string): return "\033[1;34m" + smart_text(string) + "\033[0m"
def green(string): return "\033[1;32m" + smart_text(string) + "\033[0m"
def yellow(string): return "\033[1;33m" + smart_text(string) + "\033[0m"
def red(string): return "\033[1;31m" + smart_text(string) + "\033[0m"
def cyan(string): return "\033[1;36m" + smart_text(string) + "\033[0m"
def mag(string): return "\033[1;35m" + smart_text(string) + "\033[0m"
def white(string): return "\033[1;37m" + smart_text(string) + "\033[0m"
def println(line, content): print("\033[" + smart_text(line) + ";0H\033[2K" + content)
def ask(self, qst, default, answers=[], bool=False, password=False): """produce one line to asking for input""" if answers: info = "(" for i, answer in enumerate(answers): info += (", " if i != 0 else "") + str((answer == default and "[%s]" % answer) or answer) info += ")" elif bool: if default == self.yes: info = "([%s]/%s)" % (self.yes, self.no) else: info = "(%s/[%s])" % (self.yes, self.no) else: info = "[%s]" % default if password: p1 = True p2 = False while p1 != p2: # getpass(_("Password: "******"Password: "******"") if len(p1) < 4: print(_("Password too short. Use at least 4 symbols.")) continue sys.stdout.write(_("Password (again): ")) p2 = getpass("") if p1 == p2: return p1 else: print(_("Passwords did not match.")) while True: try: input_value = input(qst + " %s: " % info) except KeyboardInterrupt: print("\nSetup interrupted") exit() input_value = smart_text(input_value, encoding=self.stdin_encoding) if input_value.strip() == "": input_value = default if bool: # yes, true,t are inputs for booleans with value true if input_value.lower().strip() in [self.yes, _("yes"), _("true"), _("t"), "yes"]: return True # no, false,f are inputs for booleans with value false elif input_value.lower().strip() in [self.no, _("no"), _("false"), _("f"), "no"]: return False else: print(_("Invalid Input")) continue if not answers: return input_value if input_value in answers: return input_value print(_("Invalid Input"))