import collections, sys, time, urllib.parse, urllib.request
import xml.etree.ElementTree as ET

QUERIES = ['"vertical farming"', '"indoor farming"', '"controlled environment agriculture"',
           '"vertical farm" funding', '"vertical farm"']
tally, domains, seen = collections.Counter(), {}, set()
for q in QUERIES:
    url = "https://news.google.com/rss/search?q=" + urllib.parse.quote_plus(q + " when:7d") + "&hl=en-US&gl=US&ceid=US:en"
    req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
    root = ET.fromstring(urllib.request.urlopen(req, timeout=20).read())
    for item in root.iter("item"):
        guid = item.findtext("guid")
        if guid in seen:
            continue
        seen.add(guid)
        src = item.find("source")
        tally[src.text] += 1
        domains[src.text] = urllib.parse.urlparse(src.get("url")).netloc
    time.sleep(2)
print(len(seen), "unique items,", len(tally), "outlets")
for name, n in tally.most_common():
    print(f"{n}\t{name}\t{domains[name]}")
