Skip to content

Commit

Permalink
added bills dataservice (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
davi17g authored and OriHoch committed May 7, 2017
1 parent 1747a8d commit 6c90631
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 4 deletions.
18 changes: 14 additions & 4 deletions knesset_data/dataservice/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ def _get_request_exception(cls, original_exception):
return KnessetDataServiceRequestException(cls._get_service_name(), cls._get_method_name(), original_exception)

@classmethod
def _get_soup(cls, url, params=None, proxies=None):
params = {} if params == None else params
timeout = params.pop('__timeout__', cls.DEFAULT_REQUEST_TIMEOUT_SECONDS)
def _get_response_content(cls, url, params, timeout, proxies):
try:
proxies = proxies if proxies else {}
response = requests.get(url, params=params, timeout=timeout, proxies=proxies)
Expand All @@ -139,7 +137,13 @@ def _get_soup(cls, url, params=None, proxies=None):
if response.status_code != 200:
raise Exception("invalid response status code: {}".format(response.status_code))
else:
return BeautifulSoup(response.content, 'html.parser')
return response.content

@classmethod
def _get_soup(cls, url, params=None, proxies=None):
params = {} if params == None else params
timeout = params.pop('__timeout__', cls.DEFAULT_REQUEST_TIMEOUT_SECONDS)
return BeautifulSoup(cls._get_response_content(url, params, timeout, proxies), 'html.parser')

@classmethod
def _handle_prop(cls, prop_type, prop_null, prop):
Expand Down Expand Up @@ -358,3 +362,9 @@ def get(cls, params, proxies=None):
soup = cls._get_soup(cls._get_url(params), proxies=proxies)
return (cls(cls._parse_element(element), proxies=proxies)
for element in soup.find_all('element'))






39 changes: 39 additions & 0 deletions knesset_data/dataservice/bills.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from base import (
BaseKnessetDataServiceCollectionObject, BaseKnessetDataServiceFunctionObject,
KnessetDataServiceSimpleField, KnessetDataServiceLambdaField
)
import constants


class Bill(BaseKnessetDataServiceCollectionObject):
SERVICE_NAME = "api"
METHOD_NAME = "KNS_Bill"
ORDERED_FIELDS = [
("id", KnessetDataServiceSimpleField('BillID', "integer", "the primary key")),
("kns_num", KnessetDataServiceSimpleField('KnessetNum', "integer", "kneset number")),
("name", KnessetDataServiceSimpleField('Name', "string", "bill heb name")),
("type_id", KnessetDataServiceSimpleField('SubTypeID', "integer", "type id of the bill")),
("type_description",
KnessetDataServiceSimpleField('SubTypeDesc', "string", "type description of the bill")),
("private_num", KnessetDataServiceSimpleField('PrivateNumber', "integer",)),
("committee_id", KnessetDataServiceSimpleField('CommitteeID', "integer",)),
("status_id", KnessetDataServiceSimpleField('StatusID', "integer",)),
("num", KnessetDataServiceSimpleField('Number', "Integer",)),
("postponent_reason_id", KnessetDataServiceSimpleField('PostponementReasonID',
"Integer",)),
("postponent_reason_desc", KnessetDataServiceSimpleField('PostponementReasonDesc', "string",)),
("public_date", KnessetDataServiceSimpleField('PublicationDate', "datetime",)),
("magazine_num", KnessetDataServiceSimpleField('MagazineNumber', "integer",)),
("page_num", KnessetDataServiceSimpleField('PageNumber', "integer")),
("is_continuation", KnessetDataServiceSimpleField('IsContinuationBill', "bool",)),
("sum_law", KnessetDataServiceSimpleField('SummaryLaw', "string", )),
("public_series_id", KnessetDataServiceSimpleField('PublicationSeriesID', "integer",)),
("public_series_desc", KnessetDataServiceSimpleField('PublicationSeriesID', "string",)),
("public_series_first_call", KnessetDataServiceSimpleField('PublicationSeriesFirstCall', "string", )),
("last_update", KnessetDataServiceSimpleField('LastUpdatedDate', "datetime", ))


]



Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions knesset_data/dataservice/tests/test_bills.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from unittest import TestCase
from knesset_data.dataservice.bills import Bill
from itertools import islice
from collections import OrderedDict
import datetime
import os

class MockBill(Bill):

@classmethod
def _get_response_content(cls, url, params, timeout, proxies):
if url == "http://knesset.gov.il/Odata/ParliamentInfo.svc//KNS_Bill":
filename = os.path.join(os.path.dirname(__file__), "ParliamentInfo.svc_KNS_Bill.xml")
if os.environ.get("TEST_BILLS_DOWNLOAD_DATA", "") == "yes":
content = super(MockBill, cls)._get_response_content(url, params, timeout, proxies)
with open(filename, "w") as f:
f.write(content)
else:
with open(filename) as f:
content = f.read()
else:
raise Exception("invalid url: {}".format(url))
return content

class BillsTestCase(TestCase):
maxDiff = None

def _listify_bills(self, bills):
res = []
for bill in bills:
res.append(bill.all_field_values())
return res

def test(self):
bills = self._listify_bills(list(islice(MockBill.get_all(), 2)))
self.assertEqual(bills, [
OrderedDict([
('id', 5), ('kns_num', 1), ('name',
u'\u05d7\u05d5\u05e7 \u05e9\u05db\u05e8 \u05d7\u05d1\u05e8\u05d9 \u05d4\u05db\u05e0\u05e1\u05ea, \u05d4\u05ea\u05e9"\u05d8-1949'),
('type_id', 53), ('type_description', u'\u05de\u05de\u05e9\u05dc\u05ea\u05d9\u05ea'),
('private_num', None), ('committee_id', 377), ('status_id', 118), ('num', None),
('postponent_reason_id', None), ('postponent_reason_desc', None),
('public_date', datetime.datetime(1949, 6, 7, 0, 0)), ('magazine_num', 10), ('page_num', 41),
('is_continuation', None), ('sum_law', None), ('public_series_id', 6071),
('public_series_desc', 6071), ('public_series_first_call', None),
('last_update', datetime.datetime(2016, 3, 8, 11, 1, 1))]),
OrderedDict([('id', 20), ('kns_num', 7), ('name',
u'\u05d7\u05d5\u05e7 \u05de\u05e7\u05e6\u05d5\u05e2\u05d5\u05ea \u05e8\u05e4\u05d5\u05d0\u05d9\u05d9\u05dd (\u05d0\u05d2\u05e8\u05d5\u05ea), \u05d4\u05ea\u05e9\u05dc"\u05d0-1971'),
('type_id', 53), ('type_description', u'\u05de\u05de\u05e9\u05dc\u05ea\u05d9\u05ea'),
('private_num', None), ('committee_id', 280), ('status_id', 118), ('num', 887),
('postponent_reason_id', None), ('postponent_reason_desc', None),
('public_date', datetime.datetime(1971, 4, 7, 0, 0)), ('magazine_num', 618), ('page_num', 68),
('is_continuation', None), ('sum_law', None), ('public_series_id', 6071),
('public_series_desc', 6071), ('public_series_first_call', None),
('last_update', datetime.datetime(2016, 3, 8, 11, 0, 56))])
])

0 comments on commit 6c90631

Please sign in to comment.