bygdis.fi

git clone https://git.tarina.org/bygdis.fi
Log | Files | Refs | README

dailymail.py (3252B)


      1 #!/usr/bin/env python3
      2 
      3 import sqlite3
      4 from datetime import datetime, timedelta
      5 import subprocess
      6 import os
      7 
      8 def sendmail(email, subject, msg):
      9     #Send mail
     10     echomsg = subprocess.Popen(('echo', msg), stdout=subprocess.PIPE)
     11     sendmsg = subprocess.check_output(('mail', '-r', 'rob@bygdis.fi', '-s', subject, email, '-a', 'Content-Type: text/html;'), stdin=echomsg.stdout)
     12     echomsg.wait()
     13     #subprocess.call(['echo', msg, '|', 'mail', '-r', 'rob@tarina.org','-s', subject, email])
     14 
     15 basedir = os.path.dirname(os.path.realpath(__file__))
     16 
     17 conn = sqlite3.connect(basedir + '/db/bygdis.db')
     18 c = conn.cursor()
     19 
     20 c.execute('SELECT * FROM "bilder" ORDER BY "uploaddate" DESC')
     21 bilder = c.fetchall().copy()
     22 c.execute('SELECT * FROM "allaversioner" ORDER BY "lastmod" DESC')
     23 info = c.fetchall().copy()
     24 c.execute('SELECT * FROM "bildkommentar" ORDER BY "datumtid" DESC')
     25 comments = c.fetchall().copy()
     26 
     27 get_info = True
     28 get_bild = True
     29 get_comment = True
     30 msg = ''
     31 info
     32 commentdate = datetime.now() - timedelta(days=90)
     33 bilddate = datetime.now() - timedelta(days=90)
     34 infodate = datetime.now() - timedelta(days=90)
     35 for n in range(100):
     36     if get_comment == True:
     37         for k in comments:
     38             try:
     39                 commentdate = datetime.strptime(k[1], "%Y-%m-%d %H:%M:%S.%f")
     40             except:
     41                 commentdate = datetime.now() - timedelta(days=90)
     42             comments.pop(0)
     43             break
     44         else:
     45             commentdate = datetime.now() - timedelta(days=90)
     46     if get_bild == True:
     47         for b in bilder:
     48             imageurl = b[1] + '.jpeg'
     49             try:
     50                 bilddate = datetime.strptime(b[2], "%Y-%m-%d %H:%M:%S.%f")
     51             except:
     52                 bilddate = datetime.now() - timedelta(days=90)
     53             bilder.pop(0)
     54             break
     55     if get_info == True:
     56         for i in info:
     57             imageurl = i[1] + '.jpeg'
     58             try:
     59                 infodate = datetime.strptime(i[10], "%Y-%m-%d %H:%M:%S.%f")
     60             except:
     61                 infodate = datetime.now() - timedelta(days=90)
     62             info.pop(0)
     63             break
     64     if info == [] and bilder == [] and comments == []:
     65         break
     66     get_info = False
     67     get_bild = False
     68     get_comment = False
     69     d = max(infodate,bilddate,commentdate)
     70     daily = datetime.now() - timedelta(days=1)
     71     if d < daily:
     72         break
     73     if d == infodate:
     74         get_info = True
     75         try:
     76             msg += ("<p><a href='https://bygdis.fi/bild/"+i[1]+"'>"+i[10][:16] + " "+ i[9] +" lade till bildinfo " + i[3] + "</a></p>")
     77         except:
     78             pass
     79     if d == bilddate:
     80         get_bild = True
     81         try:
     82             msg += ("<p><a href='https://bygdis.fi/bild/"+b[1]+"'>"+ b[2][:16]+" "+b[7]+" lade till bild "+b[3]+"</a></p>")
     83         except:
     84             pass
     85     if d == commentdate:
     86         get_comment = True
     87         try:
     88             msg +=("<p><a href='https://bygdis.fi/bild/"+k[5]+"'>"+ k[1][:16]+" "+k[2]+" kommenterade "+k[3]+"</a></p>")
     89         except:
     90             pass
     91 print(msg)
     92 
     93 c.execute('SELECT * FROM bildadmin WHERE subscribe="dagligen"')
     94 users = c.fetchall().copy()
     95 print(users)
     96 print(msg)
     97 if msg != '':
     98     for u in users:
     99         sendmail(u[4], 'Va ha de häändi nu då?', msg)
    100 
    101 c.close()
    102