Nymbo commited on
Commit
3e374ff
·
verified ·
1 Parent(s): dd33d18

Modernize: Gradio 6.29, Python 3.12; JSON-safe titles, skip anchors without href, per-request link list, timeouts

Browse files
Files changed (3) hide show
  1. README.md +2 -1
  2. app.py +11 -7
  3. requirements.txt +2 -1
README.md CHANGED
@@ -4,7 +4,8 @@ emoji: 👁
4
  colorFrom: indigo
5
  colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 4.19.2
 
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: indigo
5
  colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 6.29.0
8
+ python_version: "3.12"
9
  app_file: app.py
10
  pinned: false
11
  ---
app.py CHANGED
@@ -99,7 +99,9 @@ link_box = []
99
 
100
  def link_find(url):
101
  out = []
102
- source = requests.get(url)
 
 
103
  if source.status_code ==200:
104
  print("YES")
105
  #soup = bs4.BeautifulSoup(source.content,'lxml')
@@ -111,11 +113,11 @@ def link_find(url):
111
  rawt=soup.text
112
  #out.append(rawp)
113
  #out.append("HTML fragments: ")
114
- node1 = {"URL":url,"TITLE":soup.title,"STRING":soup.description,"TEXT":rawt,"LINKS":[],"TREE":[]}
115
  node2 = {"URL":url,"LINK_KEY":[],"LINKS":[],"TREE":[]}
116
 
117
  q=("a","p","span","content","article")
118
- for p in soup.find_all("a"):
119
  url0=p.get('href')
120
  if url0.startswith("//"):
121
  print(url0)
@@ -135,12 +137,12 @@ def link_find(url):
135
  else:
136
  uri=url0
137
  node1['LINKS'].append(uri)
138
- node1['TREE'].append({"URL":uri,"TITLE":p.get('title'),"STRING":p.string,"TEXT":"","LINKS":[],"TREE":[]})
139
  node2['TREE'].append({"URL":uri,"LINKS":[],"TREE":[]})
140
  node2['LINKS'].append(uri)
141
  #node2['LINK_KEY'].append(uri_key)
142
  link_box.append(uri)
143
- #out.append({"URL":p.get('href'),"TITLE":p.get('title'),"STRING":p.string,"TEXT":"","TREE":[]})
144
 
145
  else:
146
  print("NO")
@@ -152,6 +154,8 @@ def link_find(url):
152
  def sitemap(url,level):
153
  uri=""
154
  uri0=""
 
 
155
  if url != "" and url != None:
156
  link1,link2=link_find(url)
157
  if level >=2:
@@ -226,7 +230,7 @@ def sitemap_OG(url,level):
226
  except Exception as e:
227
  print (e)
228
  return link1
229
- with gr.Blocks(theme="Nymbo/Nymbo_Theme") as app:
230
  with gr.Row():
231
  with gr.Column(scale=3):
232
  with gr.Row():
@@ -238,4 +242,4 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme") as app:
238
  with gr.Column(scale=1):
239
  outmap=gr.JSON()
240
  btn.click(sitemap,[inp,level],[outp,outmap,key_json])
241
- app.launch()
 
99
 
100
  def link_find(url):
101
  out = []
102
+ node1 = {"URL":url,"TITLE":None,"STRING":None,"TEXT":"","LINKS":[],"TREE":[]}
103
+ node2 = {"URL":url,"LINK_KEY":[],"LINKS":[],"TREE":[]}
104
+ source = requests.get(url, timeout=20, headers={"User-Agent": "Mozilla/5.0 (compatible; JSON-Crawl)"})
105
  if source.status_code ==200:
106
  print("YES")
107
  #soup = bs4.BeautifulSoup(source.content,'lxml')
 
113
  rawt=soup.text
114
  #out.append(rawp)
115
  #out.append("HTML fragments: ")
116
+ node1 = {"URL":url,"TITLE":str(soup.title.string) if soup.title and soup.title.string else None,"STRING":None,"TEXT":rawt,"LINKS":[],"TREE":[]}
117
  node2 = {"URL":url,"LINK_KEY":[],"LINKS":[],"TREE":[]}
118
 
119
  q=("a","p","span","content","article")
120
+ for p in soup.find_all("a", href=True):
121
  url0=p.get('href')
122
  if url0.startswith("//"):
123
  print(url0)
 
137
  else:
138
  uri=url0
139
  node1['LINKS'].append(uri)
140
+ node1['TREE'].append({"URL":uri,"TITLE":p.get('title'),"STRING":str(p.string) if p.string else None,"TEXT":"","LINKS":[],"TREE":[]})
141
  node2['TREE'].append({"URL":uri,"LINKS":[],"TREE":[]})
142
  node2['LINKS'].append(uri)
143
  #node2['LINK_KEY'].append(uri_key)
144
  link_box.append(uri)
145
+ #out.append({"URL":p.get('href'),"TITLE":p.get('title'),"STRING":str(p.string) if p.string else None,"TEXT":"","TREE":[]})
146
 
147
  else:
148
  print("NO")
 
154
  def sitemap(url,level):
155
  uri=""
156
  uri0=""
157
+ link_box.clear() # was global and grew across requests
158
+ link1, link2, uri_key = {}, {}, {}
159
  if url != "" and url != None:
160
  link1,link2=link_find(url)
161
  if level >=2:
 
230
  except Exception as e:
231
  print (e)
232
  return link1
233
+ with gr.Blocks() as app:
234
  with gr.Row():
235
  with gr.Column(scale=3):
236
  with gr.Row():
 
242
  with gr.Column(scale=1):
243
  outmap=gr.JSON()
244
  btn.click(sitemap,[inp,level],[outp,outmap,key_json])
245
+ app.launch(theme="Nymbo/Nymbo_Theme")
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- bs4
 
 
1
+ bs4
2
+ requests