googleDrive.send_files(paths) elif args.mail: from utils.mail import MailBook mb = MailBook(cfgFilePath) pdfPath = None mobiPath = None try: pdfPath = [ path for path in paths if path.split('.')[-1] == 'pdf' ][-1] mobiPath = [ path for path in paths if path.split('.')[-1] == 'mobi' ][-1] except: pass if pdfPath: mb.send_book(pdfPath) if mobiPath: mb.send_kindle(mobiPath) if args.sgd or args.mail: [os.remove(path) for path in paths] elif args.dall: downloader.downloadBooks() elif args.dchosen: downloader.downloadBooks(myAccount.downloadBookTitles) logger.success("Good, looks like all went well! :-)") except Exception as e: logger.error("Exception occurred {}".format(e))
def packt_cli(cfgpath, grab, grabd, dall, sgd, mail, status_mail, folder, noauth_local_webserver): config_file_path = cfgpath into_folder = folder try: cfg = ConfigurationModel(config_file_path) ebook = PacktPublishingFreeEbook(cfg) api_client = PacktAPIClient(cfg.my_packt_email, cfg.my_packt_password) # Grab the newest book if grab or grabd or sgd or mail: ebook.grab_ebook(api_client) # Send email about successful book grab. Do it only when book # isn't going to be emailed as we don't want to send email twice. if status_mail and not mail: from utils.mail import MailBook mb = MailBook(config_file_path) mb.send_info(subject=SUCCESS_EMAIL_SUBJECT.format( dt.datetime.now().strftime(DATE_FORMAT), ebook.book_data['title']), body=SUCCESS_EMAIL_BODY.format( ebook.book_data['title'])) # Download book(s) into proper location. if grabd or dall or sgd or mail: if dall: ebook.download_books(api_client, into_folder=into_folder) elif grabd: ebook.download_books(api_client, ebook.book_data, into_folder=into_folder) else: # sgd or mail # download it temporarily to cwd cfg.download_folder_path = os.getcwd() ebook.download_books(api_client, ebook.book_data, into_folder=False) # Send downloaded book(s) by mail or to Google Drive. if sgd or mail: paths = [ os.path.join(cfg.download_folder_path, path) for path in os.listdir(cfg.download_folder_path) if os.path.isfile(path) and slugify_book_title(ebook.book_data['title']) in path ] if sgd: from utils.google_drive import GoogleDriveManager google_drive = GoogleDriveManager(config_file_path) google_drive.send_files(paths) else: from utils.mail import MailBook mb = MailBook(config_file_path) pdf_path = None mobi_path = None try: pdf_path = [ path for path in paths if path.endswith('.pdf') ][-1] mobi_path = [ path for path in paths if path.endswith('.mobi') ][-1] except IndexError: pass if pdf_path: mb.send_book(pdf_path) if mobi_path: mb.send_kindle(mobi_path) for path in paths: os.remove(path) logger.success("Good, looks like all went well! :-)") except Exception as e: logger.error("Exception occurred {}".format(e)) if status_mail: from utils.mail import MailBook mb = MailBook(config_file_path) mb.send_info(subject=FAILURE_EMAIL_SUBJECT.format( dt.datetime.now().strftime(DATE_FORMAT)), body=FAILURE_EMAIL_BODY.format(str(e))) sys.exit(2)
google_drive = GoogleDriveManager(cfg_file_path) google_drive.send_files(paths) else: from utils.mail import MailBook mb = MailBook(cfg_file_path) pdf_path = None mobi_path = None try: pdf_path = [path for path in paths if path.endswith('.pdf')][-1] mobi_path = [path for path in paths if path.endswith('.mobi')][-1] except IndexError: pass if pdf_path: mb.send_book(pdf_path) if mobi_path: mb.send_kindle(mobi_path) for path in paths: os.remove(path) logger.success("Good, looks like all went well! :-)") except Exception as e: logger.error("Exception occurred {}".format(e)) if args.status_mail: from utils.mail import MailBook mb = MailBook(cfg_file_path) mb.send_info( subject=FAILURE_EMAIL_SUBJECT.format(dt.datetime.now().strftime(DATE_FORMAT)), body=FAILURE_EMAIL_BODY.format(str(e)) ) sys.exit(2)
def packt_cli(cfgpath, grab, grabd, dall, sgd, mail, status_mail, folder, jwt, cookiejwt, noauth_local_webserver, product): config_file_path = cfgpath into_folder = folder try: cfg = ConfigurationModel(config_file_path) product_data = None if jwt or cookiejwt: recaptcha_solution = '' else: recaptcha_solution = solve_recaptcha(cfg.anticaptcha_api_key, PACKT_URL, PACKT_RECAPTCHA_SITE_KEY) cj = None if cookiejwt: logger.info("Fetching packtpub.com cookie") cj = browser_cookie3.load(domain_name='.packtpub.com') api_client = PacktAPIClient( { 'recaptcha': recaptcha_solution, **cfg.packt_login_credentials }, cj) if product != '': download_one = True else: download_one = False # Grab the newest book if grab or grabd or sgd or mail: product_data = claim_product(api_client, recaptcha_solution) # Send email about successful book grab. Do it only when book # isn't going to be emailed as we don't want to send email twice. if status_mail and not mail: from utils.mail import MailBook mb = MailBook(config_file_path) mb.send_info(subject=SUCCESS_EMAIL_SUBJECT.format( dt.datetime.now().strftime(DATE_FORMAT), product_data['title']), body=SUCCESS_EMAIL_BODY.format( product_data['title'])) # Download book(s) into proper location. if grabd or dall or sgd or mail or download_one: download_directory, formats = cfg.config_download_data download_directory = download_directory if ( dall or grabd) else os.getcwd() # cwd for temporary downloads formats = formats or AVAILABLE_DOWNLOAD_FORMATS # Download one book into proper location. if download_one: product_data = get_book_data(api_client, product) if product_data is not None: # get_product_download_urls download_products(api_client, download_directory, formats, [product_data], into_folder=into_folder) elif dall: download_products(api_client, download_directory, formats, get_all_books_data(api_client), into_folder=into_folder) elif grabd: download_products(api_client, download_directory, formats, [product_data], into_folder=into_folder) else: # sgd or mail download_products(api_client, download_directory, formats, [product_data], into_folder=False) # Send downloaded book(s) by mail or to Google Drive. if sgd or mail: paths = [ os.path.join(download_directory, path) for path in os.listdir(download_directory) if os.path.isfile(path) and slugify_product_name(product_data['title']) in path ] if sgd: from utils.google_drive import GoogleDriveManager google_drive = GoogleDriveManager(config_file_path) google_drive.send_files(paths) else: from utils.mail import MailBook mb = MailBook(config_file_path) pdf_path = None mobi_path = None try: pdf_path = [ path for path in paths if path.endswith('.pdf') ][-1] mobi_path = [ path for path in paths if path.endswith('.mobi') ][-1] except IndexError: pass if pdf_path: mb.send_book(pdf_path) if mobi_path: mb.send_kindle(mobi_path) for path in paths: os.remove(path) logger.success("Good, looks like all went well! :-)") except Exception as e: logger.error("Exception occurred {}".format(e)) if status_mail: from utils.mail import MailBook mb = MailBook(config_file_path) mb.send_info(subject=FAILURE_EMAIL_SUBJECT.format( dt.datetime.now().strftime(DATE_FORMAT)), body=FAILURE_EMAIL_BODY.format(str(e))) sys.exit(2)
def packt_cli(cfgpath, grab, grabd, dall, sgd, mail, status_mail, folder, noauth_local_webserver): config_file_path = cfgpath into_folder = folder try: cfg = ConfigurationModel(config_file_path) ebook = PacktPublishingFreeEbook(cfg) api_client = PacktAPIClient(cfg.my_packt_email, cfg.my_packt_password) # Grab the newest book if grab or grabd or sgd or mail: ebook.grab_ebook(api_client) # Send email about successful book grab. Do it only when book # isn't going to be emailed as we don't want to send email twice. if status_mail and not mail: from utils.mail import MailBook mb = MailBook(config_file_path) mb.send_info( subject=SUCCESS_EMAIL_SUBJECT.format( dt.datetime.now().strftime(DATE_FORMAT), ebook.book_data['title'] ), body=SUCCESS_EMAIL_BODY.format(ebook.book_data['title']) ) # Download book(s) into proper location. if grabd or dall or sgd or mail: if dall: ebook.download_books(api_client, into_folder=into_folder) elif grabd: ebook.download_books(api_client, ebook.book_data, into_folder=into_folder) else: # sgd or mail # download it temporarily to cwd cfg.download_folder_path = os.getcwd() ebook.download_books(api_client, ebook.book_data, into_folder=False) # Send downloaded book(s) by mail or to Google Drive. if sgd or mail: paths = [ os.path.join(cfg.download_folder_path, path) for path in os.listdir(cfg.download_folder_path) if os.path.isfile(path) and slugify_book_title(ebook.book_data['title']) in path ] if sgd: from utils.google_drive import GoogleDriveManager google_drive = GoogleDriveManager(config_file_path) google_drive.send_files(paths) else: from utils.mail import MailBook mb = MailBook(config_file_path) pdf_path = None mobi_path = None try: pdf_path = [path for path in paths if path.endswith('.pdf')][-1] mobi_path = [path for path in paths if path.endswith('.mobi')][-1] except IndexError: pass if pdf_path: mb.send_book(pdf_path) if mobi_path: mb.send_kindle(mobi_path) for path in paths: os.remove(path) logger.success("Good, looks like all went well! :-)") except Exception as e: logger.error("Exception occurred {}".format(e)) if status_mail: from utils.mail import MailBook mb = MailBook(config_file_path) mb.send_info( subject=FAILURE_EMAIL_SUBJECT.format(dt.datetime.now().strftime(DATE_FORMAT)), body=FAILURE_EMAIL_BODY.format(str(e)) ) sys.exit(2)
def main(): parser = argparse.ArgumentParser() parser.add_argument("-g", "--grab", help="grabs daily ebook", action="store_true") parser.add_argument("-gl", "--grabl", help="grabs and log ebook extra info data", action="store_true") parser.add_argument("-gd", "--grabd", help="grabs daily ebook and downloads the title afterwards", action="store_true") parser.add_argument("-da", "--dall", help="downloads all ebooks from your account", action="store_true") parser.add_argument("-dc", "--dchosen", help="downloads chosen titles described in [downloadBookTitles] field", action="store_true") parser.add_argument("-sgd", "--sgd", help="sends the grabbed eBook to google drive", action="store_true") parser.add_argument("-m", "--mail", help="send download to emails defined in config file", default=False, action="store_true") parser.add_argument("-f", "--folder", help="downloads eBook into a folder", default=False, action="store_true") parser.add_argument("-c", "--config", help="specify custom config file (use 'configFile.cfg' by default)", default='configFile.cfg') args = parser.parse_args() cfgFilePath = args.config try: myAccount = PacktAccountData(cfgFilePath) grabber = FreeEBookGrabber(myAccount) downloader = BookDownloader(myAccount) if args.sgd: from utils.googleDrive import GoogleDriveManager googleDrive= GoogleDriveManager(cfgFilePath) if args.grab or args.grabl or args.grabd or args.sgd or args.mail: if not args.grabl: grabber.grabEbook() else: grabber.grabEbook(logEbookInfodata=True) if args.grabd or args.dall or args.dchosen or args.sgd or args.mail: downloader.getDataOfAllMyBooks() intoFolder = False if args.folder: intoFolder = True if args.grabd or args.sgd or args.mail: if args.sgd or args.mail: myAccount.downloadFolderPath = os.getcwd() downloader.downloadBooks([grabber.bookTitle], intoFolder = intoFolder) if args.sgd or args.mail: paths = [os.path.join(myAccount.downloadFolderPath, path) \ for path in os.listdir(myAccount.downloadFolderPath) \ if os.path.isfile(path) and path.find(grabber.bookTitle) is not -1] if args.sgd: googleDrive.send_files(paths) elif args.mail: from utils.mail import MailBook mb = MailBook(cfgFilePath) pdfPath = None mobiPath = None try: pdfPath = [path for path in paths if path.split('.')[-1] == 'pdf'][-1] mobiPath = [path for path in paths if path.split('.')[-1] == 'mobi'][-1] except: pass if pdfPath: mb.send_book(pdfPath) if mobiPath: mb.send_kindle(mobiPath) if args.sgd or args.mail: [os.remove(path) for path in paths] elif args.dall: downloader.downloadBooks(intoFolder = intoFolder) elif args.dchosen: downloader.downloadBooks(myAccount.downloadBookTitles, intoFolder = intoFolder) logger.success("Good, looks like all went well! :-)") except Exception as e: logger.error("Exception occurred {}".format(e))
def packt_cli(cfgpath, grab, grabd, dall, sgd, oc, ocall, mail, status_mail, folder, cli, noauth_local_webserver): config_file_path = cfgpath into_folder = folder try: cfg = ConfigurationModel(config_file_path) product_data = None api_client = PacktAPIClient(*cfg.packt_login_credentials) # Grab the newest book if grab or grabd or sgd or mail or oc: product_data = claim_product(api_client, cfg.anticaptcha_api_key) # Send email about successful book grab. Do it only when book # isn't going to be emailed as we don't want to send email twice. if status_mail and not mail: from utils.mail import MailBook mb = MailBook(config_file_path) mb.send_info(subject=SUCCESS_EMAIL_SUBJECT.format( dt.datetime.now().strftime(DATE_FORMAT), product_data['title']), body=SUCCESS_EMAIL_BODY.format( product_data['title'])) # Download book(s) into proper location. if grabd or dall or sgd or mail or oc or ocall or cli: download_directory, formats = cfg.config_download_data download_directory = download_directory if ( dall or grabd or cli) else os.getcwd() # cwd for temporary downloads formats = formats or AVAILABLE_DOWNLOAD_FORMATS if cli: from cli import Prompt all_books = get_all_books_data(api_client) cli = Prompt("tab", None, None, all_books, api_client, download_directory, formats, into_folder=into_folder) cli.cmdloop() if dall or ocall: download_products(api_client, download_directory, formats, get_all_books_data(api_client), into_folder=into_folder) elif grabd: download_products(api_client, download_directory, formats, [product_data], into_folder=into_folder) else: # sgd or mail or oc download_products(api_client, download_directory, formats, [product_data], into_folder=False) # Send downloaded book(s) by mail, owncloud, nextcloud or to Google Drive. if sgd or mail or oc or ocall: if ocall: formats = ['.pdf', '.mobi', '.epub', '.zip'] # This is not that good but works for me - needs refactor paths = [ os.path.join(download_directory, path) for path in os.listdir(download_directory) if os.path.isfile(path) and os.path.splitext(path)[1] in formats ] else: paths = [ os.path.join(download_directory, path) for path in os.listdir(download_directory) if os.path.isfile(path) and slugify_product_name(product_data['title']) in path ] if sgd: from utils.google_drive import GoogleDriveManager google_drive = GoogleDriveManager(config_file_path) google_drive.send_files(paths) elif oc or ocall: from utils.occloud import OwncloudManager oc = OwncloudManager(config_file_path) oc.send_files(paths) else: from utils.mail import MailBook mb = MailBook(config_file_path) pdf_path = None mobi_path = None try: pdf_path = [ path for path in paths if path.endswith('.pdf') ][-1] mobi_path = [ path for path in paths if path.endswith('.mobi') ][-1] except IndexError: pass if pdf_path: mb.send_book(pdf_path) if mobi_path: mb.send_kindle(mobi_path) for path in paths: os.remove(path) logger.success("Good, looks like all went well! :-)") except Exception as e: logger.error("Exception occurred {}".format(e)) if status_mail: from utils.mail import MailBook mb = MailBook(config_file_path) mb.send_info(subject=FAILURE_EMAIL_SUBJECT.format( dt.datetime.now().strftime(DATE_FORMAT)), body=FAILURE_EMAIL_BODY.format(str(e))) sys.exit(2)