2014-05-26 02:29:22 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# This file is part of aDBa.
|
|
|
|
#
|
|
|
|
# aDBa is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# aDBa is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with aDBa. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from __future__ import with_statement
|
2014-07-15 02:55:52 -04:00
|
|
|
|
2014-05-26 02:29:22 -04:00
|
|
|
import hashlib
|
|
|
|
import os
|
|
|
|
import xml.etree.cElementTree as etree
|
2014-07-15 02:55:52 -04:00
|
|
|
import time
|
2014-05-26 02:29:22 -04:00
|
|
|
|
|
|
|
# http://www.radicand.org/blog/orz/2010/2/21/edonkey2000-hash-in-python/
|
2014-07-15 02:55:52 -04:00
|
|
|
import requests
|
|
|
|
|
|
|
|
|
2014-05-26 02:29:22 -04:00
|
|
|
def get_file_hash(filePath):
|
|
|
|
""" Returns the ed2k hash of a given file."""
|
|
|
|
if not filePath:
|
|
|
|
return None
|
|
|
|
md4 = hashlib.new('md4').copy
|
|
|
|
|
|
|
|
def gen(f):
|
|
|
|
while True:
|
|
|
|
x = f.read(9728000)
|
2014-07-15 02:55:52 -04:00
|
|
|
if x:
|
|
|
|
yield x
|
|
|
|
else:
|
|
|
|
return
|
2014-05-26 02:29:22 -04:00
|
|
|
|
|
|
|
def md4_hash(data):
|
|
|
|
m = md4()
|
|
|
|
m.update(data)
|
|
|
|
return m
|
|
|
|
|
|
|
|
with open(filePath, 'rb') as f:
|
|
|
|
a = gen(f)
|
|
|
|
hashes = [md4_hash(data).digest() for data in a]
|
|
|
|
if len(hashes) == 1:
|
|
|
|
return hashes[0].encode("hex")
|
2014-07-15 02:55:52 -04:00
|
|
|
else:
|
|
|
|
return md4_hash(reduce(lambda a, d: a + d, hashes, "")).hexdigest()
|
|
|
|
|
|
|
|
|
2014-05-26 02:29:22 -04:00
|
|
|
def get_file_size(path):
|
|
|
|
size = os.path.getsize(path)
|
|
|
|
return size
|
|
|
|
|
2014-07-15 03:17:08 -04:00
|
|
|
def _remove_file_failed(file):
|
|
|
|
try:
|
|
|
|
os.remove(file)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def download_file(url, filename):
|
|
|
|
try:
|
|
|
|
r = requests.get(url, stream=True, verify=False)
|
|
|
|
with open(filename, 'wb') as fp:
|
|
|
|
for chunk in r.iter_content(chunk_size=1024):
|
|
|
|
if chunk:
|
|
|
|
fp.write(chunk)
|
|
|
|
fp.flush()
|
|
|
|
|
|
|
|
except requests.HTTPError, e:
|
|
|
|
_remove_file_failed(filename)
|
|
|
|
return False
|
|
|
|
except requests.ConnectionError, e:
|
|
|
|
return False
|
|
|
|
except requests.Timeout, e:
|
|
|
|
return False
|
|
|
|
except Exception:
|
|
|
|
_remove_file_failed(filename)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2014-05-26 02:29:22 -04:00
|
|
|
|
2014-07-15 02:55:52 -04:00
|
|
|
def get_anime_titles_xml(path):
|
2014-07-15 03:17:08 -04:00
|
|
|
return download_file("https://raw.githubusercontent.com/ScudLee/anime-lists/master/animetitles.xml", path)
|
2014-07-15 02:55:52 -04:00
|
|
|
|
|
|
|
def get_anime_list_xml(path):
|
2014-07-15 03:17:08 -04:00
|
|
|
return download_file("https://raw.githubusercontent.com/ScudLee/anime-lists/master/anime-list.xml", path)
|
2014-07-15 02:55:52 -04:00
|
|
|
|
|
|
|
def read_anidb_xml(filePath=None):
|
2014-05-26 02:29:22 -04:00
|
|
|
if not filePath:
|
2014-07-15 02:55:52 -04:00
|
|
|
filePath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "animetitles.xml")
|
|
|
|
|
|
|
|
if not os.path.isfile(filePath):
|
2014-07-15 03:17:08 -04:00
|
|
|
if not get_anime_titles_xml(filePath):
|
|
|
|
return
|
2014-07-15 02:55:52 -04:00
|
|
|
else:
|
|
|
|
mtime = os.path.getmtime(filePath)
|
|
|
|
if time.time() > mtime + 24 * 60 * 60:
|
2014-07-15 03:17:08 -04:00
|
|
|
if not get_anime_titles_xml(filePath):
|
|
|
|
return
|
2014-07-15 02:55:52 -04:00
|
|
|
|
2014-05-26 02:29:22 -04:00
|
|
|
return read_xml_into_etree(filePath)
|
|
|
|
|
|
|
|
|
2014-07-15 02:55:52 -04:00
|
|
|
def read_tvdb_map_xml(filePath=None):
|
2014-05-26 02:29:22 -04:00
|
|
|
if not filePath:
|
2014-07-15 02:55:52 -04:00
|
|
|
filePath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "anime-list.xml")
|
|
|
|
|
|
|
|
if not os.path.isfile(filePath):
|
2014-07-15 03:17:08 -04:00
|
|
|
if not get_anime_list_xml(filePath):
|
|
|
|
return
|
2014-07-15 02:55:52 -04:00
|
|
|
else:
|
|
|
|
mtime = os.path.getmtime(filePath)
|
|
|
|
if time.time() > mtime + 24 * 60 * 60:
|
2014-07-15 03:17:08 -04:00
|
|
|
if not get_anime_list_xml(filePath):
|
|
|
|
return
|
2014-07-15 02:55:52 -04:00
|
|
|
|
2014-05-26 02:29:22 -04:00
|
|
|
return read_xml_into_etree(filePath)
|
|
|
|
|
|
|
|
|
|
|
|
def read_xml_into_etree(filePath):
|
2014-07-15 02:55:52 -04:00
|
|
|
if not filePath:
|
|
|
|
return None
|
|
|
|
|
|
|
|
with open(filePath, "r") as f:
|
|
|
|
return etree.ElementTree(file=f)
|