import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import requests
from pprint import pprint
from bs4 import BeautifulSoup

from selenium import webdriver
import re
import json
from lxml import html

"""
Explanation: Working with JSON files
JSON is one of the most populat data storage types nowadays. To put in short, it is a collection of Python disctionaries inside a list.
End of explanation
"""


#example json file
my_json = [{"name":"Hrant","surname":"Davtyan"},
           {"name":"Davit","surname":"Abgaryan"}]

url = "http://api.open-notify.org/astros.json"

response = requests.get(url)
response.content

type(response.content)

# if website (API) provides not HTML but directly JSON,
# you can easily get that with requests
my_json = response.json()

pprint(my_json)

#get names from the JSON above
[my_json["people"][i]["name"] for i in range(len(my_json["people"]))]

#shorter code for getting names
[i["name"] for i in my_json["people"]]

#pandas can read JSON as well, however it loses the structure,
#if the JSON document is nested as show below
df = pd.read_json(url)

df

#saving JSON into file
with open("my_json.json","w") as f:
    json.dump(my_json,f,indent=4)

#reading JSON from a file
with open('my_json.json',"r") as f:
    data = json.load(f)

pprint(data)

#creating a string of dictionaires
json_str =   '''
{"message": "success",
"number": 6,
"people": [{"craft": "ISS", "name": "Oleg Artemyev"},
            {"craft": "ISS", "name": "Oleg Artemyev"}]}'''

str(my_json)

print(json_str)

#loading JSON from string
new_json = json.loads(json_str)

pprint(new_json)

"""
Explanation: JSON
End of explanation
"""


#sample XML document
my_xml = """
<person class="Dilijan">
    <name>Hrant</name>
    <surname>Davtyan</surname>
</person>
<person>
    <name>Davit</name>
    <surname>Abgaryan</surname>
</person>
"""

#the XML above would lokk like this if it was converted to JSON
xml_json = """
{"person":
     {"name":"Hrant",
      "surname":"Davtyan"}},
{"person":
     {"name":"Davit",
      "surname":"Abgaryan"}}
"""

tree = html.document_fromstring(my_xml)
type(tree)

#similar to get_text() in BS
tree.text_content().replace("\n","").strip()

#similar to find_all in BS
tree.xpath("//person/name")

my_names = [i.text_content() for i in tree.xpath("//person/name")]

print(my_names)

#similar to select() in BS
tree.cssselect("person")

tree.xpath("//person[@class='Dilijan']")[0].text_content()

tree.xpath("//person/@class")

"""
Explanation: XML
End of explanation
"""


