Skip to content

Commit d60409f

Browse files
committed
v2.2.2
Signed-off-by: TIANHE <TIANHE@GMAIL.COM>
1 parent a6ea4d9 commit d60409f

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

backend_api_python/app/data_sources/polymarket.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,26 @@ def _get_cached_markets(self, category: str = None, limit: int = 50) -> Optional
262262
cur.close()
263263

264264
if rows:
265-
return [{
266-
"market_id": str(row.get('market_id') or ''),
267-
"question": row.get('question') or '',
268-
"category": row.get('category') or 'other',
269-
"current_probability": float(row.get('current_probability') or 0),
270-
"volume_24h": float(row.get('volume_24h') or 0),
271-
"liquidity": float(row.get('liquidity') or 0),
272-
"end_date_iso": row.get('end_date_iso'),
273-
"status": row.get('status') or 'active',
274-
"outcome_tokens": row.get('outcome_tokens') if row.get('outcome_tokens') else {},
275-
"polymarket_url": f"https://polymarket.com/event/{row.get('market_id') or ''}"
276-
} for row in rows]
265+
result = []
266+
for row in rows:
267+
market_id = str(row.get('market_id') or '')
268+
slug = row.get('slug')
269+
# 确保使用正确的URL构建方法
270+
polymarket_url = self._build_polymarket_url(slug, market_id)
271+
result.append({
272+
"market_id": market_id,
273+
"question": row.get('question') or '',
274+
"category": row.get('category') or 'other',
275+
"current_probability": float(row.get('current_probability') or 0),
276+
"volume_24h": float(row.get('volume_24h') or 0),
277+
"liquidity": float(row.get('liquidity') or 0),
278+
"end_date_iso": row.get('end_date_iso'),
279+
"status": row.get('status') or 'active',
280+
"outcome_tokens": row.get('outcome_tokens') if row.get('outcome_tokens') else {},
281+
"polymarket_url": polymarket_url,
282+
"slug": slug if slug and not str(slug).isdigit() else None
283+
})
284+
return result
277285

278286
return None
279287
except Exception as e:

backend_api_python/app/routes/polymarket.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,32 +160,39 @@ def get_markets():
160160
opportunity_markets = markets[:min(limit, len(markets))]
161161

162162
# 排序和筛选
163+
# 辅助函数:安全获取 ai_analysis 数据(处理 None 情况)
164+
def safe_get_ai_analysis(market, key, default=0):
165+
ai_analysis = market.get('ai_analysis')
166+
if ai_analysis is None:
167+
return default
168+
return ai_analysis.get(key, default) or default
169+
163170
if sort_by == "ai_score":
164171
# 按AI机会评分排序(高概率/高回报比优先)
165172
opportunity_markets.sort(
166173
key=lambda x: (
167-
x.get('ai_analysis', {}).get('opportunity_score', 0) or 0,
168-
abs(x.get('ai_analysis', {}).get('divergence', 0) or 0), # 差异越大越好
169-
x.get('ai_analysis', {}).get('confidence_score', 0) or 0 # 置信度越高越好
174+
safe_get_ai_analysis(x, 'opportunity_score', 0),
175+
abs(safe_get_ai_analysis(x, 'divergence', 0)), # 差异越大越好
176+
safe_get_ai_analysis(x, 'confidence_score', 0) # 置信度越高越好
170177
),
171178
reverse=True
172179
)
173180
elif sort_by == "high_probability":
174181
# 高概率机会:AI预测概率 > 市场概率 + 10%
175182
opportunity_markets.sort(
176183
key=lambda x: (
177-
x.get('ai_analysis', {}).get('ai_predicted_probability', 0) or 0,
178-
x.get('ai_analysis', {}).get('confidence_score', 0) or 0
184+
safe_get_ai_analysis(x, 'ai_predicted_probability', 0),
185+
safe_get_ai_analysis(x, 'confidence_score', 0)
179186
),
180187
reverse=True
181188
)
182189
elif sort_by == "high_return":
183190
# 高回报比机会:AI与市场差异大且置信度高
184191
opportunity_markets.sort(
185192
key=lambda x: (
186-
abs(x.get('ai_analysis', {}).get('divergence', 0) or 0) *
187-
(x.get('ai_analysis', {}).get('confidence_score', 0) or 0) / 100,
188-
x.get('ai_analysis', {}).get('opportunity_score', 0) or 0
193+
abs(safe_get_ai_analysis(x, 'divergence', 0)) *
194+
safe_get_ai_analysis(x, 'confidence_score', 0) / 100,
195+
safe_get_ai_analysis(x, 'opportunity_score', 0)
189196
),
190197
reverse=True
191198
)

backend_api_python/app/services/market_data_collector.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,14 +1242,26 @@ def _get_polymarket_events(self, symbol: str, market: str) -> List[Dict]:
12421242
market_id = market_data.get('market_id')
12431243
if market_id and market_id not in seen:
12441244
seen.add(market_id)
1245+
# 构建正确的 Polymarket URL
1246+
# 优先使用已有的 polymarket_url,如果没有则根据 slug 或 market_id 构建
1247+
polymarket_url = market_data.get('polymarket_url')
1248+
if not polymarket_url:
1249+
slug = market_data.get('slug')
1250+
if slug and not str(slug).isdigit() and ('-' in str(slug) or any(c.isalpha() for c in str(slug))):
1251+
# 使用有效的 slug
1252+
polymarket_url = f"https://polymarket.com/event/{slug}"
1253+
else:
1254+
# 使用 markets 端点(更可靠)
1255+
polymarket_url = f"https://polymarket.com/markets/{market_id}"
1256+
12451257
result.append({
12461258
"market_id": market_id,
12471259
"question": market_data.get('question', ''),
12481260
"current_probability": market_data.get('current_probability', 50.0),
12491261
"volume_24h": market_data.get('volume_24h', 0),
12501262
"liquidity": market_data.get('liquidity', 0),
12511263
"category": market_data.get('category', 'other'),
1252-
"polymarket_url": market_data.get('polymarket_url', f"https://polymarket.com/event/{market_id}")
1264+
"polymarket_url": polymarket_url
12531265
})
12541266

12551267
logger.info(f"Total {len(result)} unique Polymarket events found for {symbol}")

0 commit comments

Comments
 (0)