origin : New job hopping to a company , I didn't expect that the first challenge was to send out weekly reports every week . Tell your boss what you have done this week and what you are going to do next week . I have a bad memory , Often forget things .
So many times the weekly newspaper will forget to send . therefore , I decided to write a gadget .
Github: Weekday Gadget
Easy to use
cmd -a " Job content " -p # Print success
Complex use
cmd -e -p # Activate vim, then :wq Then go on cmd perform , As to the git commit -a, But print success
I forgot how to use the program I wrote python Learning exchange group :660193417 ###
cmd -h # Print help
There is a temporary file , Used to store records . After each email sent , Archive temporary documents .
# The temporary file , There is ~/.weekday/current.rp
[global]
editor = vim
[to]
email =
cc =
[from]
email =
password =
nickname =
[SMTP]
host = smtp.exmail.qq.com
Can be in python When the script executes , Read the configuration file python Learning exchange group :660193417 ###
# Get configuration item examples
import ConfigParser
config = ConfigParser.ConfigParser()
config.readfp(open(' file name '))
defaultEditor = confg.get('global', 'editor') # return vim
You can parse the parameters passed in when executing the command
import argparse
parser = argparse.ArgumentParser(
description='Tell your leader what you did this week')
# define command line parameters
parser.add_argument('-v', '--version',
action='store_true',
help='show the version information')
parser.add_argument('-e', '--edit',
action='store_true',
help='open default editor for editing report')
args = parser.parse_args()
print args
Ready to send email , The first markdown Turn into HTML, And then use smtplib Send E-mail .
import smtplib
import mistune
from email.mime.text import MIMEText
content = mistune.markdown(content, escape=True, hard_wrap=True)
msg = MIMEText(content, 'html', 'utf-8')
msg['Subject'] = u' Work week %s\r\n' % date
msg['From'] = hFrom
msg['To'] = hTo
msg['Cc'] = hCc
smtp = smtplib.SMTP(conf['host'])
smtp.login(conf['from_email'], conf['password'])
smtp.sendmail(conf['from_email'],
TO_ADDR + CC_ADDR,
msg.as_string())
smtp.close()
The key codes of the subdivision function implementation have been sorted out . Now we just need to connect them through a series of logic . It looks like we can accomplish our goal .
# main.py
def main():
args = loadArguments()
if args.version:
print DESC
if args.append:
append(args.append, tmpFileName)
// ... other code ...
if __name__ == '__main__':
main()
python main.py -a Hello -p
Finally, we just need to cooperate crontab Tools , You can realize periodic timing transmission .python Learning exchange group :660193417 ###
# crontab -l
SHELL=/bin/zsh
PATH=/usr/local/bin
HOME=/Users/qiuwei
# run tasks
# minute hour day month week command
0 18 * * 5 wp -p