Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from threading import Timer
- import xml.etree.ElementTree as ET
- import urllib
- import time
- datetime = time.strftime('%Y-%m-%d %H.%M')
- filename = datetime + ".sql"
- url = "localhost/send.cgi"
- interval = 0.5
- table_name = "0001"
- columns = "(time, foo, bar)"
- # Past here nothing needs to be changed if you're using this. Well, some things need to be but whatever. c:
- counting = 0 # Counting variable starts at 0 and ends at 59, at 60 is restarts.
- def parser():
- t = Timer(interval, parser)
- global counting
- global filename
- query = open(filename, "a") # Open fle for writing purposes.
- if counting == 0:
- date = int(time.time()*1000) # Get current time in milliseconds in a UNIX timestamp for SQL table.
- web = urllib.urlopen(url)
- tree = ET.parse(web) # I categorize everything into a tree format and encode it so this module can understand.
- root = tree.getroot()
- foo = root[1].text.replace(" ", "") # I get each variable by its number after the tree starts.
- bar = root[2].text.replace(" ", "") # Down there I plop in SQL syntax.
- sql = "INSERT INTO %s %s VALUES ('%s', '%s', '%s');" % (table_name, columns, date, foo, bar)
- counting += 1 # Counting variable goes up by one and then script continues onto the elif after.
- query.write(sql) # Write query to text file. This one has no \n but has a semicolon since it's first.
- print counting # For debugging code but I think it looks pretty.
- t.start() # Go back to the start of the parser and run again.
- elif (counting > 0) and (counting < 59):
- date = int(time.time()*1000)
- web = urllib.urlopen(url)
- tree = ET.parse(web)
- root = tree.getroot()
- foo = root[1].text.replace(" ", "")
- bar = root[2].text.replace(" ", "")
- sql = "\nINSERT INTO %s %s VALUES ('%s', '%s', '%s');" % (table_name, columns, date, foo, bar)
- counting += 1
- query.write(sql)
- print counting
- t.start()
- elif counting == 59:
- date = int(time.time()*1000)
- web = urllib.urlopen(url)
- tree = ET.parse(web)
- root = tree.getroot()
- foo = root[1].text.replace(" ", "")
- bar = root[2].text.replace(" ", "")
- sql = "\nINSERT INTO %s %s VALUES ('%s', '%s', '%s')" % (table_name, columns, date, foo, bar)
- counting += 1
- query.write(sql)
- print counting
- t.start()
- elif counting == 60:
- query.close()
- datetime = time.strftime('%Y-%m-%d %H-%M')
- filename = datetime + ".sql"
- counting = 0
- t.start()
- parser()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement