Python with love to record and log girlfriend

--- --- restore content begins

Read the text takes about 5 minutes.

Recently been learning Python, thought to write a program to automatically send micro letter to his girlfriend every morning, content is the time we were in love, and one day a good morning.

Preparatory

1.Python of wxpy library, wxpy basis itchat on by a large number of interfaces optimized to enhance the ease of use of the module, and feature-rich expansion.

wxpy library function is very powerful, for example, automatically sends when you run the script log into your micro letter, plus the main group as a friend automatically pulled into the group, automatically accompany someone to talk to and so on, in short, can be used to achieve a variety of micro-channel personal number automation.

Reptile basics of 2.Python. For example, I wrote before requests using the library.

3.Python operation to date.

For installation wxpy libraries and library requests, can be quickly installed with pip3 install "library name."

1. Implement love time

That relationship is first set we were determined as an initial time, the current time using the time acquisition function, by calling datetime () library, the two values ​​subtracted.

datetime.date () function may convert user input to datetime type, datetime.datetiem.now () function reads the current time and the display format.

Finally the package is Cal_Date () function call returns to its number of days between the current time and the time difference is set.

def Cal_Date():
    First_Day_We_Loved = datetime.datetime(2013, 10, 26) Today = datetime.datetime.now() The_Day_We_Loved = Today - First_Day_We_Loved return The_Day_We_Loved.days 

2. Implement a daily

Had wanted to send a daily smile, but the search a bit, we all did, would like to get some different new things, to write a function, crawling on a daily Kingsoft one day one of the main content from movie clips, and then translate English into Chinese. Finally the package is get_news () function.

def get_news():
    #获取一个连接中的内容 url = "http://open.iciba.com/dsapi/" r = requests.get(url) print(r.json()) contents = r.json()['content'] translation = r.json()['translation'] return contents, translation 

3. The above-described function assembled into a main function

It should be noted that we must join exception handling, and I'm stuck here for a long time, how to adjust the code will not work, and finally a variety of online search, after the addition of exception handling it. All content ready to be sent can be added in this main function.

def send_news():
    if bot == None: login_WeChat() try: my_friend = bot.friends().search(u'stormwen')[0] # stormwen表示微信昵称 weekdic = {'Mon': '星期一', 'Tue': '星期二', 'Wed': '星期三', 'Thu': '星期四', 'Fri': '星期五', 'Sat': '星期六', 'Sun': '星期日'} date = time.strftime('%Y-%m-%d', time.localtime(time.time())) week = time.strftime('%a', time.localtime(time.time())) my_friend.send('今天是' + date + ' ' + weekdic[week] +',' + '是我们相恋的第' \ + str(Cal_Date()) + '天。'+ '\n ' +get_news()[0]+ '\n' + get_news()[1][5:]) my_friend.send(u"Darling:Good morning,I love you!!") t = Timer(86400, send_news) #86400是秒数:86400秒发送一次 t.start() except: print(u"失败!") 

4. Run results show

5. Summary

Today a small share of the project is ideal for zero-based learning Python, reptile school junior partner, the code is easy to understand.

这个项目可以扩展和优化的地方还有很多,比如,发送每日天气、每日笑话等等功能,而且用函数来实现这些功能,最后一起添加到总函数中,非常简单。大家可以先动手操作一下,后面我也会找时间,将这些功能一一分享出来。

---恢复内容结束---

Guess you like

Origin www.cnblogs.com/qingdeng123/p/11755408.html