Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python realizes the function of reminding lunar birthday

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Foreword:

One day I had a whim and wanted to realize a lunar birthday reminder. At this time, I have the following general ideas:

Get lunar calendar-- > match-- > reminder

It is found that the most important way to achieve this requirement is to obtain the lunar calendar, and the latter two are not a great challenge. After consulting some materials, it is found that the database can realize the function of changing solar calendar to lunar calendar. In this part of the database, I am a piece of cake, do not understand stored procedures. The balls hurt! Just give up and switch to other ideas. After thinking about it for a long time, I thought that I could crawl the ready-made calendar information through the crawler, and then match it in the repository. So, now the idea is as follows:

Climb to the lunar calendar repository-- > match name table-- > reminder

First, climb the calendar website

At first, we follow the idea of a table every year, and the code and comments are as follows:

The first is to create a table:

(file name: reptile\ CreateDb.py)

#-*-coding:utf-8-*-import MySQLdbimport os# writes sensitive information to environment variables through export to set valueMYSQLDB_HOST=os.environ.get ('MYSQLDB_HOST') MYSQLDB_USER=os.environ.get (' MYSQLDB_USER') MYSQLDB_PASSWD=os.environ.get ('MYSQLDB_PASSWD') db= MySQLdb.connect (host=MYSQLDB_HOST, port=3306, user=MYSQLDB_USER, passwd = MYSQLDB_PASSWD, db='Calendar', charset = "utf8" ) cursor = db.cursor () # Database insert def Insert_mysql (sql): cursor.execute (sql) db.commit () # Database query def Inquire_mysql (sql): cursor.execute (sql) request = cursor.fetchall () return requestif _ _ name__ = = "_ main__": st_sql = "show tables "cursor.execute (st_sql) request = cursor.fetchall () for year in range: if year in request: print" [*]% d is in database! " Else: print "[!]% s No in the Database,create now."% year ct_sql = "" CREATE TABLE `% d` (`DAY` date NOT NULL, `WEEK` varchar (50) NULL, `CONSTELLATON` varchar (50) NULL, `FESTIVAL` varchar (24) NULL, `YEAR` varchar (24) NULL `LUNARCALENDAR` varchar (60) NULL, `LUNNAR` varchar (24) NULL, `ERSHIBASU` varchar (24) NULL, `JIAZI` varchar (24) NULL, PRIMARY KEY (`DAY`) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 ""% year try: cursor.execute (ct_sql) db.commit () print "[.]% s OK!"% year except: print "Error:unable to fecth data" db.close ()

Effect:

Then crawl:

(file name: reptile\ Spider.py)

# coding:utf-8import re,urllib2from bs4 import BeautifulSoupfrom urllib import urlencodeclass SiteData: def _ _ init__ (self,url): self.Url = url def Data (self): # camouflage header values = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,p_w_picpath/w ebp,*/*;q=0.8',' Accept-Language':'zh-CN,zh QQ 0.8, 'User-Agent':'Mozilla/5.0 (Windows NT WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36'} SiteUrl = urllib2.Request (self.Url) Headers=values) WebSite = urllib2.urlopen (SiteUrl) HtmlData = WebSite.read () WebSite.close () return HtmlData def Find (self): # BeautifulSoup filter out the content I want HtmlData = self.Data () HtmlSoup = BeautifulSoup (HtmlData "html5lib") FindALL = HtmlSoup.find_all ("a") List= [] for i in FindALL: filter = i.encode ('gb2312'). Replace (' & # 160'). Replace (' ',') if "#" in filter: ReplaceText = filter.replace ('> ") Split = echo.split ('') if echo = =" exit ": print" Exit! " Break elif echo = "": pass # if the input information is correct Then run the method elif Split [0] = "add" or Split [0] = "del": try: a = run (Split [0], Split [1], Split [2], Split [3]) except: pass else: print "Please Use 'add' to Add user."

Send a reminder after matching

(file name: Run.py)

# coding:utf-8import time,json,smtplib,osfrom reptile.CreateDb import Insert_mysql,Inquire_mysqlfrom email.mime.text import MIMETextfrom email.MIMEMultipart import MIMEMultipartfrom email.MIMEBase import MIMEBase# today's date Today = time.strftime ('% YMY% MMI% dumbjinjinjitytime.localtime (time.time () # get today's IDToday_Sql = "SELECT Calendar.ID FROM `Calendar` WHERE TB_DAY='%s' from the database "% TodayTodayID = Inquire_mysql (Today_Sql) [0] [0] # this method is used to get today's date def Get_today (id): Sql =" SELECT Calendar.TB_DAY FROM `Calendar` WHERE ID='%s' "% id Today = Inquire_mysql (Sql) [0] [0] return Today# is used to get which birthday partners have today def Today_Birthday (id): Text = [] Today_Sql =" SELECT Calendar.LUNARCALENDAR FROM `Calendar` WHERE ID='%s' "% id TodayLunar = Inquire_mysql (Today_Sql) [0] [0] Today_Birthday_sql =" SELECT Tb_use.NAME FROM `Tb_ use` WHERE LUNARCALENDAR='%s' "% TodayLunar Today_Birthday = Inquire_mysql (Today_Birthday_sql) if str (Today_Birthday) = ='()': Text = [] else: for i in range (0 Len (Today_Birthday): Text.append (Today_ Birthday [I] [0]) if Text = []: Str = "" else: Str ='\ t'.join (i.encode ('utf-8') for i in Text) return Str# is used to get which birthdays def Week_Birthday (): Text = [] for item in range (TodayID) TodayID+7): if Today_Birthday (item) = = "": pass else: Text.append ("% s:% s"% (Get_today (item), Today_Birthday (item)) Str ='\ n'.join (i for i in Text) return Str # email alert def Mail (Subject Text): MAIL_USER=os.environ.get ('MAIL_USER') MAIL_PASSWD=os.environ.get (' MAIL_PASSWD') sender = 'cctv' receiver = [' send to @ 139.com'] subject = Subject smtpserver = 'smtp.mail.qq.com' msg = MIMEMultipart (' alternative') msg ['Subject'] = subject html = text part = MIMEText (html,'html') 'utf-8') msg.attach (part) smtp = smtplib.SMTP () smtp.connect (' smtp.mail.qq.com') smtp.login (MAIL_USER,MAIL_PASSWD) smtp.sendmail (sender,receiver,msg.as_string ()) smtp.quit () if _ _ name__ = ='_ _ main__': # get today's week a = time.localtime () Time = time.strftime ("% w" A) # tb_text = Today_Birthday (TodayID) for today's birthday # wb_text = Week_Birthday () # if someone has a birthday today, email if tb_text = =': pass else: Mail ('TodayBirthday',tb_text) # check it out once a week If someone sends an email on their birthday within the next 7 days, if Time = '1birthday: if wb_text = ='': pass else: Mail ('WeekBirthday',wb_text) else: pass

Finally, join the mission plan

(file name: Run.sh)

#! / bin/bashexport MYSQLDB_USER=****export MYSQLDB_PASSWD=****export MYSQLDB_HOST=****export MAIL_PASSWD=****export MAIL_USER=****# I am in a virtual environment, so run `/ home/ubuntu/class/env/bin/python / home/ubuntu/class/LunarBirthday/ Run.py` with the path of the virtual environment

Join the mission plan and you're done!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report