-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite_rename.py
360 lines (333 loc) · 11.4 KB
/
site_rename.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import streamlit as st
import pandas as pd
import json
import streamlit as st
import os
import zipfile
import time
import shutil
import ast
import hashlib
import requests
#获取经纬度
def get_lat_lon(addr,key):
para = {
'key': key, # 请替换为你的高德地图API Key
'address': addr
}
url = 'https://restapi.amap.com/v3/geocode/geo?' # 高德地图地理编码API服务地址
result = requests.get(url, para).json()
print(result)
if result['geocodes'] and "geocodes" in list(result.keys()):
lon_lat = result['geocodes'][0]['location']
lon, lat = lon_lat.split(',')
return float(lon), float(lat)
return None, None
def md5(text):
# 创建一个MD5散列对象
hash_object = hashlib.md5(text.encode())
# hexdigest()方法返回散列的十六进制表示
return hash_object.hexdigest()
#大模型经纬度加工
def get_lng(x):
location = x.split(",")
print(location)
if len(location) == 0:
return ""
if len(location) >1:
print(location)
return location[0]
else:
return ""
def get_lat(x):
location = x.split(",")
if len(location) == 0:
return None
if len(location) >1:
#print(location[1])
return location[1]
else:
return ""
def get_gd_lnglat(url):
"""
输入高德分享地址,返回经纬度
"""
import urllib.parse
import requests
#url = "https://surl.amap.com/2nRQxUtlTbj9"
response = requests.get(url)
headers = response.headers
lat = response.request.path_url.split(",")[1]
lng = response.request.path_url.split(",")[2]
s = response.request.path_url
parts = s.split(',')
new_parts = []
for part in parts:
try:
decoded = urllib.parse.unquote(part)
new_parts.append(decoded)
except:
new_parts.append(part)
text = ','.join(new_parts)
res = {
"经度":lng,
"纬度":lat,
"文本":text
}
return res
mul_sel = st.sidebar.selectbox(options=['景点图片重命名','json转csv','数据处理'],label= '选择工具')
if mul_sel == 'json转csv':
st.title("JSON to csv Converter")
# 文本输入框
json_data = st.text_area("请输入 JSON 数据:")
excel = st.radio(options= ['是','否'],label= '上传excel匹配顺序',index = 1,key=11)
if excel == '是':
d1 = st.file_uploader(label = '上传csv')
if d1 is not None:
df_excel = pd.read_csv(d1)
col = st.selectbox(options=df_excel.columns,label='选择匹配列',index=0)
if json_data:
try:
data_dict = json.loads(json_data)
#df = pd.DataFrame.from_dict(data_dict, orient='index').reset_index()
df = pd.DataFrame([(key, value) for key, value in data_dict.items()], columns=['景点名', 'json结果'])
df.columns = ['景点名称', 'json结果']
types = st.selectbox(options=['是','否'],label= 'list是否合并',index = 1,key = 200)
if types == '是':
split_str = st.text_input(label='输入分割符')
df['json结果'] = df['json结果'].apply(lambda x : split_str.join(x))
if excel == '是':
df = pd.merge(left=df_excel[[col]],right=df,how='left',left_on=col,right_on='景点名称')
st.download_button(
label="下载为 csv 文件",
data=df.to_csv(index=False),
file_name='景点解析.csv',
)
except json.JSONDecodeError:
st.error("输入的 JSON 数据格式不正确。")
if mul_sel == '数据处理':
tools = st.radio("选择处理工具",['list转换csv','md5加密','经纬度查询','大模型经纬度处理','高德分享地址经纬度查询'])
if tools == 'list转换csv':
list_d = st.text_area("请输入 list 数据:")
#st.write(pd.DataFrame({"数据":list_data}) )
if list_d:
try:
list_data = ast.literal_eval(list_d)
df = pd.DataFrame({"数据":list_data})
st.download_button(
label="下载为 csv 文件",
data=df.to_csv(index=False),
file_name='list解析.csv'
)
except:
st.error("输入的 list 数据格式不正确。")
if tools == '高德分享地址经纬度查询':
int_url = st.text_input(label = "输入高德分享地址")
if st.button("查询经纬度"):
st.write(get_gd_lnglat(int_url))
if tools == 'md5加密':
d1 = st.file_uploader(label = '上传csv')
if d1 is not None:
df_excel = pd.read_csv(d1)
col = st.selectbox(options=df_excel.columns,label='选择匹配列',index=0)
df_excel['md5_data'] = df_excel[col].apply(md5)
st.download_button(
label="下载为 csv 文件",
data=df_excel.to_csv(index=False),
file_name='md5.csv')
if tools == '经纬度查询':
city = st.text_input(label="输入城市")
gd_key = st.text_input(label="输入高德apikey")
d1 = st.file_uploader(label = '上传地址csv')
if d1 is not None:
df_excel = pd.read_csv(d1)
col = st.selectbox(options=df_excel.columns,label='选择匹配地址',index=0)
if st.button("开始查询"):
lng_lat = []
for i in df_excel[col]:
lng_lat.append(get_lat_lon(city + i,key = gd_key))
df_excel['lng_lat'] = lng_lat
# 提取经度和纬度分别作为单独的列
df_excel['longitude'] = df_excel['lng_lat'].apply(lambda coord: coord[0])
df_excel['latitude'] = df_excel['lng_lat'].apply(lambda coord: coord[1])
st.download_button(
label="下载为 csv 文件",
data=df_excel.to_csv(index=False),
file_name='经纬度.csv')
if tools == '大模型经纬度处理':
list_d = st.text_area("请输入 list 数据:")
#st.write(pd.DataFrame({"数据":list_data}) )
if list_d:
try:
list_data = ast.literal_eval(list_d)
df = pd.DataFrame(list_data)
df['lng'] = df['poi_location'].apply(get_lng)
df['lat'] = df['poi_location'].apply(get_lat)
st.download_button(
label="下载为 csv 文件",
data=df.to_csv(index=False),
file_name='list-经纬度结果.csv'
)
except:
st.error("输入的 list 数据格式不正确。")
if mul_sel == '景点图片重命名':
uploaded_files = st.file_uploader("批量上传文件", accept_multiple_files=True)
df = ['北京环球度假区',
'故宫博物院',
'八达岭长城',
'中国国家博物馆',
'颐和园',
'恭王府',
'天坛',
'圆明园',
'慕田峪长城',
'北京动物园',
'天安门广场',
'北京野生动物园',
'中国科学技术馆',
'奥林匹克公园',
'雍和宫',
'鸟巢(国家体育场)',
'什刹海',
'明十三陵',
'景山公园',
'国家自然博物馆',
'古北水镇',
'中国地质博物馆',
'北京欢乐谷',
'北京海洋馆',
'北京天文馆',
'国家大剧院',
'北海公园',
'香山公园',
'南锣鼓巷',
'水立方(国家游泳中心)',
'居庸关长城',
'朝阳公园',
'泡泡玛特城市乐园',
'北京大观园',
'北京汽车博物馆',
'世界公园',
'前门大街',
'王府井',
'北京杜莎夫人蜡像馆',
'老舍茶馆',
'南山滑雪场',
'孔庙和国子监博物馆',
'清华大学艺术博物馆',
'太平洋海底世界',
'中央广播电视塔',
'798 艺术区',
'雁栖湖',
'大栅栏',
'北京宋庆龄故居',
'龙庆峡',
'潭柘寺',
'北京欢乐水魔方',
'德云社剧场',
'红螺寺',
'鼓楼',
'八大处公园',
'北京奥林匹克塔',
'中华民族博物院',
'三里屯',
'司马台长城',
'石景山游乐园',
'首都博物馆',
'国家图书馆',
'中国美术馆',
'北京刘老根大舞台',
'后海',
'纪晓岚故居',
'地坛',
'人民大会堂',
'中山公园',
'玉渊潭公园',
'红砖美术馆',
'白云观',
'亮马河国际风情水岸',
'北京市档案馆',
'世贸天阶',
'西单商业街',
'中国电影博物馆',
'中国航空博物馆',
'青龙峡风景区',
'国家植物园',
'陶然亭公园',
'白塔寺',
'大运河博物馆',
'东交民巷',
'北京平谷金海湖',
'周口店北京人遗址博物馆',
'牛街',
'朝阳剧场',
'北京西山国家森林公园',
'玉渡山景区',
'SOLANA 蓝色港湾',
'和平菓局',
'十渡风景名胜区',
'北京大学-校史馆',
'黑龙潭风景区',
'央视总部大楼',
'白瀑寺',
'雪都滑雪场',
'清凉谷风景区',
'北京西山滑雪场',
'北京园博园',
'八大胡同',
'望京 SOHO',
'北京温榆河公园',
'京东大溶洞',
'北京梨园剧场',
'日坛公园',
'华熙 Live·五棵松',
'五道口',
'京东大峡谷',
'京杭大运河',
'月坛公园',
'梅兰芳纪念馆',
'北京工人体育场',
'张裕爱斐堡酒庄',
'元大都城垣遗址公园',
'鬼笑石',
'海坨山',
'齐白石旧居纪念馆',
'梅兰芳大剧院',
'北京潘家园古玩城',
'灵境胡同']
site_name = st.selectbox(options=df,label = '选择景点' )
types = st.selectbox(options=['打卡机位','打卡姿势'],label = '打卡类型' )
city = st.text_input(label="请输入城市名")
if uploaded_files:
st.write(len(uploaded_files))
target_directory = './renamed_files/'
if os.path.exists(target_directory):
try:
shutil.rmtree(target_directory)
except PermissionError:
st.error(f"没有权限删除目录 {target_directory}。")
except OSError as e:
st.error(f"删除目录时出现错误:{e}")
os.makedirs(target_directory)
for index, uploaded_file in enumerate(uploaded_files):
old_filename = uploaded_file.name
base_name, extension = os.path.splitext(old_filename)
time.sleep(1)
timestamp = int(time.time())
new_filename = f'{city}_{site_name}_{types}_{timestamp}{extension}'
file_path = os.path.join(target_directory, new_filename)
print(file_path)
with open(file_path, 'wb') as f:
f.write(uploaded_file.read())
st.success(f'成功上传并重命名 {len(uploaded_files)} 个文件到 {target_directory}。')
# 添加下载按钮
if st.button('下载所有文件'):
zip_filename = site_name + '.zip'
with zipfile.ZipFile(zip_filename, 'w') as zipf:
for root, dirs, files in os.walk(target_directory):
for file in files:
file_path = os.path.join(root, file)
zipf.write(file_path, os.path.relpath(file_path, target_directory))
with open(zip_filename, 'rb') as f:
bytes_data = f.read()
st.download_button(label='点击下载', data=bytes_data, file_name=zip_filename)