Advertisement
Guest User

pfsense captive portal voucher ticket generator

a guest
Feb 2nd, 2013
1,329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os
  4. import sys
  5.  
  6. def csv_to_htm(filename, minutes):
  7.  
  8.     outfilename = filename.split('.')[0] + '.html'
  9.  
  10.     try:
  11.         csv_file = open(filename,"r")
  12.     except IOError as e:
  13.         if e.errno == 2:
  14.             print 'Unable to open file: ' + filename
  15.             sys.exit()
  16.  
  17.     vouchers = []
  18.  
  19.     for line in csv_file.readlines():
  20.         if line.find('#') != 0:
  21.             line = line.replace('"','')
  22.             line = line.replace(' ','')
  23.             line = line.replace('\n','')
  24.             vouchers.append(line)
  25.  
  26.     try:
  27.         html_file = open(outfilename,"w")
  28.     except Exception as e:
  29.         print e
  30.    
  31.     html_file.write('<html><head></head><body>\n')
  32.     html_file.write('<table border="1" cellpadding="15">\n')
  33.  
  34.     colcount = 0
  35.  
  36.     for code in vouchers:
  37.  
  38.         if colcount > 3:
  39.             html_file.write('</tr>\n')
  40.             colcount = 0
  41.  
  42.         if colcount == 0:
  43.             html_file.write('<tr>\n')
  44.  
  45.         html_file.write('<td><center>Internet Access Voucher<br><br>')
  46.         html_file.write('One Time Use Only!<br><br>')
  47.         html_file.write('Valid For ' + str(minutes) + ' Minutes<br><br><b>' + code + '</b></center></td>')
  48.    
  49.         colcount += 1
  50.    
  51.     html_file.write('</table></body></html>')
  52.  
  53. files = os.listdir('.')
  54. csv_files = []
  55. csv_durations = []
  56.  
  57. for item in files:
  58.     if item.endswith('.csv'):
  59.         csv_files.append(item)
  60.  
  61. for csv_file in csv_files:
  62.     valid_input = False
  63.     while valid_input != True:
  64.         minutes = raw_input('Enter the amount of time for vouchers in file ' + csv_file + ': ')
  65.         minutes = minutes.strip()
  66.         if minutes.isdigit():
  67.             csv_durations.append(minutes)
  68.             valid_input = True
  69.  
  70. for filename, minutes in zip(csv_files, csv_durations):
  71.     print 'Generating vouchers: ' + filename, minutes
  72.     csv_to_htm(filename, minutes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement