Advertisement
max2201111

app.py very good sloveso pridavne jmeno

Jun 11th, 2024
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.68 KB | Science | 0 0
  1. from flask import Flask, render_template, request, session
  2. import requests
  3. from bs4 import BeautifulSoup
  4. import json
  5.  
  6. app = Flask(__name__)
  7. app.secret_key = 'your_secret_key'
  8.  
  9. @app.route('/', methods=['GET', 'POST'])
  10. def index():
  11.     if 'en_cz_translations' not in session:
  12.         session['en_cz_translations'] = []
  13.     if 'cz_en_translations' not in session:
  14.         session['cz_en_translations'] = []
  15.  
  16.     if request.method == 'POST':
  17.         term = request.form.get('term')
  18.         direction = request.form.get('direction')
  19.  
  20.         if direction == 'en_cz':
  21.             url = f'https://slovnik.seznam.cz/preklad/anglicky_cesky/{term}'
  22.         elif direction == 'cz_en':
  23.             url = f'https://slovnik.seznam.cz/preklad/cesky_anglicky/{term}'
  24.         else:
  25.             return render_template('index.html', en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
  26.  
  27.         response = requests.get(url)
  28.  
  29.         if response.status_code == 200:
  30.             soup = BeautifulSoup(response.content, 'lxml')
  31.  
  32.             # Najdeme skript obsahující JSON data
  33.             script_tag = soup.find('script', {'id': '__NEXT_DATA__'})
  34.  
  35.             if script_tag:
  36.                 try:
  37.                     json_data = json.loads(script_tag.string)
  38.                     # Extrahujeme relevantní část JSON dat
  39.                     translations = json_data['props']['pageProps']['translations']
  40.                     term_data = []
  41.                     for translation in translations:
  42.                         sens_data = []
  43.                         for sens in translation['sens']:
  44.                             desc = sens.get('desc2', '')
  45.                             note = sens.get('note2', '')
  46.                             morf = sens.get('morf', 'Neznámý druh')
  47.                             if not morf:
  48.                                 morf = translation.get('morf', 'Neznámý druh')
  49.                             samples = [f"{samp['samp2s']} -> {samp['samp2t']}" for samp in sens.get('samp2', [])]
  50.                             trans = [''.join(t) for t in sens.get('trans', []) if t]
  51.                             coll2 = [f"{c['coll2s']} -> {c['coll2t']}" for c in sens.get('coll2', [])]
  52.                             sens_data.append({'morf': morf, 'desc': desc, 'note': note, 'samples': samples, 'trans': trans, 'coll2': coll2})
  53.                         term_data.append(sens_data)
  54.  
  55.                     if direction == 'en_cz':
  56.                         session['en_cz_translations'] = [(term, term_data)] + session['en_cz_translations']
  57.                     else:
  58.                         session['cz_en_translations'] = [(term, term_data)] + session['cz_en_translations']
  59.  
  60.                 except json.JSONDecodeError as e:
  61.                     error = f'Error decoding JSON: {str(e)}'
  62.                     return render_template('index.html', error=error, en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
  63.             else:
  64.                 error = 'Error: Unable to find the JSON data script tag.'
  65.                 return render_template('index.html', error=error, en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
  66.         else:
  67.             error = f'Error: Unable to fetch data for term "{term}".'
  68.             return render_template('index.html', error=error, en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
  69.  
  70.     return render_template('index.html', en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
  71.  
  72. if __name__ == '__main__':
  73.     app.run(debug=True)
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement