Cameron Yan commited on
Commit
d00753c
·
1 Parent(s): 507f40f

Add raw XML downloader and trasformer script.

Browse files
Files changed (1) hide show
  1. raw_xml_transformer.py +36 -0
raw_xml_transformer.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gzip
3
+ import requests
4
+ import xml.etree.ElementTree as ET
5
+ from datetime import datetime
6
+
7
+ os.makedirs('./tmp', exist_ok=True)
8
+
9
+ url = "https://anidb.net/api/anime-titles.xml.gz"
10
+
11
+ cache_file = './last_downloaded.txt'
12
+ if not os.path.exists(cache_file) or datetime.strptime(open(cache_file).read(), "%Y-%m-%d").date() != datetime.now().date():
13
+ response = requests.get(url, headers={
14
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
15
+ })
16
+
17
+ # write out the gzipped file
18
+ with open('./tmp/anime-titles.xml.gz', 'wb') as f:
19
+ f.write(response.content)
20
+
21
+ # update cache file
22
+ with open(cache_file, 'w') as f:
23
+ f.write(datetime.now().strftime("%Y-%m-%d"))
24
+
25
+ with gzip.open('./tmp/anime-titles.xml.gz', 'rb') as f:
26
+ file_content = f.read()
27
+ root = ET.fromstring(file_content)
28
+
29
+ with open('./raw/anime-titles.csv', 'w') as f:
30
+ for anime in root.findall('anime'):
31
+ aid = anime.get('aid')
32
+ for title in anime.findall('title'):
33
+ type_ = title.get('type')
34
+ lang = title.attrib['{http://www.w3.org/XML/1998/namespace}lang']
35
+ title_text = title.text.replace("|", "") # clean any pipes out of the title
36
+ f.write(f'{aid}|{type_}|{lang}|{title_text}\n')