ebook.download_books([ebook.book_title], into_folder=into_folder) else: cfg.download_folder_path = os.getcwd() ebook.download_books([ebook.book_title], into_folder=into_folder) # Send downloaded book(s) by mail or to google_drive if args.sgd or args.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 ebook.book_title in path ] if args.sgd: from utils.google_drive import GoogleDriveManager 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:
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 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 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)