Advertisement
Guest User

Untitled

a guest
Jul 10th, 2013
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. from threading import Timer
  2. import xml.etree.ElementTree as ET
  3. import urllib
  4. import time
  5.  
  6. datetime = time.strftime('%Y-%m-%d %H.%M')
  7. filename = datetime + ".sql"
  8. url = "localhost/send.cgi"
  9. interval = 0.5
  10. table_name = "0001"
  11.  
  12. columns = "(time, foo, bar)"
  13.  
  14. # Past here nothing needs to be changed if you're using this. Well, some things need to be but whatever. c:
  15.  
  16. counting = 0  # Counting variable starts at 0 and ends at 59, at 60 is restarts.
  17.  
  18. def parser():
  19.     t = Timer(interval, parser)
  20.     global counting
  21.     global filename
  22.     query = open(filename, "a")  # Open fle for writing purposes.
  23.  
  24.     if counting == 0:
  25.         date = int(time.time()*1000)  # Get current time in milliseconds in a UNIX timestamp for SQL table.
  26.         web = urllib.urlopen(url)
  27.         tree = ET.parse(web)  # I categorize everything into a tree format and encode it so this module can understand.
  28.         root = tree.getroot()
  29.         foo = root[1].text.replace(" ", "")  # I get each variable by its number after the tree starts.
  30.         bar = root[2].text.replace(" ", "")  # Down there I plop in SQL syntax.
  31.         sql = "INSERT INTO %s %s VALUES ('%s', '%s', '%s');" % (table_name, columns, date, foo, bar)
  32.         counting += 1  # Counting variable goes up by one and then script continues onto the elif after.
  33.         query.write(sql)  # Write query to text file. This one has no \n but has a semicolon since it's first.
  34.         print counting  # For debugging code but I think it looks pretty.
  35.         t.start()  # Go back to the start of the parser and run again.
  36.     elif (counting > 0) and (counting < 59):
  37.         date = int(time.time()*1000)
  38.         web = urllib.urlopen(url)
  39.         tree = ET.parse(web)
  40.         root = tree.getroot()
  41.         foo = root[1].text.replace(" ", "")
  42.         bar = root[2].text.replace(" ", "")
  43.         sql = "\nINSERT INTO %s %s VALUES ('%s', '%s', '%s');" % (table_name, columns, date, foo, bar)
  44.         counting += 1
  45.         query.write(sql)
  46.         print counting
  47.         t.start()
  48.     elif counting == 59:
  49.         date = int(time.time()*1000)
  50.         web = urllib.urlopen(url)
  51.         tree = ET.parse(web)
  52.         root = tree.getroot()
  53.         foo = root[1].text.replace(" ", "")
  54.         bar = root[2].text.replace(" ", "")
  55.         sql = "\nINSERT INTO %s %s VALUES ('%s', '%s', '%s')" % (table_name, columns, date, foo, bar)
  56.         counting += 1
  57.         query.write(sql)
  58.         print counting
  59.         t.start()
  60.     elif counting == 60:
  61.         query.close()
  62.         datetime = time.strftime('%Y-%m-%d %H-%M')
  63.         filename = datetime + ".sql"
  64.         counting = 0
  65.         t.start()
  66. parser()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement