-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbounty_hunter.py
More file actions
executable file
·354 lines (293 loc) · 12.1 KB
/
Copy pathbounty_hunter.py
File metadata and controls
executable file
·354 lines (293 loc) · 12.1 KB
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
#!/usr/bin/env python3
"""
Bounty Hunter Assistant - 找到并分析GitHub赏金项目
帮助用户识别合适的赏金任务进行贡献
"""
import requests
import json
import re
from datetime import datetime
from typing import List, Dict, Optional
import sys
class BountyFinder:
def __init__(self, token: str):
self.token = token
self.headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github.v3+json"
}
self.api_base = "https://api.github.com"
def search_bounty_issues(self, languages: Optional[List[str]] = None, limit: int = 20) -> List[Dict]:
"""搜索带有赏金标签的issue"""
results = []
# 常见赏金标签关键词
bounty_keywords = [
"bounty",
"good first issue",
"help wanted",
"hacktoberfest",
"reward",
"赏金" # 中文标签
]
for keyword in bounty_keywords:
# 构建搜索查询
query_parts = [f'label:"{keyword}"', 'is:issue', 'is:open']
if languages:
lang_query = " OR ".join([f'language:{lang}' for lang in languages])
query_parts.append(f"({lang_query})")
query = " ".join(query_parts)
try:
params = {
"q": query,
"per_page": 100,
"sort": "updated",
"order": "desc"
}
response = requests.get(
f"{self.api_base}/search/issues",
headers=self.headers,
params=params
)
response.raise_for_status()
data = response.json()
if "items" in data:
for item in data["items"]:
if "pull_request" not in item: # 排除PR
issue_info = self._parse_issue(item)
if issue_info:
results.append(issue_info)
except Exception as e:
print(f"搜索 '{keyword}' 时出错: {e}")
# 去重
unique_results = self._deduplicate_results(results)
return unique_results[:limit]
def _parse_issue(self, issue: Dict) -> Optional[Dict]:
"""解析issue数据"""
repo_url = issue["repository_url"]
parts = repo_url.split("/")
owner = parts[-2]
repo = parts[-1]
# 提取可能的赏金金额
bounty_amount = self._extract_bounty_amount(issue)
# 评估任务难度
difficulty = self._assess_difficulty(issue)
return {
"title": issue["title"],
"number": issue["number"],
"url": issue["html_url"],
"repo_full_name": f"{owner}/{repo}",
"repo_url": f"https://github.com/{owner}/{repo}",
"created_at": issue["created_at"],
"updated_at": issue["updated_at"],
"labels": [label["name"] for label in issue.get("labels", [])],
"bounty_amount": bounty_amount,
"difficulty": difficulty,
"comments": issue.get("comments", 0),
"reactions": issue.get("reactions", {}).get("total_count", 0),
"body": issue.get("body", "")[:500] # 前言部分
}
def _extract_bounty_amount(self, issue: Dict) -> Optional[float]:
"""从标签或标题中提取赏金金额"""
# 检查标签
for label in issue.get("labels", []):
label_name = label["name"].lower()
if "$" in label_name:
try:
amount_str = re.sub(r'[^\d.]', '', label_name)
return float(amount_str)
except:
continue
# 检查标题
amount_pattern = r'\$([0-9,]+(?:\.[0-9]+)?)'
matches = re.findall(amount_pattern, issue["title"])
if matches:
try:
return float(matches[0].replace(",", ""))
except:
pass
return None
def _assess_difficulty(self, issue: Dict) -> str:
"""评估任务难度"""
labels = [l.lower() for l in issue.get("labels", [])]
if "good first issue" in labels:
return "简单"
elif "help wanted" in labels:
return "中等"
else:
return "需评估"
def _deduplicate_results(self, results: List[Dict]) -> List[Dict]:
"""去重"""
seen = set()
unique = []
for r in results:
key = f"{r['repo_full_name']}#{r['number']}"
if key not in seen:
seen.add(key)
unique.append(r)
return unique
def get_repo_stats(self, repo_full_name: str) -> Dict:
"""获取仓库统计信息"""
try:
response = requests.get(
f"{self.api_base}/repos/{repo_full_name}",
headers=self.headers
)
response.raise_for_status()
data = response.json()
return {
"stars": data.get("stargazers_count", 0),
"forks": data.get("forks_count", 0),
"open_issues": data.get("open_issues_count", 0),
"language": data.get("language", "Unknown"),
"description": data.get("description", ""),
"updated_at": data.get("updated_at", "")
}
except Exception as e:
print(f"获取仓库信息失败: {e}")
return {}
def analyze_issue(self, repo_full_name: str, issue_number: int) -> Dict:
"""深入分析issue详情"""
try:
response = requests.get(
f"{self.api_base}/repos/{repo_full_name}/issues/{issue_number}",
headers=self.headers
)
response.raise_for_status()
issue = response.json()
# 获取仓库统计
repo_stats = self.get_repo_stats(repo_full_name)
# 分析可行性
feasibility = self._analyze_feasibility(issue, repo_stats)
return {
"issue": issue,
"repo_stats": repo_stats,
"feasibility": feasibility
}
except Exception as e:
print(f"分析issue失败: {e}")
return {}
def _analyze_feasibility(self, issue: Dict, repo_stats: Dict) -> Dict:
"""分析任务可行性"""
feasibility_score = 0
reasons = []
# 仓库活跃度
if repo_stats.get("stars", 0) > 100:
feasibility_score += 2
reasons.append("仓库活跃度较高")
elif repo_stats.get("stars", 0) > 20:
feasibility_score += 1
reasons.append("仓库有一定关注度")
# 标签分析
labels = [l.lower() for l in issue.get("labels", [])]
if "good first issue" in labels:
feasibility_score += 3
reasons.append("适合新手的任务")
elif "help wanted" in labels:
feasibility_score += 2
reasons.append("需要帮助的任务")
# 讨论程度
comments = issue.get("comments", 0)
if comments < 5:
feasibility_score += 1
reasons.append("讨论较少,竞争可能较小")
elif comments > 20:
feasibility_score -= 1
reasons.append("讨论较多,可能有竞争")
# 代码语言
lang = repo_stats.get("language", "")
common_langs = ["Python", "JavaScript", "TypeScript", "Go", "Java"]
if lang in common_langs:
feasibility_score += 1
reasons.append(f"使用常见语言: {lang}")
# 生成评分
if feasibility_score >= 5:
grade = "⭐⭐⭐ 高"
elif feasibility_score >= 3:
grade = "⭐⭐ 中"
else:
grade = "⭐ 低"
return {
"score": feasibility_score,
"grade": grade,
"reasons": reasons
}
def display_bounty_list(issues: List[Dict]):
"""显示赏金任务列表"""
if not issues:
print("\n❌ 没有找到符合条件的赏金任务")
return
print(f"\n✅ 找到 {len(issues)} 个潜在的赏金任务:\n")
print("=" * 110)
for i, issue in enumerate(issues, 1):
print(f"\n{i}. {issue['title']}")
print(f" 📦 仓库: {issue['repo_full_name']}")
print(f" 🔗 链接: {issue['url']}")
print(f" 🏷️ 标签: {', '.join(issue['labels'])}")
if issue['bounty_amount']:
print(f" 💰 预估赏金: ${issue['bounty_amount']}")
print(f" 📊 难度: {issue['difficulty']}")
print(f" 📅 创建: {issue['created_at'][:10]} | 💬 评论: {issue['comments']}")
if issue['body']:
print(f" 📝 简介: {issue['body'][:100]}...")
print("-" * 110)
def main():
print("=" * 50)
print("GitHub 赏金任务查找助手")
print("=" * 50)
# 获取token
token = input("\n请输入GitHub个人访问令牌 (Personal Access Token): ").strip()
if not token:
print("❌ Token不能为空")
return
finder = BountyFinder(token)
# 选择编程语言
print("\n请选择要筛选的编程语言 (用逗号分隔, 直接回车表示不限制):")
print(" 常见: Python, JavaScript, TypeScript, Go, Java, Rust, C++")
lang_input = input("语言: ").strip()
languages = [l.strip() for l in lang_input.split(",")] if lang_input else None
# 搜索
print("\n🔍 正在搜索赏金任务...")
issues = finder.search_bounty_issues(languages=languages, limit=20)
# 显示结果
display_bounty_list(issues)
# 选择要深入分析的任务
if issues:
print("\n" + "=" * 50)
choice = input("\n是否要深入分析某个任务? (输入编号, 0=退出): ").strip()
if choice.isdigit() and int(choice) > 0 and int(choice) <= len(issues):
idx = int(choice) - 1
selected = issues[idx]
print(f"\n🔍 正在分析任务: {selected['title']}")
analysis = finder.analyze_issue(selected['repo_full_name'], selected['number'])
if analysis:
print(f"\n📊 仓库统计:")
repo_stats = analysis['repo_stats']
print(f" ⭐ Stars: {repo_stats.get('stars', 0)}")
print(f" 🍴 Forks: {repo_stats.get('forks', 0)}")
print(f" 🐛 Issues: {repo_stats.get('open_issues', 0)}")
print(f" 💻 语言: {repo_stats.get('language', 'Unknown')}")
print(f" 📝 描述: {repo_stats.get('description', 'N/A')[:80]}...")
print(f"\n✅ 可行性分析:")
feasible = analysis['feasibility']
print(f" 评级: {feasible['grade']}")
print(f" 分数: {feasible['score']}")
print(f" 理由:")
for reason in feasible['reasons']:
print(f" • {reason}")
# 保存结果
save = input("\n💾 保存搜索结果到文件? (y/n): ").strip().lower()
if save == 'y':
filename = f"bounty_tasks_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
with open(filename, 'w', encoding='utf-8') as f:
json.dump(issues, f, indent=2, ensure_ascii=False)
print(f"✅ 结果已保存到: {filename}")
print("\n👋 谢谢使用!")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\n👋 程序已中断")
except Exception as e:
print(f"\n❌ 发生错误: {e}")
import traceback
traceback.print_exc()