Esempio n. 1
0
    def run(self):
        try:
            # 读取待转换的书籍信息
            wait_converts = Tbl_Wait_Converts()
            wait_converts_info = wait_converts.get(self.convert_id)
            if not wait_converts_info:
                raise Exception, '未找到待转换的书籍信息'

            # 读取书籍所需图片
            book_img = Tbl_Book_Img()
            book_img_info = book_img.get(self.book_id)
            if book_img_info:
                # 更新书籍所需图片表信息
                book_img.update_local_path(self.book_id,
                                           self.book_images_task.get())

            # 读取书籍信息
            books = Tbl_Books()
            book_info = books.get_by_book_id(self.book_id)

            ## 调用转换功能
            out_file_path = proc_helper.convert(
                str(wait_converts_info['book_html_local_path']), self.out_dir,
                book_info['book_author'], book_info['book_cover_local_path'])
            if out_file_path == None:
                # 转换失败
                wait_converts.update_status(gk7.STATUS.get('error'),
                                            self.convert_id)
                raise Exception, '转换html to mobi失败'

            # 转换成功,修改状态,添加书籍输出路径
            wait_converts.update_status(gk7.STATUS.get('complete'),
                                        self.convert_id, out_file_path)
            # 修改书籍文件路径
            books.update_file_path(self.book_id, out_file_path)

            wait_email = Tbl_Wait_Emails()
            # 修改待发送邮件附件信息
            wait_email.update_attach_file(self.email_id, out_file_path)
            # 读取待发送邮件信息
            wait_email_info = wait_email.get(self.email_id)
            if not wait_email_info:
                raise Exception, '未找到待发送邮件信息,邮件ID:%s' % self.email_id

            # 发送邮件
            Common.send_mail(self.send_mail_type, self.email_id,
                             wait_email_info['email_attach_file'],
                             str(wait_email_info['email_to_user']),
                             str(wait_email_info['email_title']),
                             str(wait_email_info['email_auth']))
        except Exception, err:
            logger.error(u'异步线程出错,转换ID:%s,错误信息:%s', self.convert_id, err)
            exit(-1)
Esempio n. 2
0
    def execute(self, args):
        try:
            # 插件版本
            version = args.get('version')
            if str(version) != '2.8':
                return json.dumps({'status': 'WARN', 'msg': u'您的插件版本太低了,请升级,豆瓣小组:https://www.douban.com/group/544287/'})
            # 加密id
            tmpl_id = args.get('tmplId')
            # 加密号(登录:53092, 不登录:24871)
            denum = args.get('denum')
            # 图书数据
            book_data = args.get('bookData')
            # 推送的email地址
            to_email = args.get('toMail')
            # 豆瓣书籍ID
            ebook_id = args.get('ebookId')
            # 图书标题
            book_title = args.get('bookTitle')
            if not book_data or not to_email or not book_title:
                return json.dumps({'status': 'WARN', 'msg': u'参数不能为空,请联系:[email protected]'})
            # 推送类型,默认为'article'
            send_type = args.get('sendType')
            if not send_type:
                send_type = 'article'
            # 用户个人邮箱
            to_private_email = args.get('toPrivateMail')
            # 处理数据
            data = decrypt.parse(tmpl_id, denum, book_data)
            # 文章集合, 图书副标题, 图书作者
            data_posts, book_subtitle, book_author = self.get_book_info(send_type, data)
            # 书籍大小
            book_size = len(book_data)

            #send_mail_type = args.get('sendMailType')

            # 将待发送邮件存储至数据库
            wait_emails = Tbl_Wait_Emails()
            email_id = RandomUtil.random32Str()
            wait_emails.add(email_id, to_email, book_title, book_author)

            # 判断书籍是否存在数据库中
            books = Tbl_Books()
            book_info = books.get(ebook_id, book_size)
            # 如果书籍已经存在
            if book_info:
                # 修改待发送邮件附件信息
                attach_file = book_info['book_file_path']
                # 如果不为空直接发送邮件
                if attach_file:
                    wait_emails.update_attach_file(email_id, attach_file)
                    # 发送邮件
                    isSend = Api.send_mail(email_id, attach_file, to_email, book_title, book_author)
                    # 发送邮件至个人邮箱to_private_email
                    if to_private_email:
                        private_email_id = RandomUtil.random32Str()
                        wait_emails.add_full(private_email_id, to_private_email, book_title, book_author, attach_file)
                        MailTask.send.delay(private_email_id, attach_file, to_private_email, book_title, book_author)
                    if isSend == False:
                        return json.dumps({'status': 'FAIL', 'msg': u'推送失败,原因:发送邮件异常,请联系:[email protected]...'})
                    return json.dumps({'status': 'SUCCESS', 'msg': u'推送成功,请稍侯查看您的kindle...'})

            # 创建HTML
            # 源文件目录[绝对路径](格式:主目录/douban书籍ID/书籍大小)
            file_dir = '%s/%s/%s' %(gk7.DATA_DIRS, ebook_id, str(book_size))
            page = HTML(book_title, book_author, file_dir)
            book_html_local_path, book_images_remote_path = page.create(send_type, data_posts)

            # 书籍封面远程路径
            book_cover_remote_path = gk7.BOOK_COVER_URL.replace('{#id}', ebook_id).replace('{#type}', send_type == gk7.BOOK_TYPE['gallery'] and gk7.BOOK_TYPE['article'] or send_type)
            # 存储书籍信息
            book_id = RandomUtil.random32Str()
            books.add(book_id, ebook_id, book_title, book_subtitle, book_author, book_size, book_cover_remote_path)
            # 抓取书籍封面并存储本地
            book_cover_task = DownloadTask.get_image.apply_async((book_cover_remote_path, gk7.BOOK_COVER_DIRS), )
            # 更新书籍封面路径
            books.update_cover(book_id, book_cover_task.get())

            # 书籍图片下载任务
            book_images_task = None            
            if len(book_images_remote_path):
                # 存储书籍图片路径信息
                book_img = Tbl_Book_Img()
                book_img.add(RandomUtil.random32Str(), book_id, book_images_remote_path)
                # celery异步任务下载书籍图片
                from helper.tasks import DownloadTask as dt
                from celery import group
                job = group(dt.get_image.s(url, file_dir) for url in book_images_remote_path)
                book_images_task = job.apply_async()

            # 将待转换的书籍html信息存储在数据库中
            wait_converts = Tbl_Wait_Converts()
            convert_id = RandomUtil.random32Str()
            wait_converts.add(convert_id, to_email, book_html_local_path)

            # 开启异步进程,转换书籍并发送邮件
            # 书籍输出目录[OUT_DATA_DIRS/douban书籍ID/大小/]
            out_file_dir = '%s/%s/%s' %(gk7.OUT_DATA_DIRS, ebook_id, str(book_size))
            thread = SyncThread(convert_id, email_id, book_id, out_file_dir, book_images_task, to_private_email)
            thread._children = weakref.WeakKeyDictionary()
            thread.start()
            if len(book_images_remote_path):
                return json.dumps({'status': 'SUCCESS', 'msg': u'推送成功,书籍中存在图片,推送的时间更长一些,请稍侯查看您的kindle...'})
            return json.dumps({'status': 'SUCCESS', 'msg': u'推送成功,请稍侯查看您的kindle...'})
        except Exception, err:
            logger.error(u'推送异常,错误信息:%s,入参:%s' %(err, str(args)))
            return json.dumps({'status': 'ABNORMAL', 'msg': u'推送异常,%s,请联系:[email protected]...' %err})