Skip to content

Instantly share code, notes, and snippets.

@ideviant
Created April 20, 2015 15:56
Show Gist options
  • Save ideviant/8b136a91b6cb67288548 to your computer and use it in GitHub Desktop.
Save ideviant/8b136a91b6cb67288548 to your computer and use it in GitHub Desktop.

Revisions

  1. ideviant created this gist Apr 20, 2015.
    1 change: 1 addition & 0 deletions App_.idea_.name
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    App
    8 changes: 8 additions & 0 deletions App_.idea_App.iml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    <?xml version="1.0" encoding="UTF-8"?>
    <module type="PYTHON_MODULE" version="4">
    <component name="NewModuleRootManager">
    <content url="file://$MODULE_DIR$" />
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    </component>
    </module>
    6 changes: 6 additions & 0 deletions App_.idea_encodings.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    <?xml version="1.0" encoding="UTF-8"?>
    <project version="4">
    <component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false">
    <file url="PROJECT" charset="UTF-8" />
    </component>
    </project>
    4 changes: 4 additions & 0 deletions App_.idea_misc.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    <?xml version="1.0" encoding="UTF-8"?>
    <project version="4">
    <component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7.8 (/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7)" project-jdk-type="Python SDK" />
    </project>
    8 changes: 8 additions & 0 deletions App_.idea_modules.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    <?xml version="1.0" encoding="UTF-8"?>
    <project version="4">
    <component name="ProjectModuleManager">
    <modules>
    <module fileurl="file://$PROJECT_DIR$/.idea/App.iml" filepath="$PROJECT_DIR$/.idea/App.iml" />
    </modules>
    </component>
    </project>
    5 changes: 5 additions & 0 deletions App_.idea_scopes_scope_settings.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    <component name="DependencyValidationManager">
    <state>
    <option name="SKIP_IMPORT_STATEMENTS" value="false" />
    </state>
    </component>
    6 changes: 6 additions & 0 deletions App_.idea_vcs.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    <?xml version="1.0" encoding="UTF-8"?>
    <project version="4">
    <component name="VcsDirectoryMappings">
    <mapping directory="" vcs="" />
    </component>
    </project>
    208 changes: 208 additions & 0 deletions App_App.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,208 @@
    #coding=UTF-8
    __author__ = 'Kane'

    import urllib2
    import json
    import poplib
    import re
    import MySQLdb
    import sys
    import smtplib
    from email.mime.text import MIMEText


    reload(sys)
    sys.setdefaultencoding('utf-8')

    def getContent(appID):
    lookupUrl = 'https://itunes.apple.com/cn/lookup?id={0}'.format(appID)
    try:
    data = urllib2.urlopen(lookupUrl).read()
    except urllib2.URLError,e:
    print "Urlopen failed:",e
    jsonData = json.loads(data)
    if(jsonData['resultCount']):
    results = jsonData[r'results'][0]
    trackName = results['trackName']
    price = results['price']
    print price
    return trackName, price


    def receiveMail():
    try:
    p=poplib.POP3('pop.qq.com')
    p.user('[email protected]')
    p.pass_('Passwd123')
    ret = p.stat()
    except poplib.error_proto,e:
    print "Login failed:",e
    list=p.list()[1]
    print list[-1:][0]

    appUrl = ''
    userEmail = ''
    infoList = []
    emailNumFile = open('emailNum.txt')
    emailNum = int(emailNumFile.read())
    print emailNum
    emailNumFile.close()

    for item in list[emailNum:]:
    number,octets = item.split(' ')
    print number
    lines=p.retr(number)[1]
    for piece in lines:
    if piece.startswith('https://appsto.re/cn/'):
    appUrl = piece
    if piece.startswith('From: '):
    userEmail = piece[(piece.find('<')+1):(piece.rfind('>'))]
    if(appUrl and userEmail):
    print appUrl
    infoList.append(appUrl+'$$$'+userEmail)
    print 'Get a mail from : ' + userEmail
    appUrl = ''
    userEmail = ''

    emailNumFile = open('emailNum.txt','w')
    emailNum, emailOctets = list[-1:][0].split(' ')
    emailNumFile.write(emailNum)
    print emailNum
    emailNumFile.close()

    print '----------------Receive mail done----------------'
    return infoList

    def getAppId(url):
    try:
    data = urllib2.urlopen(url).read()
    except urllib2.URLError,e:
    print "Urlopen failed:",e
    reAppID = u'https://itunes.apple.com/(.*?)l=en&mt=8'
    appID = re.findall(reAppID, data)[0]
    return appID[(appID.rfind(u'/')+3):-1]

    def recordData():
    mailLists = list(set(receiveMail()))
    for mail in mailLists:
    appUrl, userEmail = mail.split('$$$')
    print appUrl
    print userEmail
    if(hasRecord(appUrl, userEmail)):
    print userEmail + ' and ' + appUrl + ' has record!'
    continue
    appId = getAppId(appUrl)
    trackName, price = getContent(appId)
    print trackName
    print '¥','%.2f'%price
    storeData(appUrl, appId, userEmail, trackName, price)
    print '------------------------'


    def storeData(appUrl, appId, userEmail, trackName, price):
    db=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test')
    cursor = db.cursor()
    sql = 'insert into app values ("{0}","{1}","{2}","{3}","{4}","{5}");'.format(appUrl, appId, userEmail,trackName, price, price)
    print sql
    try:
    cursor.execute(sql)
    db.commit()
    except:
    db.rollback()
    db.close()
    print 'Store data done!'

    def readData():
    db=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test')
    cursor = db.cursor()
    cursor.execute("select * from app")
    datas = cursor.fetchall()
    for data in datas:
    print data
    db.close()

    def getCurrentPrice(appID):
    lookupUrl = 'https://itunes.apple.com/cn/lookup?id={0}'.format(appID)
    try:
    data = urllib2.urlopen(lookupUrl).read()
    jsonData = json.loads(data)
    results = jsonData[r'results'][0]
    price = results['price']
    return price
    except urllib2.URLError,e:
    print "Urlopen failed:",e


    def updateCurrentPrice(appId, currentPrice):
    db=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test')
    cursor = db.cursor()
    # sql = 'update app set currentPrice="{0}" where appId="{1}"'.format(currentPrice, appId)
    sql = 'update app set currentPrice="{0}" where appId="{1}"'.format(currentPrice, appId)
    cursor.execute(sql)
    db.close()

    def updateBasePrice(appId,currentPrice):
    db=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test')
    cursor = db.cursor()
    sql = 'update app set price="{0}" where appId="{1}"'.format(currentPrice, appId)
    cursor.execute(sql)
    db.close()


    def updateData():
    db=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test')
    cursor = db.cursor()
    cursor.execute("select * from app")
    datas = cursor.fetchall()
    for data in datas:
    appId = data[1]
    currentPrice = getCurrentPrice(appId)
    updateCurrentPrice(appId, currentPrice)
    print data
    db.close()

    def checkDiscount():
    db=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test')
    cursor = db.cursor()
    cursor.execute("select * from app")
    datas = cursor.fetchall()
    for data in datas:
    print data
    if(data[4] > data[5]):
    sendEmail(data[2], '冰点:'+data[3],str(data[0])+ '\n原价:' + str(data[4]) +'\n现价:'+str(data[5]))
    print data[0] + ' Price changes!'
    elif(data[4] < data[5]):
    updateBasePrice(data[1],data[5])
    sendEmail('[email protected]', '涨价:'+data[3],str(data[0])+ '\n原价:' + str(data[4]) +'\n现价:'+str(data[5]))
    print data[0] + ' Price changes!'
    db.close()

    def sendEmail(to, subject, content):
    msg = MIMEText(content,'plain','utf-8')
    msg['Subject'] = subject #设置主题
    msg['From'] = '[email protected]' #发件人
    msg['To'] = to; #收件人
    try:
    s = smtplib.SMTP()
    s.connect('smtp.qq.com')
    s.login('nowiam','Passwd123')
    s.sendmail('[email protected]', to, msg.as_string())
    print 'Mail sent to:' + to
    s.close()
    except Exception, e:
    print str(e)

    def hasRecord(appUrl, userEmail):
    db=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test')
    cursor = db.cursor()
    sql = 'select COUNT(*) from app where appUrl="{0}" and userEmail="{1}"'.format(appUrl, userEmail)
    cursor.execute(sql)
    datas = cursor.fetchall()
    if(datas[0][0]):
    return True
    db.close()

    recordData()
    updateData()
    #readData()
    checkDiscount()
    1 change: 1 addition & 0 deletions App_emailNum.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    2681