### Instantiate and Setup CrawlerConfig Source: https://github.com/fhamborg/news-please/wiki/config-parser Get the singleton instance of CrawlerConfig and set up the configuration file path. This should be done once at the start of the application. ```python cfg = CrawlerConfig.get_instance() cfg.setup() ``` -------------------------------- ### Instantiate and Setup JsonConfig Source: https://github.com/fhamborg/news-please/wiki/config-parser Obtain the singleton instance of JsonConfig and specify the path to the JSON configuration file. This setup step should be performed once. ```python json = JsonConfig.get_instance() json.setup() ``` -------------------------------- ### Install news-please using pip Source: https://github.com/fhamborg/news-please/blob/master/README.md Install the news-please library using pip. This command is for Python 3.8+. ```bash pip install news-please ``` -------------------------------- ### CLI Mode: Install and Run NewsPlease Source: https://context7.com/fhamborg/news-please/llms.txt Installs the news-please package using pip and runs it from the command line. Use the -c flag to specify a custom configuration directory. ```bash # Install news-please pip install news-please # Run with default configuration (creates ~/news-please/config/) news-please # Run with custom config directory news-please -c /path/to/config # The config directory contains: # - config.cfg: Main configuration file # - sitelist.hjson: List of websites to crawl ``` -------------------------------- ### Build and Upload Package to PyPI Source: https://github.com/fhamborg/news-please/wiki/PyPI---How-to-upload-a-new-version Execute these commands in the root directory after updating the version in setup.py. Ensure build and twine are installed. ```bash python -m build ``` ```bash python -m twine upload dist/* ``` ```bash find dist -mindepth 1 -delete ``` ```bash find news_please.egg-info -mindepth 1 -delete ``` -------------------------------- ### Install news-please via pip Source: https://github.com/fhamborg/news-please/wiki/user-guide Install the news-please package using pip. This command is for systems with Python 3.5+. ```bash sudo pip install news-please ``` -------------------------------- ### Configure News-Please Crawling Source: https://context7.com/fhamborg/news-please/llms.txt Set up filter hosts, date ranges, and callback functions for article extraction and WARC processing. Ensure the download directory exists before starting. ```python FILTER_HOSTS = ['reuters.com', 'bbc.com'] # Empty list = all hosts START_DATE = datetime.datetime(2024, 1, 1) END_DATE = datetime.datetime(2024, 1, 31) WARC_START_DATE = datetime.datetime(2024, 1, 1) WARC_END_DATE = datetime.datetime(2024, 2, 1) os.makedirs(DOWNLOAD_DIR_ARTICLES, exist_ok=True) def on_article_extracted(article): """Callback for each successfully extracted article""" # Save to JSON file filename = hashlib.sha256(article.url.encode()).hexdigest()[:16] filepath = os.path.join(DOWNLOAD_DIR_ARTICLES, f"{filename}.json") with open(filepath, 'w', encoding='utf-8') as f: json.dump(article.__dict__, f, default=str, indent=2, ensure_ascii=False) print(f"Saved: {article.title[:50]}...") def on_warc_completed(warc_path, passed, discarded, errors, total, processed): """Callback when a WARC file is fully processed""" print(f"WARC completed: {warc_path}") print(f" Articles: passed={passed}, discarded={discarded}, errors={errors}") # Start crawling commoncrawl_crawler.crawl_from_commoncrawl( callback_on_article_extracted=on_article_extracted, callback_on_warc_completed=on_warc_completed, valid_hosts=FILTER_HOSTS, start_date=START_DATE, end_date=END_DATE, warc_files_start_date=WARC_START_DATE, warc_files_end_date=WARC_END_DATE, strict_date=True, reuse_previously_downloaded_files=True, local_download_dir_warc=DOWNLOAD_DIR_WARC, continue_after_error=True, number_of_extraction_processes=4, delete_warc_after_extraction=True, fetch_images=False, dry_run=False # Set True to list files without processing ) ``` -------------------------------- ### PostgreSQL Connection Configuration Source: https://github.com/fhamborg/news-please/blob/master/README.md Configuration parameters for connecting to a PostgreSQL database. Ensure psycopg2 is installed for production environments. ```ini host = localhost port = 5432 database = 'news-please' # schema = 'news-please' user = 'user' password = 'password' ``` -------------------------------- ### Website Object Configuration Source: https://github.com/fhamborg/news-please/wiki/user-guide Details on the parameters available for configuring the Website Object, which defines the start point, crawler, and heuristics for data collection. ```APIDOC ## Website Object Configuration ### Description Configuration for the `base_urls` entries within the Website Object, allowing customization of crawling start points, crawlers, and heuristics. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **url** (string) - Required - The root URL to start crawling (e.g., "http://example.com"). - **crawler** (string) - Optional - The crawler used to collect data. Refer to [crawlers](crawlers-and-heuristics#markdown-header-crawlers) for implemented crawlers. - **overwrite_heuristics** (dictionary, containing mixed types) - Optional - Overwrites default heuristics for article detection. Expects a dictionary with heuristic names as keys and conditions as values. - **bool**: Acceptable conditions are `True` and `False`. `False` disables the heuristic. - **string**: Acceptable conditions are simple strings like `"string_heuristic": "matched_value"`. - **float/int**: Acceptable conditions are strings with an equality operator (`<`, `>`, `<=`, `>=`, `=`) and a number (e.g., `"linked_headlines": "<=0.65"`). No spaces between operator and number. - **pass_heuristics_condition** (string) - Optional - Overwrites the default boolean expression for evaluating heuristics. Can include heuristic names, boolean operators (`and`, `or`, `not`), and parentheses. - **daemonize** (int) - Optional - If set, the crawler will start as a daemon. The value is the seconds to wait before scraping again. Only supported by `RSSCrawler`. - **additional_rss_daemonize** (int) - Optional - If set, an additional `RSSCrawler` is spawned for the same target. The value is the seconds to wait before scraping again. Not supported by `RSSCrawler`. ### Request Example ```json { "url": "http://example.com", "crawler": "ArticleNewsPlease", "overwrite_heuristics": { "og_type": True, "links_ratio": "<=0.5" }, "pass_heuristics_condition": "og_type and links_ratio", "daemonize": 3600 } ``` ### Response #### Success Response (200) This section is not detailed in the provided text. #### Response Example This section is not detailed in the provided text. ``` -------------------------------- ### sitelist.hjson Configuration for Crawling Source: https://context7.com/fhamborg/news-please/llms.txt Example Hjson configuration file for defining websites to crawl. Specifies base URLs, crawler types (RssCrawler, SitemapCrawler, RecursiveCrawler, Download), and daemonization intervals. ```json // sitelist.hjson - Define sites to crawl { "base_urls": [ { "url": "https://www.reuters.com/", "crawler": "RssCrawler", "daemonize": 3600 }, { "url": "https://www.bbc.com/news/", "crawler": "SitemapCrawler" }, { "url": "https://www.theguardian.com/", "crawler": "RecursiveCrawler", "overwrite_heuristics": { "og_type": true, "linked_headlines": true } }, { "crawler": "Download", "url": [ "https://example.com/article1.html", "https://example.com/article2.html" ] } ] } ``` -------------------------------- ### Access JsonConfig Instance Source: https://github.com/fhamborg/news-please/wiki/config-parser Retrieve the singleton instance of JsonConfig in any module after initial setup. This ensures consistent access to the parsed JSON configuration. ```python json = JsonConfig.get_instance() ``` -------------------------------- ### Get CrawlerConfig Configuration Source: https://github.com/fhamborg/news-please/wiki/config-parser Retrieve a deep copy of the entire configuration as a 2D dictionary. Access specific options using section and option keys. ```python config = cfg.config() config[
][