Yasu777 commited on
Commit
ffa25ff
·
1 Parent(s): 2f83a2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -42
app.py CHANGED
@@ -10,7 +10,6 @@ from googleapiclient.discovery import build
10
  import urllib.request
11
  import urllib.error
12
  import datetime
13
- import html
14
 
15
  @st.cache_resource
16
  def get_top_urls_and_keyword(keyword):
@@ -27,7 +26,6 @@ def get_top_urls_and_keyword(keyword):
27
  ).execute()
28
 
29
  urls = [item['link'] for item in response["items"][:3]]
30
-
31
  return urls, keyword
32
 
33
  def get_valid_url(urls):
@@ -64,8 +62,10 @@ db.remove(Query().timestamp.test(lambda x: datetime.datetime.fromisoformat(x) <=
64
 
65
  st.title("記事生成ウェブアプリ")
66
  st.write("このアプリは、与えられたキーワードを使用して記事を生成します。")
 
67
  new_keyword = st.text_input("キーワード:")
68
  keyword_id = re.sub(r"\W+", "", new_keyword) if new_keyword else None
 
69
  last_keyword = db.search(Query().keyword_id.exists())
70
 
71
  if new_keyword and (not last_keyword or last_keyword[0]['keyword_id'] != keyword_id):
@@ -84,12 +84,12 @@ if new_keyword and (not last_keyword or last_keyword[0]['keyword_id'] != keyword
84
 
85
  if new_keyword:
86
  urls, keyword = get_top_urls_and_keyword(new_keyword)
87
- if len(urls) < 3:
88
  st.error("Google検索の結果が3つ未満です。別のキーワードを試してみてください。")
89
  else:
90
  url1, url2, url3 = urls
91
 
92
- if keyword_id:
93
  output1 = st.empty()
94
  output2 = st.empty()
95
  output3 = st.empty()
@@ -101,20 +101,29 @@ if keyword_id:
101
  editable_output2 = ""
102
 
103
  if st.button("記事構成作成", key=f"run_button_{keyword_id}"):
104
- with st.spinner("タイトル・見出し作成中..."):
105
- urls, keyword = get_top_urls_and_keyword(new_keyword)
106
- url1, url2, url3 = urls
107
- parsed_urls = [urlparse(url) for url in urls]
108
- if len(urls) != len(set(urls)):
109
- st.error("異なるURLを入力してください。")
110
- st.stop()
111
- elif len(set([url.netloc for url in parsed_urls])) != len(urls):
112
- st.error("異なるサイトのURLを入力してください。")
113
- st.stop()
114
- subprocess.run(["python3", "first.py", url1, url2, url3])
115
-
116
- with st.spinner("関連コンテンツ抽出中..."): # 新しいスピナーを追加
117
- subprocess.run(["python3", "second.py", keyword])
 
 
 
 
 
 
 
 
 
118
  with open("output2.txt", "r", encoding="utf-8") as f:
119
  editable_output2 = f.read()
120
  soup = BeautifulSoup(editable_output2, "html.parser")
@@ -131,23 +140,36 @@ if keyword_id:
131
  {"name": "output2.txt", "content": editable_output2, "tags": str(h_tags),
132
  "timestamp": current_time.isoformat(), "keyword_id": keyword_id}
133
  )
134
- st.success("処理が完了しました。")
 
 
 
 
 
135
 
136
  editable_output2 = st.text_area("output2.txtを編集してください:", value=editable_output2)
137
 
138
  if st.button("記事作成"):
139
- with st.spinner("記事作成中..."):
140
- subprocess.run(["python3", "run_third.py", editable_output2, keyword_id])
 
 
141
  with open("output3.txt", "r", encoding="utf-8") as f:
142
  content = f.read()
143
  output3.text(content)
 
 
 
 
144
 
145
  if st.button("保存"):
146
  h2_limit = 5
147
  h3_limit = 10
 
148
  soup = BeautifulSoup(editable_output2, "html.parser")
149
  h2_count = len(soup.find_all("h2"))
150
  h3_count = len(soup.find_all("h3"))
 
151
  if h2_count > h2_limit or h3_count > h3_limit:
152
  st.error(f"h2タグの数が{h2_limit}を、h3タグの数が{h3_limit}を超えています。")
153
  elif not is_valid_html(editable_output2):
@@ -157,31 +179,11 @@ if keyword_id:
157
  with open("output2.txt", "w", encoding="utf-8") as f:
158
  f.write(content)
159
  db.upsert({"name": "output2.txt", "content": content, "timestamp": current_time.isoformat(),
160
- "keyword_id": keyword_id}, (Query().name == "output2.txt") & (Query().keyword_id == keyword_id))
161
  st.write("output2.txt に変更が保存されました。")
162
 
163
  if st.button("データクリア"):
164
  db.remove(Query().keyword_id == keyword_id)
165
  st.write("データベースがクリアされました。")
166
-
167
- # コピー機能の追加
168
- if os.path.exists("output3.txt"):
169
- with open("output3.txt", "r", encoding="utf-8") as f:
170
- content = f.read()
171
-
172
- sanitized_content = html.escape(content)
173
- st.text_area("コピーするテキスト:", value=sanitized_content, height=200, key="copy_area")
174
-
175
- copy_button = """
176
- <button id="copyButton">
177
- テキストをコピー
178
- </button>
179
- <script>
180
- document.getElementById('copyButton').addEventListener('click', function() {
181
- navigator.clipboard.writeText(document.getElementById('copy_area').value);
182
- });
183
- </script>
184
- """
185
- st.markdown(copy_button, unsafe_allow_html=True)
186
  else:
187
  st.warning("キーワードを入力してください。")
 
10
  import urllib.request
11
  import urllib.error
12
  import datetime
 
13
 
14
  @st.cache_resource
15
  def get_top_urls_and_keyword(keyword):
 
26
  ).execute()
27
 
28
  urls = [item['link'] for item in response["items"][:3]]
 
29
  return urls, keyword
30
 
31
  def get_valid_url(urls):
 
62
 
63
  st.title("記事生成ウェブアプリ")
64
  st.write("このアプリは、与えられたキーワードを使用して記事を生成します。")
65
+
66
  new_keyword = st.text_input("キーワード:")
67
  keyword_id = re.sub(r"\W+", "", new_keyword) if new_keyword else None
68
+
69
  last_keyword = db.search(Query().keyword_id.exists())
70
 
71
  if new_keyword and (not last_keyword or last_keyword[0]['keyword_id'] != keyword_id):
 
84
 
85
  if new_keyword:
86
  urls, keyword = get_top_urls_and_keyword(new_keyword)
87
+ if len(urls) < 3:
88
  st.error("Google検索の結果が3つ未満です。別のキーワードを試してみてください。")
89
  else:
90
  url1, url2, url3 = urls
91
 
92
+ if keyword_id:
93
  output1 = st.empty()
94
  output2 = st.empty()
95
  output3 = st.empty()
 
101
  editable_output2 = ""
102
 
103
  if st.button("記事構成作成", key=f"run_button_{keyword_id}"):
104
+ try:
105
+ with st.spinner("タイトル・見出し作成中..."):
106
+ urls, keyword = get_top_urls_and_keyword(new_keyword)
107
+ url1, url2, url3 = urls
108
+
109
+ parsed_urls = [urlparse(url) for url in urls]
110
+ if len(urls) != len(set(urls)):
111
+ st.error("異なるURLを入力してください。")
112
+ st.stop()
113
+ elif len(set([url.netloc for url in parsed_urls])) != len(urls):
114
+ st.error("異なるサイトのURLを入力してください。")
115
+ st.stop()
116
+
117
+ subprocess.run(["python3", "first.py", url1, url2, url3], check=True)
118
+
119
+ with open("output1.txt", "r", encoding="utf-8") as f:
120
+ content = f.read()
121
+ content = re.sub(r"\n関連するテキスト部分:.*", "", content, flags=re.DOTALL)
122
+ output1.text(content)
123
+ db.upsert({"name": "output1.txt", "content": content, "keyword_id": keyword_id},
124
+ (Query().name == "output1.txt") & (Query().keyword_id == keyword_id))
125
+
126
+ subprocess.run(["python3", "second.py", keyword], check=True)
127
  with open("output2.txt", "r", encoding="utf-8") as f:
128
  editable_output2 = f.read()
129
  soup = BeautifulSoup(editable_output2, "html.parser")
 
140
  {"name": "output2.txt", "content": editable_output2, "tags": str(h_tags),
141
  "timestamp": current_time.isoformat(), "keyword_id": keyword_id}
142
  )
