Commits

Jan Guth committed 17f15f2

Save Game

Comments (0)

Files changed (1)

 import urllib.request as url
 import argparse
 import json
+import xml.etree.ElementTree as ET
 
 parser = argparse.ArgumentParser()
 parser.add_argument("-f", "--format", help="xml (default), json or csv", required=False)
-parser.add_argument("-ip", help="takes a single, multiple or a file as argument", required=True)
+parser.add_argument("-i", help="takes a single, multiple or a file as argument", required=True)
 parser.add_argument("-export", help="exports results to file", required=False)
 parser.add_argument("-v", "--version", help="print verion", required=False)
 args = parser.parse_args()
 
 format = args.format
 
+format = 'xml' #default
+
 if format == 'xml':
     format = "xml"
 elif format == 'json':
 else:
     print("Invalid format.")
  
-ip = args.ip
+ip = args.i
 
 # Greps geolocation intel from http://freegeoip.net/
 # by a GET request 
 
 f = url.urlopen("http://freegeoip.net/" + format + "/" + ip)
-#print(f.read())
+
+# XML declaration
+# Convert GET request from byte code 2 string
+b2s = f.read().decode("utf-8")
+tree = ET.fromstring(b2s)
+print(tree)
 
 def display():
-    '''Displays the results in a humand readable way'''
+    '''Displays the results in a human readable way'''
     global format
-    if format == 'xml':
-        b2s = f.read().decode("utf-8")
+    if format == 'xml':        
+        for elements in tree:
+            print(elements.tag, ":", elements.text)
+           
+display()
+
+def export():
+    filename = "geopy-results." + format
+    fobj = open(filename, 'w')
+    for elements in tree:
+        #fobj.write(ET.tostring(ET.Element(elements.tag), encoding="utf-8").decode('utf-8'))
+        #fobj.write(":")
+        fobj.write(ET.tostring(ET.Element(elements.text), encoding="utf-8").decode('utf-8'))
+        fobj.write("\n")
+    fobj.close()
+    
+export()
+            
+'''
     elif format == 'json':
         print(json.dumps(b2s, sort_keys=True, indent=4))
     elif format == 'csv':
         print(b2s)
-   
-display()
+'''
+