Hey all
Here is a small script that I wrote, mostly to learn Python. It is
designed to run from a cronjob after mythfilldatabase has run and
will attempt to pick out any "new" shows and e-mail them to you.
This is very handy for TV stations that broadcast an endless stream
of 'C.S.I.' and "Law and Order" reruns but drop in a couple of movies
every now and then.
The reason I post it here is that I would like to get some feedback
on it before I submit it to trac. Currently, all I know is that it
works for me. :-)
Specifically, I want to know:
* Does it work with any other listings grabber than my own?
* Do the MythWeb links work outside the GMT timezone?
* Is the seriesid a good indicator of whether a show is 'new'? I
could use the title and subtitle instead if it is preferred.
* The script creates a new table in 'mythconverg'. Is this OK? I
could create my own database but that seems like a bit of a waste for
a single table.
* Can anyone think of a better name? There is already a script in
contrib called mythmail but I really can't think of anything
descriptive.
run 'mythmail.py --help' for more info
Eggert
#!/usr/bin/python
# mythmail.py
# by Eggert Thorlacius (eggi at menandmice com)
#
# This script scans the MythTV databse for programs that it has not seen before
# and sends an e-mail message with details on those programs.
# Run this script from a cronjob after mythfilldatabase has run.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import MySQLdb
import sys
import smtplib
import getopt
import time;
def main():
# Defaults for parameters. Change these if you're bored with passing arguments
mail_server="localhost"
base_url="http://localhost/mythweb"
mail_to = "mythtv"
mail_from="mythtv"
mail_from_name="MythTV"
mail_subject="New programs in database"
db_host = "localhost"
db_user = "mythtv"
db_pass = "mythtv"
db_database="mythconverg"
db_table="mythmail"
chan_num=""
format="html"
sendmail = True
printout = False
updatedb = True
# The code itself. Don't modify anything below this line unless you really, really want to
def usage():
print """
mythmail.py
This script scans the MythTV databse for programs that it has not seen
before and sends an e-mail message with details on those programs.
Run this script from a cron-job after mythfilldatabase has run.
Options:
-i, --initial-run Initial run. Create table and store all programs
in database but dont't send any mail
-n, --no-update Don't update the database
-o, --printout Print the results to stdout instead of sending mail
-h, --help Prints this message
-c X,--channels=X Comma seperated list of channel ids. If ommitted,
look for programs on all channels.
-f X, --format=X Should mail be sent in text or html format?
Default is 'html'
-m X, --mailto=X Address to send mail to. Default is 'mythtv'
-b X, --baseurl=X Base URL for your MythWeb site (used in HTML mail)
Default is http://localhost/mythweb
-s X, --host=X Database host. Default is 'localhost'
-u X, --user=X Database user. Default is 'mythtv'
-p X, --pass=X Database password. Default is 'mythtv'
-d X, --database=X Database name. Default is 'mythconverg'
-t X, --table=X Database table. Default is 'mythmail'
"""
try:
opts, args = getopt.getopt(sys.argv[1:], "inohf:m:b:s:u:p:d:t:c:", ["initial-run", "no-update", "printout", "help", "format=", "mailto=", "baseurl=", "host=", "user=", "pass=", "database=", "table=", "channels="])
except getopt.GetoptError:
usage()
sys.exit(2)
create_db = False
for o, a in opts:
if o in ("-i", "--initial-run"):
sendmail = False
printout = False
create_db = True
if o in ("-o", "--printout"):
sendmail = False
printout = True
if o in ("-n", "--no-update"):
updatedb = False
if o in ("-h", "--help"):
usage()
sys.exit()
if o in ("-f", "--format"):
if (a in ("html", "text")):
format = a
else:
usage()
sys.exit(2)
if o in ("-m", "--mailto"):
mail_to = a
if o in ("-b", "--baseurl"):
base_url = a
if o in ("-s", "--host"):
db_host = a
if o in ("-u", "--user"):
db_user = a
if o in ("-p", "--pass"):
db_pass = a
if o in ("-d", "--database"):
db_database = a
if o in ("-t", "--table"):
db_table = a
if o in ("-c", "--channels"):
chan_num = a
# Done getting parameters, now let's do something useful
kSeriesID = 0
kChanID = 1
kStartTime = 2
kEndTime = 3
kTitle = 4
kSubtitle = 5
kChanName = 6
kDesc = 7
sql_statement = """
select mythconverg.program.seriesid, mythconverg.program.chanid,
mythconverg.program.starttime, mythconverg.program.endtime,
mythconverg.program.title, mythconverg.program.subtitle,
mythconverg.channel.name, mythconverg.program.description
from mythconverg.program LEFT JOIN mythconverg.channel ON mythconverg.program.chanid = mythconverg.channel.chanid
"""
where_clause = " where "
if (chan_num != "") :
where_clause += "( channel.channum = " + " OR channel.channum = ".join(chan_num.split(",")) + ") AND "
where_clause += "seriesid not in (select seriesid from " + db_database + "." + db_table + ")"
sql_statement += where_clause + " ORDER BY mythconverg.program.chanid, mythconverg.program.starttime";
conn = MySQLdb.connect (host = db_host,
user = db_user,
passwd = db_pass)
cursor = conn.cursor ()
if (create_db):
cursor.execute("CREATE TABLE IF NOT EXISTS " + db_database + "." + db_table + " (seriesid varchar(12) NOT NULL)")
cursor.execute (sql_statement)
rows = cursor.fetchall ()
output = ""
found_program = False
if(format=="html"):
output += "<HTML><HEAD></HEAD><body>";
for row in rows:
found_program = True
if (format=="html"):
subtitleAndDesc = ""
if (row[kSubtitle] != ""):
subtitleAndDesc = ": " + row[kSubtitle];
if (row[kDesc] != ""):
subtitleAndDesc += ": " + row[kDesc];
# URL format is http://my.host.com/mythweb/tv/detail/1001/1131531600
href = "%s/tv/detail/%u/%s" % (base_url, row[kChanID], int(time.mktime(row[kStartTime].timetuple())) )
# Old (pre 0.19 format) href = "%s/program_detail.php?chanid=%u&starttime=%s" % (base_url, row[kChanID], int(time.mktime(row[kStartTime].timetuple())) )
output += "%s %s:<BR>\n<A HREF=\"%s\">%s</A>%s<BR><BR>\n" % \
( str(row[kStartTime]), row[kChanName]
, href
, row[kTitle], subtitleAndDesc)
else:
output += row[kStartTime].strftime("%c") + " (" + row[kChanName] +"): " + row[kTitle] + ": " + row[kSubtitle] +"\n"
if (updatedb):
sql_statement = "INSERT INTO " + db_database + "." + db_table + " (seriesid) VALUES ( " + row[0] + ")"
cursor.execute(sql_statement);
if (format=="html"):
output +="</body></html>"
cursor.close ()
conn.close ()
if (found_program):
if (printout):
print output
if (sendmail):
server = smtplib.SMTP(mail_server)
header = "Mime-Version: 1.0\nContent-Type: text/html; charset=unicode-1-1-utf-8\n"
header += "From: " + mail_from + " (" + mail_from_name + ")\n"
header += "To: " + mail_to +"\n"
header += "Subject: " + mail_subject + "\n\n"
server.sendmail(mail_from, mail_to, header + output)
if __name__ == "__main__":
main()
_______________________________________________
mythtv-dev mailing list
[email protected]
http://mythtv.org/cgi-bin/mailman/listinfo/mythtv-dev