bradley6597 commited on
Commit
c8e9ef0
1 Parent(s): 73d6bf2

Better way of getting javascript data

Browse files

Found a better way of getting javascript data using raw content of webpage on requests.get()

Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
  import pandas as pd
3
- from requests_html import HTMLSession
 
 
4
 
5
  english_dict = pd.read_csv("dictionary.txt",
6
  header = None,
@@ -8,6 +10,8 @@ english_dict = pd.read_csv("dictionary.txt",
8
  names = ['word'])
9
  english_dict = english_dict.reset_index(drop = True)
10
  english_dict = english_dict.dropna()
 
 
11
  def spell_bee_solver(no_centre, centre):
12
  full_set = set(no_centre.lower() + centre.lower())
13
  spell_bee_solver = english_dict[english_dict['word'].str.contains(str(centre.lower()), regex = False)]
@@ -25,20 +29,18 @@ def spell_bee_solver(no_centre, centre):
25
  final_word_df = final_word_df.sort_values('word_length', ascending = False)
26
  return(final_word_df)
27
 
28
- def get_todays_answers():
29
- url = 'https://spellbee.org/'
30
- with HTMLSession() as session:
31
- page = session.get(url)
32
- valid_words = page.html.render(script = 'game.validWords')
33
-
34
  final_word_df = pd.DataFrame(valid_words, columns = ['word'])
35
  final_word_df['word_length'] = final_word_df['word'].str.len()
36
  final_word_df = final_word_df[final_word_df['word_length'] > 3]
37
  final_word_df = final_word_df.sort_values('word_length', ascending = False)
38
  return(final_word_df)
39
 
40
-
41
-
42
  with gr.Blocks() as app:
43
  with gr.Row():
44
  no_centre = gr.Textbox(label = 'Letters Outside of Centre')
@@ -49,6 +51,6 @@ with gr.Blocks() as app:
49
  with gr.Row():
50
  output_df = gr.DataFrame(headers = ['word', 'word_length'])
51
  solve_button.click(spell_bee_solver, inputs = [no_centre, centre], outputs = [output_df])
52
- get_today_answers.click(get_todays_answers, inputs = None, outputs = [output_df])
53
 
54
  app.launch(debug = True, share = False)
 
1
  import gradio as gr
2
  import pandas as pd
3
+ import requests
4
+ import re
5
+ import json
6
 
7
  english_dict = pd.read_csv("dictionary.txt",
8
  header = None,
 
10
  names = ['word'])
11
  english_dict = english_dict.reset_index(drop = True)
12
  english_dict = english_dict.dropna()
13
+
14
+ url = 'https://spellbee.org'
15
  def spell_bee_solver(no_centre, centre):
16
  full_set = set(no_centre.lower() + centre.lower())
17
  spell_bee_solver = english_dict[english_dict['word'].str.contains(str(centre.lower()), regex = False)]
 
29
  final_word_df = final_word_df.sort_values('word_length', ascending = False)
30
  return(final_word_df)
31
 
32
+ def get_spellbee_answers(x):
33
+ content = requests.get(url)._content
34
+ content = re.sub(".*window.game = ", "", str(content))
35
+ content = re.sub("(.*?)\\;.*", "\\1", content)
36
+ content = json.loads(content)
37
+ valid_words = content['data']['dictionary']
38
  final_word_df = pd.DataFrame(valid_words, columns = ['word'])
39
  final_word_df['word_length'] = final_word_df['word'].str.len()
40
  final_word_df = final_word_df[final_word_df['word_length'] > 3]
41
  final_word_df = final_word_df.sort_values('word_length', ascending = False)
42
  return(final_word_df)
43
 
 
 
44
  with gr.Blocks() as app:
45
  with gr.Row():
46
  no_centre = gr.Textbox(label = 'Letters Outside of Centre')
 
51
  with gr.Row():
52
  output_df = gr.DataFrame(headers = ['word', 'word_length'])
53
  solve_button.click(spell_bee_solver, inputs = [no_centre, centre], outputs = [output_df])
54
+ get_today_answers.click(get_spellbee_answers, inputs = [no_centre], outputs = [output_df])
55
 
56
  app.launch(debug = True, share = False)