|
| 1 | +#! /usr/bin/python |
| 2 | +# -*- python -*- |
| 3 | +# $Id: ngupdate,v 1.3 1998/04/10 08:06:55 tpc Exp tpc $ |
| 4 | +# |
| 5 | +# 1997.11.21 [email protected] add Expires header to newsgroups.html |
| 6 | +# 1997.11.20 [email protected] update newsgroups.html to show activity |
| 7 | +# 1998.04.02 [email protected] fix row matching regex so that it finds rows |
| 8 | +# with <FONT></FONT> in them |
| 9 | + |
| 10 | +import sys, os, string, time, cgi, regex |
| 11 | +from nntplib import NNTP |
| 12 | +from time import ctime, time, localtime, strftime, asctime, gmtime |
| 13 | +from backupfile import backupfile |
| 14 | + |
| 15 | +# number of seconds until the HTML page 'expires' |
| 16 | +valid_secs = 60*60*12 # 12 hours |
| 17 | + |
| 18 | +# news server authentication |
| 19 | +user = 'account' |
| 20 | +password = 'password' |
| 21 | + |
| 22 | +NEWS_SERVER = 'news.tpc.ml.org' |
| 23 | +#NEWS_SERVER = 'pengu.mww.dyn.ml.org' |
| 24 | +filenames = ('/home/tpc/www/tpc/newsgroups.html', |
| 25 | + '/home/tpc/www/member/newsgroups.html', |
| 26 | + '/home/tpc/www/exec/newsgroups.html') |
| 27 | +#filename = '/home/jwt/newsgroups.html' |
| 28 | + |
| 29 | +def do_update(filename): |
| 30 | + try: |
| 31 | + news = NNTP(NEWS_SERVER) |
| 32 | + except: |
| 33 | + print "Can not open news server:", NEWS_SERVER |
| 34 | + raise SystemExit |
| 35 | + |
| 36 | + try: |
| 37 | + resp = news.shortcmd('MODE READER') |
| 38 | + except: |
| 39 | + print "Can not communicate with server:", NEWS_SERVER |
| 40 | + raise SystemExit |
| 41 | + |
| 42 | + resp = news.shortcmd('authinfo user '+user) |
| 43 | + if resp[:3] != '381': |
| 44 | + raise SystemExit |
| 45 | + resp = news.shortcmd('authinfo pass '+password) |
| 46 | + if resp[:3] != '281': |
| 47 | + raise SystemExit |
| 48 | + |
| 49 | + (fin, fout) = backupfile(filename) |
| 50 | + |
| 51 | + nowsecs = time() |
| 52 | + newstime = strftime("%H%M%S", localtime(nowsecs)) |
| 53 | + datem1 = strftime("%y%m%d", localtime(nowsecs - (60*60*24))) |
| 54 | + datem7 = strftime("%y%m%d", localtime(nowsecs - (60*60*24*7))) |
| 55 | + datem21 = strftime("%y%m%d", localtime(nowsecs - (60*60*24*21))) |
| 56 | + |
| 57 | + # copy the page up to the table comment |
| 58 | + while 1: |
| 59 | + line = fin.readline() |
| 60 | + if line == '': break |
| 61 | + if string.find(line, 'HTTP-EQUIV="Expires"') >= 0: |
| 62 | + fout.write('<META HTTP-EQUIV="Expires" CONTENT="%s"\n' % asctime(gmtime(nowsecs + valid_secs))) |
| 63 | + else: |
| 64 | + fout.write(line) |
| 65 | + if string.find(line, "##newsgroups table") >= 0: break |
| 66 | + |
| 67 | + row = regex.symcomp('<TR><TD>.*</TD>\(<lead><TD>[^:]*://news.tpc.ml.org/\)\(<newsgroup>[^"]+\)\(<rest>.*\)') |
| 68 | + |
| 69 | + while 1: |
| 70 | + line = fin.readline() |
| 71 | + if line == '': break |
| 72 | + if string.find(line, "</TABLE>") >= 0: |
| 73 | + break |
| 74 | + |
| 75 | + if row.search(line) >= 0: |
| 76 | + newsgroup = row.group('newsgroup') |
| 77 | + idsm1 = 0 |
| 78 | + idsm7 = 0 |
| 79 | + idsm21 = 0 |
| 80 | + try: |
| 81 | + resp, idsm1 = news.newnews(newsgroup, datem1, newstime) |
| 82 | + resp, idsm7 = news.newnews(newsgroup, datem7, newstime) |
| 83 | + resp, idsm21 = news.newnews(newsgroup, datem21, newstime) |
| 84 | + except: |
| 85 | + print "no such newsgroup:", newsgroup |
| 86 | + |
| 87 | + fontm1 = "" |
| 88 | + fontm1e = "" |
| 89 | + fontm7 = "" |
| 90 | + fontm7e = "" |
| 91 | + if len(idsm1)>3: |
| 92 | + fontm1 = '<FONT COLOR="#FF0000">' |
| 93 | + fontm1e = '</FONT>' |
| 94 | + if len(idsm7)>10: |
| 95 | + fontm7 = '<FONT COLOR="#800000">' |
| 96 | + fontm7e = '</FONT>' |
| 97 | + line = "<TR><TD>%s%d%s/%s%d%s/%d</TD>%s%s%s\n" % (fontm1, len(idsm1), fontm1e, fontm7, len(idsm7), fontm7e, len(idsm21), row.group('lead'), newsgroup, row.group('rest')) |
| 98 | + fout.write(line) |
| 99 | + |
| 100 | + # close the table and indicate update time |
| 101 | + fout.write("</TABLE><P>Last updated: %s</P>\n" % ctime(time())) |
| 102 | + |
| 103 | + while 1: |
| 104 | + line = fin.readline() |
| 105 | + if line == '': break |
| 106 | + fout.write(line) |
| 107 | + |
| 108 | + fin.close() |
| 109 | + fout.close() |
| 110 | + |
| 111 | +for filename in filenames: |
| 112 | + do_update(filename) |
| 113 | + |
0 commit comments