Example #1
0
 def SendToKindle(self, emails, title, booktype, attachment, tz=TIMEZONE, filewithtime=True):
     if not isinstance(emails, list):
         emails = [emails,]
         
     if PINYIN_FILENAME: # 将中文文件名转换为拼音
         from calibre.ebooks.unihandecode.unidecoder import Unidecoder
         decoder = Unidecoder()
         basename = decoder.decode(title)
     else:
         basename = title
     
     for email in emails:
         user = KeUser.all().filter("kindle_email = ", email).get()
         tz = user.timezone if user else TIMEZONE
         if filewithtime:
             filename = "%s(%s).%s"%(basename,local_time('%Y-%m-%d_%H-%M',tz=tz),booktype)
         else:
             filename = "%s.%s"%(basename,booktype)
         try:
             mail.send_mail(SrcEmail, email, "KindleEar", "Deliver from KindlerEar",
                 attachments=[(filename, attachment),])
         except OverQuotaError as e:
             self.log.warn('overquota when sendmail to %s:%s', (email, str(e)))
             self.deliverlog(email, title, len(attachment), tz=tz, status='over quota')
         except Exception as e:
             default_log.warn('sendmail to %s failed:%s', (email, str(e)))
             self.deliverlog(email, title, len(attachment), tz=tz, status='send failed')
         else:
             self.deliverlog(email, title, len(attachment), tz=tz)
Example #2
0
    def SendToKindle(self, name, to, title, booktype, attachment, tz=TIMEZONE, filewithtime=True):
        if PINYIN_FILENAME:  # 将中文文件名转换为拼音
            from calibre.ebooks.unihandecode.unidecoder import Unidecoder

            decoder = Unidecoder()
            basename = decoder.decode(title)
        else:
            basename = title

        lctime = local_time("%Y-%m-%d_%H-%M", tz)
        if filewithtime:
            filename = "%s(%s).%s" % (basename, lctime, booktype)
        else:
            filename = "%s.%s" % (basename, booktype)
        try:
            mail.send_mail(
                SrcEmail, to, "KindleEar %s" % lctime, "Deliver from KindlerEar", attachments=[(filename, attachment)]
            )
        except OverQuotaError as e:
            default_log.warn("overquota when sendmail to %s:%s" % (to, str(e)))
            self.deliverlog(name, to, title, len(attachment), tz=tz, status="over quota")
        except Exception as e:
            default_log.warn("sendmail to %s failed:%s" % (to, str(e)))
            self.deliverlog(name, to, title, len(attachment), tz=tz, status="send failed")
        else:
            self.deliverlog(name, to, title, len(attachment), tz=tz)
Example #3
0
 def SendToKindle(self, name, to, title, booktype, attachment, tz=TIMEZONE, filewithtime=False):
     if PINYIN_FILENAME: # 将中文文件名转换为拼音
         from calibre.ebooks.unihandecode.unidecoder import Unidecoder
         decoder = Unidecoder()
         basename = decoder.decode(title)
     else:
         basename = title
         
     lctime = local_time('%Y-%m-%d_%H-%M',tz)
     if booktype:
         if filewithtime:
             filename = "%s(%s).%s"%(basename,lctime,booktype)
         else:
             filename = "%s.%s"%(basename,booktype)
     else:
         filename = basename
         
     for i in range(SENDMAIL_RETRY_CNT+1):
         try:
             mail.send_mail(SRC_EMAIL, to, "KindleEar %s" % lctime, "Deliver from KindleEar",
                 attachments=[(filename, attachment),])
         except OverQuotaError as e:
             default_log.warn('overquota when sendmail to %s:%s' % (to, str(e)))
             self.deliverlog(name, str(to), title, len(attachment), tz=tz, status='over quota')
             default_log.warn('overquota when sendmail to %s:%s, retry!' % (to, str(e)))
             time.sleep(10)
             if i>2:
                 break
         except InvalidSenderError as e:
             default_log.warn('UNAUTHORIZED_SENDER when sendmail to %s:%s' % (to, str(e)))
             self.deliverlog(name, str(to), title, len(attachment), tz=tz, status='wrong SRC_EMAIL')
             break
         except InvalidAttachmentTypeError as e: #继续发送一次
             if SENDMAIL_ALL_POSTFIX:
                 filename = filename.replace('.', '_')
                 title = title.replace('.', '_')
             else:
                 default_log.warn('InvalidAttachmentTypeError when sendmail to %s:%s' % (to, str(e)))
                 self.deliverlog(name, str(to), title, len(attachment), tz=tz, status='invalid postfix')
                 break
         except DeadlineExceededError as e:
             if i < SENDMAIL_RETRY_CNT:
                 default_log.warn('timeout when sendmail to %s:%s, retry!' % (to, str(e)))
                 time.sleep(5)
             else:
                 default_log.warn('timeout when sendmail to %s:%s, abort!' % (to, str(e)))
                 self.deliverlog(name, str(to), title, len(attachment), tz=tz, status='timeout')
                 break
         except Exception as e:
             default_log.warn('sendmail to %s failed:%s.<%s>' % (to, str(e), type(e)))
             self.deliverlog(name, str(to), title, len(attachment), tz=tz, status='send failed')
             break
         else:
             self.deliverlog(name, str(to), title, len(attachment), tz=tz)
             break
Example #4
0
 def SendToKindle(self, name, to, title, booktype, attachment, tz=TIMEZONE, filewithtime=True):
     if PINYIN_FILENAME: # 将中文文件名转换为拼音
         from calibre.ebooks.unihandecode.unidecoder import Unidecoder
         decoder = Unidecoder()
         basename = decoder.decode(title)
     else:
         basename = title
         
     lctime = local_time('%Y-%m-%d_%H-%M',tz)
     if booktype:
         if filewithtime:
             filename = "%s(%s).%s"%(basename,lctime,booktype)
         else:
             filename = "%s.%s"%(basename,booktype)
     else:
         filename = basename
         
     for i in range(SENDMAIL_RETRY_CNT+1):
         try:
             mail.send_mail(SRC_EMAIL, to, "KindleEar %s" % lctime, "Deliver from KindleEar",
                 attachments=[(filename, attachment),])
         except OverQuotaError as e:
             default_log.warn('overquota when sendmail to %s:%s' % (to, str(e)))
             self.deliverlog(name, to, title, len(attachment), tz=tz, status='over quota')
             default_log.warn('overquota when sendmail to %s:%s, retry!' % (to, str(e)))
             time.sleep(10)
             if i>2:
                 break
         except InvalidSenderError as e:
             default_log.warn('UNAUTHORIZED_SENDER when sendmail to %s:%s' % (to, str(e)))
             self.deliverlog(name, to, title, len(attachment), tz=tz, status='wrong SRC_EMAIL')
             break
         except InvalidAttachmentTypeError as e: #继续发送一次
             if SENDMAIL_ALL_POSTFIX:
                 filename = filename.replace('.', '_')
                 title = title.replace('.', '_')
             else:
                 default_log.warn('InvalidAttachmentTypeError when sendmail to %s:%s' % (to, str(e)))
                 self.deliverlog(name, to, title, len(attachment), tz=tz, status='invalid postfix')
                 break
         except DeadlineExceededError as e:
             if i < SENDMAIL_RETRY_CNT:
                 default_log.warn('timeout when sendmail to %s:%s, retry!' % (to, str(e)))
                 time.sleep(5)
             else:
                 default_log.warn('timeout when sendmail to %s:%s, abort!' % (to, str(e)))
                 self.deliverlog(name, to, title, len(attachment), tz=tz, status='timeout')
                 break
         except Exception as e:
             default_log.warn('sendmail to %s failed:%s.<%s>' % (to, str(e), type(e)))
             self.deliverlog(name, to, title, len(attachment), tz=tz, status='send failed')
             break
         else:
             self.deliverlog(name, to, title, len(attachment), tz=tz)
             break
Example #5
0
 def SendToKindle(self, emails, title, booktype, attachment, tz=TIMEZONE):
     if PINYIN_FILENAME:
         from calibre.ebooks.unihandecode.unidecoder import Unidecoder
         decoder = Unidecoder()
         filename = decoder.decode(title)
     else:
         filename = title
     filename = "%s(%s).%s"%(filename,local_time('%Y-%m-%d_%H-%M',tz=tz),booktype)
     
     self.deliverlog(emails, title, len(attachment), tz=tz)
     mail.send_mail(SrcEmail, emails, "KindleEar", "Deliver from KindlerEar",
             attachments=[(filename, attachment),])
Example #6
0
 def __init__(self, lang="zh", encoding='utf-8'):
     self.preferred_encoding = encoding
     lang = lang.lower()
     if lang[:2] == 'ja':
         from calibre.ebooks.unihandecode.jadecoder import Jadecoder
         self.decoder = Jadecoder()
     elif lang[:2] == 'kr' or lang == 'korean':
         from calibre.ebooks.unihandecode.krdecoder import Krdecoder
         self.decoder = Krdecoder()
     elif lang[:2] == 'vn' or lang == 'vietnum':
         from calibre.ebooks.unihandecode.vndecoder import Vndecoder
         self.decoder = Vndecoder()
     else:  # zh and others
         from calibre.ebooks.unihandecode.unidecoder import Unidecoder
         self.decoder = Unidecoder()
Example #7
0
 def __init__(self, lang="zh", encoding='utf-8'):
     self.preferred_encoding = encoding
     lang = lang.lower()
     if lang[:2] == u'ja':
         from calibre.ebooks.unihandecode.jadecoder import Jadecoder
         self.decoder = Jadecoder()
     elif lang[:2] == u'kr' or lang == u'korean':
         from calibre.ebooks.unihandecode.krdecoder import Krdecoder
         self.decoder = Krdecoder()
     elif lang[:2] == u'vn' or lang == u'vietnum':
         from calibre.ebooks.unihandecode.vndecoder import Vndecoder
         self.decoder = Vndecoder()
     else:  # zh and others
         from calibre.ebooks.unihandecode.unidecoder import Unidecoder
         self.decoder = Unidecoder()
Example #8
0
class Unihandecoder(object):
    preferred_encoding = None
    decoder = None

    def __init__(self, lang="zh", encoding='utf-8'):
        self.preferred_encoding = encoding
        lang = lang.lower()
        if lang[:2] == 'ja':
            from calibre.ebooks.unihandecode.jadecoder import Jadecoder
            self.decoder = Jadecoder()
        elif lang[:2] == 'kr' or lang == 'korean':
            from calibre.ebooks.unihandecode.krdecoder import Krdecoder
            self.decoder = Krdecoder()
        elif lang[:2] == 'vn' or lang == 'vietnum':
            from calibre.ebooks.unihandecode.vndecoder import Vndecoder
            self.decoder = Vndecoder()
        else:  # zh and others
            from calibre.ebooks.unihandecode.unidecoder import Unidecoder
            self.decoder = Unidecoder()

    def decode(self, text):
        if isinstance(text, bytes):
            try:
                text = text.decode(self.preferred_encoding)
            except Exception:
                text = text.decode('utf-8', 'replace')
        # at first unicode normalize it. (see Unicode standards)
        ntext = unicodedata.normalize('NFKC', text)
        return self.decoder.decode(ntext)
Example #9
0
class Unihandecoder(object):
    preferred_encoding = None
    decoder = None

    def __init__(self, lang="zh", encoding='utf-8'):
        self.preferred_encoding = encoding
        lang = lang.lower()
        if lang[:2] == u'ja':
            from calibre.ebooks.unihandecode.jadecoder import Jadecoder
            self.decoder = Jadecoder()
        elif lang[:2] == u'kr' or lang == u'korean':
            from calibre.ebooks.unihandecode.krdecoder import Krdecoder
            self.decoder = Krdecoder()
        elif lang[:2] == u'vn' or lang == u'vietnum':
            from calibre.ebooks.unihandecode.vndecoder import Vndecoder
            self.decoder = Vndecoder()
        else:  # zh and others
            from calibre.ebooks.unihandecode.unidecoder import Unidecoder
            self.decoder = Unidecoder()

    def decode(self, text):
        if isinstance(text, bytes):
            try:
                text = text.decode(self.preferred_encoding)
            except Exception:
                text = text.decode('utf-8', 'replace')
        # at first unicode normalize it. (see Unicode standards)
        ntext = unicodedata.normalize('NFKC', text)
        return self.decoder.decode(ntext)
Example #10
0
    def SendToKindle(self,
                     name,
                     to,
                     title,
                     booktype,
                     attachment,
                     tz=TIMEZONE,
                     filewithtime=True):
        if PINYIN_FILENAME:  # 将中文文件名转换为拼音
            from calibre.ebooks.unihandecode.unidecoder import Unidecoder
            decoder = Unidecoder()
            basename = decoder.decode(title)
        else:
            basename = title

        lctime = local_time('%Y-%m-%d_%H-%M', tz)
        if filewithtime:
            filename = "%s(%s).%s" % (basename, lctime, booktype)
        else:
            filename = "%s.%s" % (basename, booktype)
        try:
            mail.send_mail(SRC_EMAIL,
                           to,
                           "KindleEar %s" % lctime,
                           "Deliver from KindlerEar",
                           attachments=[
                               (filename, attachment),
                           ])
        except OverQuotaError as e:
            default_log.warn('overquota when sendmail to %s:%s' % (to, str(e)))
            self.deliverlog(name,
                            to,
                            title,
                            len(attachment),
                            tz=tz,
                            status='over quota')
        except Exception as e:
            default_log.warn('sendmail to %s failed:%s' % (to, str(e)))
            self.deliverlog(name,
                            to,
                            title,
                            len(attachment),
                            tz=tz,
                            status='send failed')
        else:
            self.deliverlog(name, to, title, len(attachment), tz=tz)
Example #11
0
 def SendToKindle(self, emails, title, booktype, attachment, tz=TIMEZONE):
     if not isinstance(emails, list):
         emails = [emails,]
         
     if PINYIN_FILENAME: # 将中文文件名转换为拼音
         from calibre.ebooks.unihandecode.unidecoder import Unidecoder
         decoder = Unidecoder()
         basename = decoder.decode(title)
     else:
         basename = title
     
     for email in emails:
         user = KeUser.all().filter("kindle_email = ", email).get()
         tz = user.timezone if user else TIMEZONE
         filename = "%s(%s).%s"%(basename,local_time('%Y-%m-%d_%H-%M',tz=tz),booktype)
         mail.send_mail(SrcEmail, email, "KindleEar", "Deliver from KindlerEar",
             attachments=[(filename, attachment),])
         self.deliverlog(email, title, len(attachment), tz=tz)