Last active
February 7, 2019 05:53
-
-
Save minikomi/0ec26782e367e0f6993af175b8c1dc64 to your computer and use it in GitHub Desktop.
art parties new python app
This file contains hidden or 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
#!/usr/bin/env python3 | |
import requests | |
import pprint | |
import itertools | |
from xml.etree import ElementTree | |
from flask import Flask, render_template, Markup, escape | |
from jinja2 import evalcontextfilter | |
import re | |
import time | |
import datetime | |
url = "http://www.tokyoartbeat.com/events/xml.php?lang=en&contentType={}" | |
areas = [ | |
"Shinjuku", | |
"Shibuya", | |
"GinzaMarunouchi", | |
"UenoYanaka", | |
"KyobashiNihonbashi", | |
"OmotesandoAoyama", | |
"RoppongiNogizaka", | |
"EbisuDaikanyama", | |
"Chiyoda" | |
] | |
def get_area(area): | |
response = requests.get(url.format(area)) | |
tree = ElementTree.fromstring(response.content) | |
parties = [] | |
today = datetime.date.today() | |
today_tuple = (today.year, today.month, today.day) | |
for e in tree.findall(".//Event"): | |
event_parties = e.findall("Party") | |
if event_parties: | |
for p in event_parties: | |
date = p.get("date") | |
date_tuple = tuple(map(int, date.split("-"))) | |
if date_tuple >= today_tuple: | |
v = e.find("Venue") | |
venue = { | |
"name": v.find("Name").text, | |
"address": v.find("Address").text, | |
"lat": e.find("Latitude").text, | |
"long": e.find("Longitude").text | |
} | |
images = [i.get("src") for i in e.findall("Image")] | |
partydata = { | |
"href": e.get("href"), | |
"description": e.find("Description").text, | |
"date": date_tuple, | |
"start": p.get("start"), | |
"type": p.text, | |
"end": p.get("end"), | |
"name": e.find("Name").text, | |
"images": images, | |
"venue": venue | |
} | |
parties.append(partydata) | |
sorted_parties = sorted(parties, key=lambda x: x["date"]) | |
grouped = [(k, list(v)) for k,v | |
in itertools.groupby(sorted_parties, lambda x: x["date"])] | |
return grouped | |
app = Flask(__name__) | |
_paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}') | |
@app.template_filter() | |
@evalcontextfilter | |
def nl2br(eval_ctx, value): | |
result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', '<br>\n') \ | |
for p in _paragraph_re.split(escape(value))) | |
if eval_ctx.autoescape: | |
result = Markup(result) | |
return result | |
last_check = None | |
partydata = {} | |
def create_app(): | |
app = Flask(__name__) | |
_paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}') | |
@app.template_filter() | |
@evalcontextfilter | |
def nl2br(eval_ctx, value): | |
result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', '<br>\n') \ | |
for p in _paragraph_re.split(escape(value))) | |
if eval_ctx.autoescape: | |
result = Markup(result) | |
return result | |
@app.route("/") | |
def home(): | |
global last_check | |
if last_check is None or time.time() - last_check > 60 * 60: | |
last_check = time.time() | |
for area in areas: | |
partydata[area] = get_area(area) | |
return render_template("index.jinja2", partydata=partydata) | |
return app | |
if __name__ == '__main__': | |
app = create_app() | |
app.run() |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Art Parties</title> | |
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic"> | |
<link rel="stylesheet" href="//cdn.rawgit.com/necolas/normalize.css/master/normalize.css"> | |
<link rel="stylesheet" href="//cdn.rawgit.com/milligram/milligram/master/dist/milligram.min.css"> | |
<style> | |
* { | |
margin: 0; | |
padding: 0; | |
} | |
body.container { | |
margin: 0 auto; | |
max-width: 800px; | |
} | |
@media (max-width: 40rem) { | |
.row .column[class*=" column-"] { | |
margin-left: 0; | |
max-width: 100%; | |
} | |
} | |
</style> | |
</head> | |
<body class="container"> | |
<h1>Art Parties</h1> | |
{% for area, dates in partydata.items() %} | |
{% if dates %} | |
<div class="container"> | |
<h2>{{area}}</h2> | |
<hr> | |
{% for date, ps in dates %} | |
<div class="row"> | |
<div class="column column-25"> | |
<h4>{{date[0]}} / {{date[1]}} / {{date[2]}}</h4> | |
</div> | |
<div class="column column-75"> | |
{% for p in ps %} | |
<h5> | |
<strong>{{p.venue.name}}</strong> | |
<br> | |
<img src="{{p.images[2]}}"> | |
</h5> | |
<p> | |
<br> | |
<a href="{{p.href}}" target="blank">{{p.name}}</a> | |
<br> | |
<small><strong>{{p.start}}</strong> ~ <strong>{{p.end}}</strong></small> | |
<br> | |
<small><a href="https://maps.google.com/?q={{p.venue.lat}},{{p.venue.long}}" target="_blank">{{p.venue.address}}</a></small> | |
</p> | |
{% if p.description %} | |
<div class="justify"> | |
{{p.description|nl2br}} | |
</div> | |
{% endif %} | |
<hr/> | |
{% endfor %} | |
</div> | |
</div> | |
{% endfor %} | |
</div> | |
{% endif %} | |
{% endfor%} | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment