Upload fetch_cc_from_bucket.py with huggingface_hub
Browse files- fetch_cc_from_bucket.py +68 -0
fetch_cc_from_bucket.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.8"
|
| 3 |
+
# dependencies = [
|
| 4 |
+
# "warcio",
|
| 5 |
+
# "requests",
|
| 6 |
+
# ]
|
| 7 |
+
# ///
|
| 8 |
+
"""Fetch a page's content from a NFS-mounted Common Crawl Hugging Face Bucket."""
|
| 9 |
+
|
| 10 |
+
import argparse
|
| 11 |
+
import io
|
| 12 |
+
import json
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
from urllib.parse import quote_plus
|
| 15 |
+
|
| 16 |
+
import requests
|
| 17 |
+
from warcio.archiveiterator import ArchiveIterator
|
| 18 |
+
|
| 19 |
+
SERVER = "http://index.commoncrawl.org/"
|
| 20 |
+
INDEX_NAME = "CC-MAIN-2026-17"
|
| 21 |
+
USER_AGENT = "cc-hf-jobs/1.0 (Buckets Example)"
|
| 22 |
+
|
| 23 |
+
def search_cc_index(url):
|
| 24 |
+
encoded_url = quote_plus(url)
|
| 25 |
+
index_url = f"{SERVER}{INDEX_NAME}-index?url={encoded_url}&output=json"
|
| 26 |
+
response = requests.get(index_url, headers={"user-agent": USER_AGENT})
|
| 27 |
+
response.raise_for_status()
|
| 28 |
+
return [json.loads(line) for line in response.text.strip().split("\n")]
|
| 29 |
+
|
| 30 |
+
def fetch_page_from_bucket(records, bucket_root):
|
| 31 |
+
for record in records:
|
| 32 |
+
offset = int(record["offset"])
|
| 33 |
+
length = int(record["length"])
|
| 34 |
+
warc_path = bucket_root / record["filename"]
|
| 35 |
+
|
| 36 |
+
if not warc_path.exists():
|
| 37 |
+
print(f"Not in bucket: {warc_path}")
|
| 38 |
+
continue
|
| 39 |
+
|
| 40 |
+
with warc_path.open("rb") as f:
|
| 41 |
+
f.seek(offset)
|
| 42 |
+
chunk = f.read(length)
|
| 43 |
+
|
| 44 |
+
# The byte range covers exactly one gzipped WARC record, so we can
|
| 45 |
+
# hand it to ArchiveIterator as an in-memory stream.
|
| 46 |
+
for warc_record in ArchiveIterator(io.BytesIO(chunk)):
|
| 47 |
+
if warc_record.rec_type == "response":
|
| 48 |
+
return warc_record.content_stream().read()
|
| 49 |
+
|
| 50 |
+
print("No response record found")
|
| 51 |
+
return None
|
| 52 |
+
|
| 53 |
+
def main():
|
| 54 |
+
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
| 55 |
+
parser.add_argument("url", help="URL to look up in the CC index (e.g. huggingface.co/blog)")
|
| 56 |
+
parser.add_argument("bucket_path", type=Path, help="Path to the mounted Common Crawl bucket")
|
| 57 |
+
args = parser.parse_args()
|
| 58 |
+
|
| 59 |
+
records = search_cc_index(args.url)
|
| 60 |
+
print(f"Found {len(records)} record(s) for {args.url}")
|
| 61 |
+
|
| 62 |
+
content = fetch_page_from_bucket(records, args.bucket_path)
|
| 63 |
+
if content is not None:
|
| 64 |
+
print(f"Fetched {len(content)} bytes")
|
| 65 |
+
print(content[:500].decode("utf-8", errors="replace"))
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
main()
|