Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template, request, session
- import requests
- from bs4 import BeautifulSoup
- import json
- app = Flask(__name__)
- app.secret_key = 'your_secret_key'
- @app.route('/', methods=['GET', 'POST'])
- def index():
- if 'en_cz_translations' not in session:
- session['en_cz_translations'] = []
- if 'cz_en_translations' not in session:
- session['cz_en_translations'] = []
- if request.method == 'POST':
- term = request.form.get('term')
- direction = request.form.get('direction')
- if direction == 'en_cz':
- url = f'https://slovnik.seznam.cz/preklad/anglicky_cesky/{term}'
- elif direction == 'cz_en':
- url = f'https://slovnik.seznam.cz/preklad/cesky_anglicky/{term}'
- else:
- return render_template('index.html', en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
- response = requests.get(url)
- if response.status_code == 200:
- soup = BeautifulSoup(response.content, 'lxml')
- # Najdeme skript obsahující JSON data
- script_tag = soup.find('script', {'id': '__NEXT_DATA__'})
- if script_tag:
- try:
- json_data = json.loads(script_tag.string)
- # Extrahujeme relevantní část JSON dat
- translations = json_data['props']['pageProps']['translations']
- term_data = []
- for translation in translations:
- sens_data = []
- for sens in translation['sens']:
- desc = sens.get('desc2', '')
- note = sens.get('note2', '')
- morf = sens.get('morf', 'Neznámý druh')
- if not morf:
- morf = translation.get('morf', 'Neznámý druh')
- samples = [f"{samp['samp2s']} -> {samp['samp2t']}" for samp in sens.get('samp2', [])]
- trans = [''.join(t) for t in sens.get('trans', []) if t]
- coll2 = [f"{c['coll2s']} -> {c['coll2t']}" for c in sens.get('coll2', [])]
- sens_data.append({'morf': morf, 'desc': desc, 'note': note, 'samples': samples, 'trans': trans, 'coll2': coll2})
- term_data.append(sens_data)
- if direction == 'en_cz':
- session['en_cz_translations'] = [(term, term_data)] + session['en_cz_translations']
- else:
- session['cz_en_translations'] = [(term, term_data)] + session['cz_en_translations']
- except json.JSONDecodeError as e:
- error = f'Error decoding JSON: {str(e)}'
- return render_template('index.html', error=error, en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
- else:
- error = 'Error: Unable to find the JSON data script tag.'
- return render_template('index.html', error=error, en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
- else:
- error = f'Error: Unable to fetch data for term "{term}".'
- return render_template('index.html', error=error, en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
- return render_template('index.html', en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
- if __name__ == '__main__':
- app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement