1 Star 0 Fork 0

orgcollector/pypi-mirrors

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
notification.py 3.12 KB
一键复制 编辑 原始数据 按行查看 历史
ibigbug 提交于 2017-09-24 07:22 . use apscheduler
import tweepy
import smtplib
from config import load_config, EMAIL_OVERRIDE
CONFIG = load_config()
def prepare_twitter_message(status):
""" shrink to the right size and add link to site. """
link = "https://www.pypi-mirrors.org"
link_len = len(link) + 4
message_len = 140 - link_len
status_new = status[:message_len]
if len(status) > message_len:
status_new += "..."
status_new += " {0}".format(link)
return status_new
def update_twitter_status(status):
""" update the twitter account's status """
consumer_key = CONFIG.get('twitter_consumer_key')
consumer_secret = CONFIG.get('twitter_consumer_secret')
access_token = CONFIG.get('twitter_access_key')
access_token_secret = CONFIG.get('twitter_access_secret')
message = prepare_twitter_message(status)
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
api.update_status(message)
def send_warning_email(message):
""" send a message saying a mirror(s) is out of date. """
email_to = CONFIG.get('email_to')
email_from = CONFIG.get('email_from')
email_template = '''Subject: [pypi-mirrors] Mirror is out of Date Notice
This is an automated email from https://www.pypi-mirrors.org to let you
know that the following mirrors are out of date.
{message}
--
This automated message is sent to you by https://www.pypi-mirrors.org If you no
longer want to receive these emails, please contact Yuwei Ba(@ibigbug) on twitter
or reply to this email.
'''
email_body = email_template.format(message=message)
send_email(email_body, email_to, email_from)
def send_status_email(message):
""" send a daily status message """
email_to = CONFIG.get('email_to_admin')
email_from = CONFIG.get('email_from')
email_template = '''Subject: [pypi-mirrors] Mirrors are all up to date
This is an automated email from https://www.pypi-mirrors.org to let you
know that the following mirrors are all up to date.
{message}
--
This automated message is sent to you by https://www.pypi-mirrors.org If you no
longer want to receive these emails, please contact Yuwei Ba(@ibigbug) on twitter
or reply to this email.
'''
email_body = email_template.format(message=message)
send_email(email_body, email_to, email_from)
def send_email(email_body, email_to, email_from):
""" Send an email using the configuration provided """
email_host = CONFIG.get('email_host')
email_port = CONFIG.get('email_port')
email_user = CONFIG.get('email_user')
email_password = CONFIG.get('email_password')
email_bcc = CONFIG.get('email_bcc')
if EMAIL_OVERRIDE:
print 'Over-riding email with {0}.'.format(EMAIL_OVERRIDE)
email = EMAIL_OVERRIDE
else:
email = email_to
print("email to {0} , bcc: {1}; from {2}".format(
email, email_bcc, email_from))
smtp = smtplib.SMTP(email_host, email_port)
smtp.starttls()
smtp.login(email_user, email_password)
smtp.sendmail(email_from, [email, email_bcc], email_body)
smtp.quit()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/orgcollector/pypi-mirrors.git
git@gitee.com:orgcollector/pypi-mirrors.git
orgcollector
pypi-mirrors
pypi-mirrors
master

搜索帮助