Advertisement
max2201111

app.py

Jun 11th, 2024
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.53 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', '')
  47.                             samples = [f"{samp['samp2s']} -> {samp['samp2t']}" for samp in sens.get('samp2', [])]
  48.                             trans = [''.join(t) for t in sens.get('trans', []) if t]
  49.                             coll2 = [f"{c['coll2s']} -> {c['coll2t']}" for c in sens.get('coll2', [])]
  50.                             sens_data.append({'desc': desc, 'note': note, 'morf': morf, 'samples': samples, 'trans': trans, 'coll2': coll2})
  51.                         term_data.append(sens_data)
  52.  
  53.                     if direction == 'en_cz':
  54.                         session['en_cz_translations'] = [(term, term_data)] + session['en_cz_translations']
  55.                     else:
  56.                         session['cz_en_translations'] = [(term, term_data)] + session['cz_en_translations']
  57.  
  58.                 except json.JSONDecodeError as e:
  59.                     error = f'Error decoding JSON: {str(e)}'
  60.                     return render_template('index.html', error=error, en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
  61.             else:
  62.                 error = 'Error: Unable to find the JSON data script tag.'
  63.                 return render_template('index.html', error=error, en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
  64.         else:
  65.             error = f'Error: Unable to fetch data for term "{term}".'
  66.             return render_template('index.html', error=error, en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
  67.  
  68.     return render_template('index.html', en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
  69.  
  70. if __name__ == '__main__':
  71.     app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement