-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimeXML.py
96 lines (74 loc) · 2.7 KB
/
AnimeXML.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#! /usr/bin/Python
# Import xml parser.
import xml.etree.ElementTree as ElementTree
# Import url library.
from urllib.request import urlopen
# Import sys library.
import sys
# XML to parse.
sampleUrl = "http://cdn.animenewsnetwork.com/encyclopedia/api.xml?anime="
# Get the number of params we have in our application.
params = len (sys.argv)
# Check the number of params we have.
if (params == 1):
print ("We need at least 1 anime identifier.")
else:
for aid in range (1, params):
# Read the xml as a file.
content = urlopen (sampleUrl + sys.argv[aid])
# XML content is stored here to start working on it.
xmlData = content.readall().decode('utf-8')
# Close the file.
content.close()
# Start parsing XML.
root = ElementTree.fromstring (xmlData)
# Extract classic data.
print ("General information")
print ("---------------------")
for info in root.iter("anime"):
print ("Id: " + info.get("id"))
print ("Gid: " + info.get("gid"))
print ("Name: " + info.get("name"))
print ("Precision: " + info.get("precision"))
print ("Type: " + info.get("type"))
# Extract date and general poster.
for info in root.iter ("info"):
if ("Vintage" in info.get("type")):
print ("Date: " + info.text)
if ("Picture" in info.get("type")):
print ("Poster: " + info.get("src"))
# Extract aditional posters.
for img in root.iter ("img"):
print ("Poster: " + img.get("src"))
print ("")
print ("Staff")
print ("---------------------")
# Extract all the staff of this anime.
for staff in root.getiterator ("staff"):
# Initialize values.
task = ""
value = {}
for elem in staff.getchildren():
if elem.tag == "task" :
task = elem.text
elif elem.tag == "person" :
tmp = elem.attrib
value["name"] = elem.text
if task :
print (task + ": " + elem.text)
print ("")
print ("Cast")
print ("---------------------")
# Extract cast data.
for cast in root.getiterator ("cast"):
# Initializce values.
role = ""
value = {}
for elem in cast.getchildren():
if elem.tag == "role" :
role = elem.text
elif elem.tag == "person" :
tmp = elem.attrib
value["name"] = elem.text
if role :
print (role + ": " + elem.text)