Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Newcastle City Council #9

Merged
merged 1 commit into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions outputs/NewcastleCityCouncil.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"bins": [
{
"BinType": "Domestic Waste",
"NextCollectionDate": "2020-05-05"
},
{
"BinType": "Recycling",
"NextCollectionDate": "2020-04-28"
},
{
"BinType": "Garden Waste",
"NextCollectionDate": "2020-04-24"
}
]
}
46 changes: 46 additions & 0 deletions scripts/NewcastleCityCouncil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
from urllib.request import Request, urlopen
import json
from bs4 import BeautifulSoup
from datetime import datetime

user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
headers = {'User-Agent': user_agent}

#Replace URL
req = Request('https://community.newcastle.gov.uk/my-neighbourhood/ajax/getBinsNew.php?uprn=XXXXXXXXXXXX')
req.add_header('User-Agent', user_agent)

fp = urlopen(req).read()
page = fp.decode("utf8")

soup = BeautifulSoup(page, features="html.parser")
soup.prettify()

#Form a JSON wrapper
data = {"bins":[]}

#Loops the strong elements
for element in soup.find_all("strong"):
#Domestic Waste is formatted differenty to other bins
if "Green Bin (Domestic Waste) details:" in str(element):
collectionInfo = element.next_sibling.find('br').next_element
else:
collectionInfo = element.next_sibling.next_sibling.next_sibling.next_sibling

binType = str(element)[str(element).find("(")+1:str(element).find(")")]
collectionDate = str(datetime.strptime(str(collectionInfo).replace('Next collection : ',''), '%d-%b-%Y').date())

dict_data = {
"BinType": binType,
"NextCollectionDate": collectionDate
}

#Add data to the main JSON Wrapper
data["bins"].append(dict_data)


##Make the JSON
json_data = json.dumps(data,sort_keys=True, indent=4)

print(json_data)