Skip to content

Commit

Permalink
add MVP functionality (basically one endpoint)
Browse files Browse the repository at this point in the history
  • Loading branch information
thundergolfer-two committed Apr 22, 2018
1 parent 55ec897 commit ef7382f
Show file tree
Hide file tree
Showing 10 changed files with 359 additions and 0 deletions.
1 change: 1 addition & 0 deletions .pytest_cache/v/cache/lastfailed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 4 additions & 0 deletions .pytest_cache/v/cache/nodeids
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"tests/integration/core_test.py::TestPolitifact::()::test_one_equals_one",
"tests/integration/core_test.py::TestPolitifact::()::test_statements_by_person"
]
3 changes: 3 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ name = "pypi"

[packages]

hammock = "*"
marshmallow = "*"
pytest = "*"


[dev-packages]
Expand Down
116 changes: 116 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions politifact/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from politifact.core import Politifact
from politifact.models import Statement
79 changes: 79 additions & 0 deletions politifact/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from hammock import Hammock

from politifact.models import statements_schema


class Person():
def __init__(self, politifact, firstname, lastname):
self.politifact = politifact
self.firstname = firstname
self.lastname = lastname

@property
def slug(self):
return self.firstname + '-' + self.lastname


class StatementsEndpoint():
def __init__(self, root):
self.root = root.statements(Politifact.EDITION)

def people(self, name, n=None):
first_name, second_name = name.lower().split()
person_slug = '{}-{}'.format(first_name, second_name)
resp = self.root.people(person_slug).json().GET()
stmts = resp.json()
# return [statement_schema.load(elem).data for elem in stmts]
return statements_schema.load(stmts).data

def rulings(self, ruling, n=None):
return self.root.rulings(ruling).json().GET()

def detail(self, id):
# TODO: check id is an int
return self.root.detail(id).json().GET()


class PromisesEndpoint():
def __init__(self, root):
self.root = root

def group(self, group_name, subject=None, n=None):
if not subject:
return getattr(self.root, group_name)().json().GET()
else:
return getattr(self.root, group_name)().subjects(subject).json().GET()


class UpdatesEndpoint():
def __init__(self, root):
self.root = root

def group(self, group_name, subject=None, n=None):
if not subject:
return getattr(self.root, group_name)().json().GET()
else:
return getattr(self.root, group_name)().subjects(subject).json().GET()


class Politifact():
API_ROOT = 'http://www.politifact.com/api'
EDITION = 'truth-o-meter'

def __init__(self):
self.root = Hammock(self.API_ROOT)

def statements(self):
return StatementsEndpoint(self.root)

def promises(self):
return PromisesEndpoint(self.API_ROOT)

def stories(self, n=None):
full_endpoint = self.root.stories().json()
if n:
full_endpoint += '?n={}'.format(n)
return full_endpoint.GET()

def updates(self):
return UpdatesEndpoint(self.API_ROOT)
65 changes: 65 additions & 0 deletions politifact/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from marshmallow import Schema, fields, post_load

from politifact.rulings import map_ruling_slug_to_ruling


class PartySchema(Schema):
party = fields.Str()
party_slug = fields.Str()


class PersonSchema(Schema):
first_name = fields.Str()
canonical_photo = fields.URL()
name_slug = fields.Str()
party = fields.Nested(PartySchema)


class StatementSchema(Schema):
statement_date = fields.Str()
statement = fields.Str()
speaker = fields.Nested(PersonSchema)
statement_context = fields.Str()
subjects = fields.List(fields.Dict())
ruling_date = fields.DateTime()
statement_type = fields.Dict()
ruling_link_text = fields.Str()
ruling_headline = fields.Str()
ruling = fields.Function(lambda obj: map_ruling_slug_to_ruling(obj))
statement_url = fields.Str()

@post_load
def make_statement(self, data):
return Statement(**data)


class Statement():
def __init__(
self,
statement_date,
statement,
speaker,
statement_context,
ruling_date,
statement_type,
ruling_link_text,
ruling_headline,
ruling,
statement_url,
subjects=None
):
self.statement_date = statement_date
self.statement = statement
self.speaker = speaker
self.statement_context = statement_context
self.ruling_date = ruling_date
self.statement_type = statement_type
self.ruling_link_text = ruling_link_text
self.ruling_headline = ruling_headline
self.ruling = ruling
self.statement_url = statement_url
self.subjects = subjects


statements_schema = StatementSchema(many=True)
statement_schema = StatementSchema()
77 changes: 77 additions & 0 deletions politifact/rulings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

class Ruling():
def __init__(self, name, slug, graphic):
self.name = name
self.slug = slug
self.graphic = graphic


PANTS_ON_FIRE = Ruling(
'Pants on Fire!',
'pants-fire',
'http://static.politifact.com.s3.amazonaws.com/rulings/tom-pantsonfire.gif'
)
TRUE = Ruling(
'True',
'true',
'http://static.politifact.com.s3.amazonaws.com/rulings/tom-pantsonfire.gif'
)
HALF_TRUE = Ruling(
'Half-True',
'half-true',
'http://static.politifact.com.s3.amazonaws.com/rulings/tom-halftrue.png'
)

FULL_FLOP = Ruling(
'Full Flop',
'full-flop',
'http://static.politifact.com.s3.amazonaws.com/rulings/fom-fullFlop.png'
)

MOSTLY_FALSE = Ruling(
'Mostly False',
'barely-true',
'http://static.politifact.com.s3.amazonaws.com/rulings/tom-mostlyfalse.png'
)

HALF_FLIP = Ruling(
'Half Flip',
'half-flip',
'http://static.politifact.com.s3.amazonaws.com/rulings/fom-halfFlip.png'
)

NO_FLIP = Ruling(
'No Flip',
'no-flip',
'http://static.politifact.com.s3.amazonaws.com/rulings/fom-noflip.png'
)

FALSE = Ruling(
'False',
'false',
'http://static.politifact.com.s3.amazonaws.com/rulings/tom-false.png'
)

MOSTLY_TRUE = Ruling(
'Mostly True',
'mostly-true',
'http://static.politifact.com.s3.amazonaws.com/rulings/tom-mostlytrue.png'
)

rulings = [
PANTS_ON_FIRE,
TRUE,
HALF_TRUE,
MOSTLY_TRUE,
FALSE,
NO_FLIP,
HALF_FLIP,
FULL_FLOP
]


def map_ruling_slug_to_ruling(slug):
for r in rulings:
if r.slug == slug:
return r
return None
12 changes: 12 additions & 0 deletions tests/integration/core_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from politifact import Politifact, Statement


class TestPolitifact():
poli = Politifact()

def test_one_equals_one(self):
assert 1 == 1

def test_statements_by_person(self):
stmts = self.poli.statements().people('barack obama')
assert all(isinstance(x, Statement) for x in stmts)
Empty file added tests/unit
Empty file.

0 comments on commit ef7382f

Please sign in to comment.