Skip to content

Commit

Permalink
Version 0.1.0
Browse files Browse the repository at this point in the history
Functioning Package, but not optimal solution to authentication
  • Loading branch information
jschibberges committed Nov 4, 2022
1 parent c76210d commit ca0bbc8
Show file tree
Hide file tree
Showing 7 changed files with 499 additions and 72 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# Backups
*.bak
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Julian Schibberges

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include README.md
include LICENSE

global-exclude *.bak
104 changes: 86 additions & 18 deletions bundestag_api/bta_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

date_gen_apikey = "31.05.2023"
date_expiry = datetime.strptime(date_gen_apikey, "%d.%m.%Y")
today = datetime.now()

base_url = "https://search.dip.bundestag.de/api/v1/"
resourcetypes = ["aktivitaet", "drucksache", "drucksache-text", "person",
Expand All @@ -16,8 +15,8 @@
institutions = ["BT", "BR", "BV", "EK"]


def btapi_query(resource,
apikey,
def btapi_query(apikey,
resource,
rformat="json",
num=100,
fid=None,
Expand All @@ -27,6 +26,45 @@ def btapi_query(resource,
documentID=None,
plenaryprotocolID=None,
processID=None):
"""A general search function for the official Bundestag API
Parameters
----------
apikey: str/BTA_Connection
Either an API key in string format or a BTA_Connection object
resource: str
The resource type to be queried. options are aktivitaet,
drucksache, drucksache-text, person, plenarprotokoll,
plenarprotokoll-text, vorgang or vorgangsposition
rformat: str, optional
Return format of the data. Defaults to json. XML not implemented
yet. Other option is "object" which will return results as class
objects
num: int, optional
Number of maximal results to be returned. Defaults to 100
fid: int/list, optional
ID of an entity. Can be a list to retrieve more than one entity
datestart: str, optional
Date after which entities should be retrieved. Format
is "YYYY-MM-DD"
dateend: str, optional
Date before which entities should be retrieved. Format
is "YYYY-MM-DD"
institution: str, optional
Filter results by institution BT, BR, BV or EK
documentID: int, optional
Entity ID of a document. Can be used to select activities,
procedures and procedure positions that are connected to the
document
plenaryprotocolID: int, optional
Entity ID of a plenary protocol. Can be used to select activities,
procedures and procedure positions that are connected to the
protocol
processID: int, optional
Entity ID of a process. Can be used to select procedure positions
that are connected to the process
"""

if hasattr(apikey, "apikey") is True:
apikey = apikey.apikey
elif isinstance(apikey, str) and len(apikey) == 42:
Expand Down Expand Up @@ -79,6 +117,8 @@ def btapi_query(resource,
if rformat == "object":
rformat = "json"
return_object = True
if isinstance(fid, list) is True:
fid = '&f.id='.join(map(str, fid))
payload = {"apikey": apikey,
"format": rformat,
"f.id": fid,
Expand All @@ -93,7 +133,7 @@ def btapi_query(resource,
prs = True
while prs is True:
r = requests.get(r_url, params=payload)
print(r.url)
# print(r.url)
if r.status_code == requests.codes.ok:
content = r.json()
if content["numFound"] == 0:
Expand Down Expand Up @@ -138,9 +178,10 @@ def btapi_query(resource,


class BTA_Connection:
'''This class handles the API authentication'''
"""This class handles the API authentication"""

def __init__(self, apikey=None):
today = datetime.now()
if apikey is None and date_expiry.date() < today.date():
print("You need to supply your own API key.")
elif apikey is None and date_expiry.date() > today.date():
Expand All @@ -153,14 +194,14 @@ def __init__(self, apikey=None):
self.apikey = apikey

def __str__(self):
return str(self.apikey)
return "API key: "+str(self.apikey)

def __repr__(self):
return str(self.apikey)
return "API key: "+str(self.apikey)


class Person:
"This class represents a German parliamentarian"
"""This class represents a German parliamentarian"""

def __init__(self, dictionary):
self.btid = dictionary["id"]
Expand Down Expand Up @@ -277,7 +318,7 @@ def updateByID(self, apikey=None):


class Role:
"This class presents a role in the German parliamentary system."
"""This class presents a role in the German parliamentary system."""

def __init__(self, dictionary):
self.function = dictionary["funktion"]
Expand Down Expand Up @@ -322,7 +363,7 @@ def __str__(self):
return f'Person: {self.firstname}{" " if self.nameaddendum!=None else ""}{self.nameaddendum if self.nameaddendum!=None else ""} {self.lastname} {"(" if self.faction!= None else ""}{self.faction if self.faction!= None else ""}{")" if self.faction!= None else ""} - {self.function}'

def __repr__(self):
pass
return f'Person: {self.firstname}{" " if self.nameaddendum!=None else ""}{self.nameaddendum if self.nameaddendum!=None else ""} {self.lastname} {"(" if self.faction!= None else ""}{self.faction if self.faction!= None else ""}{")" if self.faction!= None else ""} - {self.function}'

def returnrole(self):
return(
Expand All @@ -331,7 +372,7 @@ def returnrole(self):


class Drucksache:
"This class represents a document of the German federal parliaments"
"""This class represents a document of the German federal parliaments"""

def __init__(self, dictionary):
self.btid = dictionary["id"]
Expand Down Expand Up @@ -392,9 +433,12 @@ def __init__(self, dictionary):
self.docnumber = None
if "autoren_anzeige" in dictionary:
auan = []
auanid = []
for a in dictionary["autoren_anzeige"]:
auan.append(Person(a))
auan.append(a["titel"])
auanid.append(a["id"])
self.author = auan
self.authorid = auanid
self.authordisplay = dictionary["autoren_anzeige"]
else:
self.author = None
Expand All @@ -408,21 +452,45 @@ def __str__(self):
return f'{self.instance}: ({self.btid}) {self.doctype} - {self.title} - {self.date}'

def __repr__(self):
pass
return f'{self.instance}: ({self.btid}) {self.doctype} - {self.title} - {self.date}'

def get_authors(self):
def get_authors(self, apikey):
pass


class Aktivitaet:
"This class represents an activity in the German federal parliaments"
"""This class represents an activity in the German federal parliaments"""

def __init__(self, dictionary):
self.btid = dictionary["id"]
self.activitytype = dictionary["aktivitaetsart"]
self.date = dictionary["datum"]
self.title = dictionary["titel"]
self.type = dictionary["typ"]
self.doctype = dictionary["dokumentart"]
self.parlsession = dictionary["wahlperiode"]
self.numprocedure = dictionary["vorgangsbezug_anzahl"]
self.procedure_reference = dictionary["vorgangsbezug"][0]["id"]
self.document_reference = dictionary["fundstelle"][0]["id"]

def __str__(self):
return f'{self.instance}: ({self.btid}) {self.activitytype} - {self.title} - {self.date}'

def get_procedure(self, apikey):
data = btapi_query(apikey=apikey, resource="vorgang",
fid=self.procedure_reference)
data = Vorgang(data[0])
return data

def get_document(self, apikey):
data = btapi_query(
apikey=apikey, resource="drucksache-text", fid=self.document_reference)
data = Drucksache(data[0])
return data


class Vorgang:
"This class represents a legislative process in of the German federal parliaments"
"""This class represents a legislative process in of the German federal parliaments"""

def __init__(self, dictionary):
self.btid = dictionary["id"]
Expand Down Expand Up @@ -523,7 +591,7 @@ def __repr__(self):


class Vorgangsposition:
"This class represents a step in a legislative process in the German federal parliaments"
"""This class represents a step in a legislative process in the German federal parliaments"""

def __init__(self, dictionary):
self.btid = dictionary["id"]
Expand Down Expand Up @@ -595,7 +663,7 @@ def __repr__(self):


class Plenarprotokoll:
"This class represents a plenary protocol of the German federal parliaments"
"""This class represents a plenary protocol of the German federal parliaments"""

def __init__(self, dictionary):
self.btid = dictionary["id"]
Expand Down
Loading

0 comments on commit ca0bbc8

Please sign in to comment.