Commit 410bd391 authored by PizZaKatZe's avatar PizZaKatZe

Shrink output down to single table

parent f53dc84d
......@@ -22,7 +22,7 @@ from json import dumps
from sys import argv
from sys import stdout
from jht_flyergen.engine import fetch_data, filter_session, generate_flyer, read_data
from jht_flyergen.engine import fetch_data, generate_flyer, process_sessions, read_data
TEST_URL = 'https://events.ccc.de/congress/2018/wiki/index.php/Special:Ask/cl:YzpVT9tKxFAM_KKaouzLQl5ERH1QYb8gp03t2Z5LSVJK_XrTdRcUcplhhjAZqmQyPGstkGKOhofWQZmQUoKRqWdR1LGuoEzSjYkCJ3w7fbxDl0hdq2IUEjf37RqneMOa1wuEXUeo4oeQtIM6DMqGLTSHR69XdfNQxecUez0eTRZuDk9e0Dw87_1Cu6dn7STOFmvBP_hm-pTasSr3bg2bD8_GOaQNq3xRid9X5f9RXUINZ-7s7sqNxHxbzIynnUCmWH6fhlnYbJslFsM9JizFg9DMF_YD' # pylint: disable=line-too-long
......@@ -59,7 +59,7 @@ def main():
sessions = fetch_data(args.url)
elif args.path:
sessions = read_data(args.path)
sessions = list(map(filter_session, sessions))
sessions = process_sessions(sessions)
if args.print:
end = int(args.print)
......
......@@ -40,31 +40,29 @@ def fetch_data(url: str) -> List[Dict]:
results: Dict = load(urlopen(url))['results']
return list(results.values())
def filter_session(session: Dict) -> Dict:
"""Re-organizes self-organized session data for easier access in templates.
def generate_flyer(sessions: List[Dict]) -> bytes:
"""Generates a PDF from self-organized session data.
"""
env = make_env(loader=FileSystemLoader(dirname(__file__)))
template = env.get_template('template.tex')
pdf = build_pdf(template.render(sessions=sessions))
return pdf.__bytes__()
Also filters out superfluous stuff, that appears to be always empty.
def process_sessions(sessions: List[Dict]) -> List[Dict]:
"""Filters out irrelevant sessions and sanitizes session data.
Args:
session: a dict containing information about a self-organized session
sessions: a list of raw session data as read from URL / file
Returns:
an uncluttered, organized version of the input dict containing only relevant data
Retruns:
a list of filtered, re-organized session data
"""
def filter_provider(provider: Dict) -> Dict:
filtered = {}
filtered['name'] = sub('Static:', '', provider['fulltext'])
filtered['url'] = sub('Static:', '', provider['fullurl'])
return filtered
def filter_start(start: Dict) -> str:
return datetime.fromtimestamp(int(start['timestamp'])).strftime('%Y-%m-%d %H:%M')
result = {}
result['title'] = sub('Session:', '', session['fulltext'])
result['description'] = ' '.join(session['printouts']['description'])
result['url'] = session['fullurl']
result['providers'] = list(map(filter_provider, session['printouts']['organized by']))
result['starttimes'] = list(map(filter_start, session['printouts']['Start']))
return result
def is_correct_date(start: Dict) -> bool:
return datetime.fromtimestamp(int(start['timestamp'])).day == 27
def relevant(session: Dict) -> bool:
return bool(list(filter(is_correct_date, session['printouts']['Start'])))
sessions = list(filter(relevant, sessions))
return list(map(transform_session, sessions))
def read_data(path: str) -> List[Dict]:
"""Reads self-organized session data from file.
......@@ -78,10 +76,26 @@ def read_data(path: str) -> List[Dict]:
with open(path) as data_file:
return load(data_file)
def generate_flyer(sessions: List[Dict]) -> bytes:
"""Generates a PDF from self-organized session data.
def transform_session(session: Dict) -> Dict:
"""Re-organizes self-organized session data for easier access in templates.
Also filters out superfluous stuff, that appears to be always empty.
Args:
session: a dict containing information about a self-organized session
Returns:
an uncluttered, organized version of the input dict containing only relevant data
"""
env = make_env(loader=FileSystemLoader(dirname(__file__)))
template = env.get_template('template.tex')
pdf = build_pdf(template.render(sessions=sessions))
return pdf.__bytes__()
def parse_date(start: Dict) -> datetime:
return datetime.fromtimestamp(int(start['timestamp']))
def process_provider(provider: Dict) -> str:
return sub('Assembly:', '', sub('Static:', '', provider['fulltext']))
result = {}
result['title'] = sub('Session:', '', session['fulltext'])
result['url'] = session['fullurl']
providers = list(map(process_provider, session['printouts']['organized by']))
result['providers'] = ', '.join(providers)
starts = list(map(parse_date, session['printouts']['Start']))
result['start'] = list(filter(lambda d: d.day == 27, starts))[0].strftime('%H:%M')
return result
......@@ -3,6 +3,7 @@
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{hyperref}
\usepackage{longtable}
\usepackage{qrcode}
\setlength{\parindent}{0pt}
......@@ -22,36 +23,16 @@
\begin{document}
\maketitle
%- for session in sessions
\section{\VAR{session.title | e}}
%- if session.description
\VAR{session.description | e}
\ \\[1em]
%- endif
%- if session.url
\qrcode{\VAR{session.url | e}}
\ \\[1em]
%- endif
%- if session.providers
Veranstalter:
\begin{itemize}
%- for provider in session.providers
\item \VAR{provider.name | e} %\qrcode{\VAR{provider.url | e}}
\begin{longtable}{|p{0.45\linewidth}|p{0.2\linewidth}|l|l|}
\hline
\textbf{Was?} & \textbf{Wo?} & \textbf{Wann?} & \textbf{URL}
\\ \hline \hline
%- for session in sessions
\VAR{session.title | e} & \VAR{session.providers | e} & \VAR{session.start | e}
& \qrcode{\VAR{session.url | e}}
\\ \hline
%- endfor
\end{itemize}
%- endif
\end{longtable}
%- if session.starttimes
Startzeit(en):
\begin{itemize}
%- for time in session.starttimes
\item \VAR{time | e}
%- endfor
\end{itemize}
%- endif
%- endfor
\end{document}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment