various scripts for various tasks.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

opdsupdates.py 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ################################################################################
  4. import time
  5. import urllib2
  6. import re
  7. import smtplib
  8. def findstring(url):
  9. """find if document's last update date is equal to current date"""
  10. doc = urllib2.urlopen(url).read()
  11. doctime = re.search(r'Catalogue g&eacute;n&eacute;r&eacute; le (.*)<',
  12. doc)
  13. if doctime != None:
  14. print url.rstrip('\n') + " : "+doctime.group(1)
  15. date = re.match(r'(.*) (.*) (.*)',doctime.group(1))
  16. day = date.group(1)
  17. month = date.group(2)
  18. year = date.group(3)
  19. #day = re.match(r'\d (.*)',doctime.group(1))
  20. if day < 10:
  21. day = '0' + day
  22. months = { 'janv.' : '01',
  23. 'f&eacute;vr.' : '02',
  24. 'mars' : '03',
  25. 'avr.' : '04',
  26. 'mai' : '05',
  27. 'juin' : '06',
  28. 'juil.' : '07',
  29. 'aout' : '08',
  30. 'sept.' : '09',
  31. 'oct.' : '10',
  32. 'nov.' : '11',
  33. 'd&eacute;c' : '12'}
  34. for i,j in months.iteritems():
  35. month = month.replace(i,j)
  36. doctime = time.strptime(day + ' ' + month + ' ' + year,
  37. '%d %m %Y')[0:3]
  38. curtime = time.localtime()[0:3]
  39. if doctime == curtime:
  40. rooturl = re.match('(.*)/_catalog/(.*)',url)
  41. url = rooturl.group(1) + '/_catalog/index.html'
  42. sendmail('OPDS Update','catalog ' + url + ' updated!')
  43. return True
  44. else:
  45. return False
  46. else:
  47. sendmail('OPDS Error','string not found at catalog ' + url )
  48. def sendmail(subject,corpus):
  49. """send an alert e-mail"""
  50. fromaddr = "noreply@tourmentine.com"
  51. mto = "n@tourmentine.com"
  52. message = """From: No Reply <noreply@tourmentine.com>
  53. To: nico <n@tourmentine.com>
  54. Subject: """+subject+"""
  55. """+corpus+"""
  56. """
  57. smtp = smtplib.SMTP()
  58. smtp.connect()
  59. smtp.sendmail(fromaddr,mto,message)
  60. smtp.close()
  61. print time.strftime('%c') + ' : Starting harvest...\n'
  62. f = open('opdslist.txt')
  63. for line in f:
  64. if not re.match(r'^#',line):
  65. findstring(line)
  66. print '\n'