<jupyter_start><jupyter_text>Download and format datasets of news articles> Takes a loader as an argument  Work in progress  Datasets to get:-CoverageTrends-GDELT-Are there others that are archived?<jupyter_code>import pandas as pd
import gzip
import urllib.request
import os<jupyter_output><empty_output><jupyter_text>Download CoverageTrends articlesCoverageTrends is a git repo I put together a few weeks ago to start playing with this. It creates daily CSVs for different publishers, updated every 30 minutes with front page coverage. The articles are in CSV form at<jupyter_code>from github import Github<jupyter_output><empty_output><jupyter_text>Download GDELT Headlines; not sure there's a memory efficient way to do this. Possibly doing some sort of integration with BigQuery?from https://blog.gdeltproject.org/announcing-gdelt-global-frontpage-graph-gfg/<jupyter_code>## GDELT files are pretty large (250mb/hr zipped, 1.5gb unzipped)
#export 
def downloadGDELT(filepath, year:"YYYY", month:"MM", day: "DD", hour:"HH"):
    "download and extract .gz, borrowed from https://stackoverflow.com/questions/3548495/download-extract-and-read-a-gzip-file-in-python"
    
    """
    WARNING: This results in a 1.5gb file per hour, or 30gb/day. Not actually sure best way to go about making this useful.
    """
    
    url = "http://data.gdeltproject.org/gdeltv3/gfg/alpha/{}{}{}{}0000.LINKS.TXT.gz".format(year, month, day, hour)
    os.makedirs(filepath, exist_ok=True)
    out_file = "{}/{}".format(filepath, url.split("/")[-1][:-3])

    try:
        with urllib.request.urlopen(url) as response:
            with gzip.GzipFile(fileobj=response) as uncompressed:
                file_content = uncompressed.read()

            with open(out_file, 'wb') as f:
                f.write(file_content)
                return file_content


    except Exception as e:
        print(e)
        return -1

tmp = downloadGDELT("test", "2020", "06", "08", "00")
os.listdir("test")
test = pd.read_csv("test/20200608000000.LINKS.TXT", sep='\\t', header=None)
test.fillna("", inplace=True)
available = test[0].unique()
[x for x in available if x.find("nytimes") > 0]
[x for x in available if x.find("wsj") > 0]
[x for x in available if x.find("cnn") > 0]
[x for x in available if x.find("dailybeast") > 0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1207563386334336, 'fraction_numerical': 0.023635582294800173, 'mean_word_length': 4.006451612903226, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 18, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '5324', 'license': 'Apache-2.0', 'n_tokens_mistral': 814, 'n_tokens_neox': 756, 'n_words': 226, 'path': '04_download.ipynb', 'repo_name': 'brockmanmatt/NewsTrends', 'size': '2327'}