143
+
144
+ st.success("処理が完了しました。")
145
+ except subprocess.CalledProcessError:
146
+ st.error("記事の構成作成中にエラーが発生しました。もう一度お試しください。")
147
+ except Exception as e:
148
+ st.error(f"予期せぬエラーが発生しました:{str(e)}")
149
 
150
  editable_output2 = st.text_area("output2.txtを編集してください:", value=editable_output2)
151
 
152
  if st.button("記事作成"):
153
+ try:
154
+ with st.spinner("記事作成中..."):
155
+ subprocess.run(["python3", "run_third.py", editable_output2, keyword_id], check=True)
156
+
157
  with open("output3.txt", "r", encoding="utf-8") as f:
158
  content = f.read()
159
  output3.text(content)
160
+ except subprocess.CalledProcessError:
161
+ st.error("エラーです。やり直してください。")
162
+ except Exception as e:
163
+ st.error(f"予期せぬエラーが発生しました:{str(e)}")
164
 
165
  if st.button("保存"):
166
  h2_limit = 5
167
  h3_limit = 10
168
+
169
  soup = BeautifulSoup(editable_output2, "html.parser")
170
  h2_count = len(soup.find_all("h2"))
171
  h3_count = len(soup.find_all("h3"))
172
+
173
  if h2_count > h2_limit or h3_count > h3_limit:
174
  st.error(f"h2タグの数が{h2_limit}を、h3タグの数が{h3_limit}を超えています。")
175
  elif not is_valid_html(editable_output2):
 
179
  with open("output2.txt", "w", encoding="utf-8") as f:
180
  f.write(content)
181
  db.upsert({"name": "output2.txt", "content": content, "timestamp": current_time.isoformat(),
182
+ "keyword_id": keyword_id}, (Query().name == "output2.txt") & (Query().keyword_id == keyword_id))
183
  st.write("output2.txt に変更が保存されました。")
184
 
185
  if st.button("データクリア"):
186
  db.remove(Query().keyword_id == keyword_id)
187
  st.write("データベースがクリアされました。")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  else:
189
  st.warning("キーワードを入力してください。")