Process a file containing a list of URLs, fetch each URL, and load the content into the database. Each line in the file should be a single URL pointing to RSS/XML or JSON content. The file itself can be a local file or a URL. Args: file_path: Path to the file containing URL
(file_path: str, site: str, batch_size: int = 100, delete_existing: bool = False, force_recompute: bool = False, database: str | None = None)
| 908 | pass |
| 909 | |
| 910 | async def loadUrlListToDB(file_path: str, site: str, batch_size: int = 100, delete_existing: bool = False, force_recompute: bool = False, database: str | None = None): |
| 911 | """ |
| 912 | Process a file containing a list of URLs, fetch each URL, and load the content into the database. |
| 913 | Each line in the file should be a single URL pointing to RSS/XML or JSON content. |
| 914 | The file itself can be a local file or a URL. |
| 915 | |
| 916 | Args: |
| 917 | file_path: Path to the file containing URLs, one per line, or a URL to such a file |
| 918 | site: Site identifier |
| 919 | batch_size: Number of documents to process and upload in each batch |
| 920 | delete_existing: Whether to delete existing entries for this site before loading |
| 921 | force_recompute: Whether to force recomputation of embeddings |
| 922 | database: Specific database endpoint to use (if None, uses preferred endpoint) |
| 923 | |
| 924 | Returns: |
| 925 | Total number of documents loaded |
| 926 | """ |
| 927 | # Use specified database or fall back to preferred endpoint |
| 928 | endpoint_name = database or CONFIG.write_endpoint |
| 929 | |
| 930 | # Check if the file_path is a URL |
| 931 | is_url_list_remote = await is_url(file_path) |
| 932 | temp_path = None |
| 933 | |
| 934 | try: |
| 935 | # If the file is a URL, fetch it first |
| 936 | if is_url_list_remote: |
| 937 | print(f"URL list file is a remote URL. Fetching: {file_path}") |
| 938 | content, _ = await fetch_url(file_path) |
| 939 | |
| 940 | # Save to temporary file |
| 941 | with tempfile.NamedTemporaryFile(suffix='.txt', delete=False, mode='w', encoding='utf-8') as temp: |
| 942 | temp.write(content) |
| 943 | temp_path = temp.name |
| 944 | |
| 945 | print(f"Saved URL list to temporary file: {temp_path}") |
| 946 | # Use the temp path for further processing |
| 947 | file_path = temp_path |
| 948 | |
| 949 | # Read all lines from the file, each line should be a URL |
| 950 | lines = await read_file_lines(file_path) |
| 951 | total_urls = len(lines) |
| 952 | |
| 953 | # Filter out empty lines and comments |
| 954 | urls = [line.strip() for line in lines if line.strip() and not line.strip().startswith('#')] |
| 955 | total_valid_urls = len(urls) |
| 956 | |
| 957 | print(f"Found {total_valid_urls} valid URLs out of {total_urls} lines in the file") |
| 958 | |
| 959 | # Delete existing entries for this site if requested (do this only once) |
| 960 | if delete_existing: |
| 961 | await delete_site_from_database(site, endpoint_name) |
| 962 | |
| 963 | # Get client directly from the factory function, using query_params for development mode override |
| 964 | query_params = {"db": database} if database else None |
| 965 | get_vector_db_client(query_params=query_params) |
| 966 | |
| 967 | # Process each URL |
no test coverage detected