-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add MVP functionality (basically one endpoint)
- Loading branch information
1 parent
55ec897
commit ef7382f
Showing
10 changed files
with
359 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,9 @@ name = "pypi" | |
|
||
[packages] | ||
|
||
hammock = "*" | ||
marshmallow = "*" | ||
pytest = "*" | ||
|
||
|
||
[dev-packages] | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from politifact.core import Politifact | ||
from politifact.models import Statement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.