-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
138 lines (108 loc) · 4.66 KB
/
app.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
import os
import yagmail
import datetime
import codecs
import requests
import schedule
import time
from pyquery import PyQuery as pq
from zhipuai import ZhipuAI
def get_contents(path):
with open(path, 'r', encoding='utf-8') as f:
return f.read()
def get_emails(path):
with open(path, 'r') as f:
return f.read().splitlines()
def get_ai_analysis(path):
try:
client = ZhipuAI(api_key=os.environ.get("ZHIPUAI_API_KEY"))
trends = get_contents(path)
print(f'ai is reading, the info is:\n{trends}')
response = client.chat.completions.create(
model="glm-4-flash", # 填写需要调用的模型编码
messages=[
{"role": "system", "content": "你是一个安全的 github trends 分析专家。负责分析 github 每日 python 项目的趋势。首先提取安全和不敏感的项目。然后将项目的介绍翻译成中文。输出整齐精致。接着在下一行,安利一个最惊艳的项目。再换一行,最后总结今天的趋势项目关注的领域和特点。语言保持简洁。最后一句话:产品联系: [email protected]"},
{"role": "user", "content":f'{trends}' }
],
)
ans = response.choices[0].message.content
return ans
except Exception as e:
print(f'when ai analyze, {e} occurs...')
def createtext(filename):
with open(filename, 'w') as f:
f.write('')
def scrape(language, filename):
try:
HEADERS = {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/20100101 Firefox/11.0',
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding' : 'gzip,deflate,sdch',
'Accept-Language' : 'zh-CN,zh;q=0.8'
}
url = f'https://github.com/trending/{language}'
r = requests.get(url, headers=HEADERS, timeout=10)
print(f'scrape() status_code: {r.status_code}')
assert r.status_code == 200
# print(f'scrape() content:\n {r.content}')
d = pq(r.content)
items = d('div.Box article.Box-row')
with codecs.open(filename, "a", "utf-8") as f:
for index, item in enumerate(items, start=1):
i = pq(item)
title = i(".lh-condensed a").text()
description = i("p.col-9").text()
url = i(".lh-condensed a").attr("href")
url = "https://github.com" + url
print(url)
f.write(u"{index}. [{title}]:{description}({url})\n".format(index=index, title=title, description=description, url=url))
except Exception as e:
print(f'when scrape {e} occurs')
def job():
strdate = datetime.datetime.now().strftime('%Y-%m-%d')
filename = f'{strdate}.txt'
print(f'{strdate} start the job...')
createtext(filename)
attempts = 0
while attempts < 6:
try:
print(f'try at {attempts} to get trends~')
scrape('python', filename) # weak point
print('scape ends\n')
ans = get_ai_analysis(filename)
print(f'ai ans is:\n {ans}')
with open(filename, 'w', encoding='utf-8') as f:
f.write(ans)
return filename
except Exception as e:
attempts += 1
print(f"Attempt {attempts} failed with error: {e}")
time.sleep(300) # Wait 300s before retrying
raise Exception("All attempts to scrape data have failed.")
def send_email(src, dst, subject, contents, attachments):
pwd = os.environ.get('wangyi_emai_auth')
yag = yagmail.SMTP(user=src, password=pwd, host='smtp.163.com', port='465')
yag.send(to=dst, subject=subject, contents=contents, attachments=attachments)
yag.close()
def send_emails(src, tos, subject, contents, attachments):
for to in tos:
send_email(src, to, subject, contents, attachments)
def daily_task():
try:
path = job()
src = '[email protected]'
tos = get_emails('emails.txt')
subject = '今日AI+头条项目'
contents = get_contents(path)
attachments = path
send_emails(src, tos, subject, contents, attachments)
except Exception as e:
print(f"{e} occured in daily_task")
if __name__ == '__main__':
try:
schedule.every().day.at('18:00').do(daily_task)
while True:
schedule.run_pending()
time.sleep(1)
except Exception as e:
print(f"{e} occured~")