diff --git a/hackr/web.py b/hackr/web.py index a59773d..cb6ed67 100644 --- a/hackr/web.py +++ b/hackr/web.py @@ -1,7 +1,11 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +import json +from xml.etree.ElementTree import tostring + import requests +from xmljson import badgerfish as bf """ Defining this function as of now. Will come back to this later. @@ -12,12 +16,22 @@ def scrape(url, **kwargs): # type can be JSON as of now scraped_webpage = requests.get(url) return_type = kwargs['type'].lower() - if return_type != "json": + if return_type == "json": + try: + return scraped_webpage.json() + except ValueError: + return 'No JSON object could be decoded' + elif return_type == "xml": + try: + json_data = json.loads(scraped_webpage.content) + xml_data = "" + for element in bf.etree(json_data): + xml_data += tostring(element) + return xml_data + except ValueError: + return 'No XML could be decoded' + else: return scraped_webpage.text - try: - return scraped_webpage.json() - except ValueError: - return 'No JSON object could be decoded' def request(url, **kwargs): diff --git a/requirements.txt b/requirements.txt index b10068a..1fa5a48 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ Faker>=0.8.4, <0.9 requests>=2.18.4, <2.19 qrcode -pillow \ No newline at end of file +pillow +xmljson