Skip to content

Commit

Permalink
Script for copying over Possible Articles
Browse files Browse the repository at this point in the history
  • Loading branch information
jornado committed May 10, 2020
1 parent 4ccc12f commit 66c977a
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 12 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# roni
# Roni
A bunch of scripts for automating creation of the daily COVID-19 news Rona Report

# Usage

* Save any desired articles to Possible Articles with:
`python save_article.py "New York Times" http://nyt.com/thearticle`

* Copy all Possible Articles marked "Use" to Articles and delete possibles:
`python copy_possible_articles.py`

* Fetch daily stats from The Atlantic:
`python parser.py`

* Print out a test report to report.html:
`python report.py`

* Send out the report emails:
`python report.py 1`
3 changes: 2 additions & 1 deletion airtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ def get_url(self, table):
"""Full URL to an AirTable table."""
return "{}/{}".format(self.config["host"], table)

def get_content(self, table, sort="Date", offset=0):
def get_content(self, table, sort="Date", offset=0, extra_params={}):
"""Pull down a list of items from an AirTable table."""
url = self.get_url(table)
params = self.get_params(sort, offset)
params.update(extra_params)

res = requests.get(url, params=params)
content = res.json()
Expand Down
78 changes: 78 additions & 0 deletions copy_possible_articles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
This file is for copying over articles from Possible Articles to Articles
"""
from __future__ import absolute_import

import requests

from airtable import Airtable
from models.article import Article


class Copy(object):
def __init__(self):
self.airtable = Airtable()

def format_payload(self, thing):
payload = {}
payload["records"] = [{"fields": thing.format()}]
payload["typecast"] = True
return payload

def get_possible_articles(self):
params = {"filterByFormula": "AND(Use = 1)"}
possible, _ = self.airtable.get_content(
"Possible Articles", "Date", 0, params)

return possible

def headers(self, thing):
return {
"Authorization": "Bearer %s" % thing.config["key"],
"Content-type": "application/json; charset=utf-8"
}

def delete_possible_article(self, article):
res = requests.delete(
self.possibles[0].alt_api_url + "/" + article.id,
headers=self.headers(article)
)
print res.json()

def copy_possible_article(self, thing):
payload = self.format_payload(thing)
res = requests.post(
thing.api_url,
json=payload,
headers=self.headers(thing),
)
print res.json()


class CopyPossibleArticle(Copy):
def __init__(self):
super(CopyPossibleArticle, self).__init__()
self.possibles = []

def go(self):
self.get_possibles()

print "Copying"
for p in self.possibles:
self.copy_possible_article(p)

print "Deleting"
for p in self.possibles:
self.delete_possible_article(p)

def get_possibles(self):
possible = self.get_possible_articles()
for p in possible["records"]:
params = p["fields"]
params["id"] = p["id"]
self.possibles.append(Article(params))


if __name__ == "__main__":
s = CopyPossibleArticle()
s.go()
11 changes: 8 additions & 3 deletions models/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ def __init__(self, params):
self._title = None
self._notes = None
self.url = None
self.data = self.today
self.date = self.today
self.source_id = None
self.source = None
self.min_to_read = None

print "params, ", params
if "id" in params:
self.id = params["id"]
if "Title" in params:
self._title = self.encode(self.parse_title(params["Title"]))
self._title = params["Title"]
Expand Down Expand Up @@ -102,9 +105,9 @@ def format(self):

def __repr__(self):
"""Return text representation."""
repr = ""
repr = "ID: %s" % self.id
if self.title:
repr += "Title: %s" % self.title
repr += "\nTitle: %s" % self.title
if self.min_to_read:
repr += "\nMin to Read: %s" % self.min_to_read
if self.date:
Expand All @@ -118,4 +121,6 @@ def __repr__(self):
if self.min_to_read:
repr += "\nMin to Read: %s" % self.min_to_read

repr += "\n"

return repr
20 changes: 13 additions & 7 deletions save_article.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,23 @@ def __init__(self, prog):
def strip_source(self, text, source):
return text

def save(self, url, source_id, thedate):
def get_metadata(self, url):
u = Urler(url)
u.fetch()

title = (u.title if not u.is_apple
else self.strip_source(u.title, source_id))


return u

def save(self, url, source_id, thedate, title, notes=None, min_to_read=None):
self.article = Article(
{
"URL": url,
"Title": title,
"Notes": u.notes,
"Notes": notes,
"Source": [source_id],
"MinToRead": u.min_to_read,
"MinToRead": min_to_read,
"Date": thedate,
}
)
Expand All @@ -79,15 +82,14 @@ def save(self, url, source_id, thedate):
print url
print ""

# TODO: take Source name out of apple news title
self.post(self.article)


# TODO: new script to move all Possible Articles marked "Use" to Articles
if __name__ == "__main__":
s = SaveArticle(sys.argv[0])
s.check_args(len(sys.argv) == 3)

url = sys.argv[2]
thedate = date.today().isoformat()
source = Source()
# uncomment this to update sources
Expand All @@ -97,8 +99,12 @@ def save(self, url, source_id, thedate):
print "No source found for %s" % sys.argv[1]
exit

metadata = s.get_metadata(url)
s.save(
url=sys.argv[2],
url=url,
source_id=source_id,
thedate=thedate,
title=metadata.title,
notes=metadata.notes,
min_to_read=metadata.min_to_read,
)
1 change: 1 addition & 0 deletions urler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __init__(self, url):
self._notes = ""
self.url = url
self.min_to_read = 0
self.source_id = ""

def get(self):
res = requests.get(self.url)
Expand Down

0 comments on commit 66c977a

Please sign in to comment.