/
parse_and_store.py
64 lines (57 loc) · 2.28 KB
/
parse_and_store.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from lxml import etree
from simplejson import JSONEncoder
import requests
import json
import glob
def dumpLeaf(leaf,n=0):
for child in leaf:
if len(child) is 0 and child.text is not None:
print(n*" "+child.tag+":"+child.text)
else:
if len(child) > 0:
print(n*" "+child.tag)
dumpLeaf(child,n+1)
def encodeLeaf(leaf,structure):
for child in leaf:
if len(child) is 0 and child.text is not None:
if child.tag in structure.keys():
out = structure[child.tag]
#print("Tag available:"+child.tag,structure[child.tag])
if isinstance(out, list) is not True:
new_list = []
new_list.append(out)
structure[child.tag]=new_list
structure[child.tag].append(child.text)
#print("Tag updated:"+child.tag,structure[child.tag])
else:
structure[child.tag]=child.text
else:
if len(child) > 0:
new_structure = {}
encodeLeaf(child,new_structure)
if child.tag in structure.keys():
out = structure[child.tag]
if isinstance(out, list) is not True:
new_list = []
new_list.append(out)
structure[child.tag]=new_list
structure[child.tag].append(new_structure)
else:
structure[child.tag]=new_structure
def convert_MediaElchNFO_to_JSON(filename):
tree = etree.parse(filename)
root = tree.getroot()
#dumpLeaf(root)
movie = {}
encodeLeaf(root,movie)
return JSONEncoder().encode(movie)
if __name__ == '__main__':
url = 'http://testpythonbackend-testpython.44fs.preview.openshiftapps.com'
url = 'http://localhost:5000'
action = 'pushFilm'
#for f in glob.glob('/home/luc/Python/testPythonBackend/*.nfo'):
for f in glob.glob('./*.nfo'):
movie_json = convert_MediaElchNFO_to_JSON(f)
#print(movie_json)
response = requests.post(url+'/'+action, movie_json)
print("Importing "+json.loads(movie_json)['title']+" ---> "+response.json()['result'])