SickGear/lib/adba/aniDBfileInfo.py
echel0n 4a061d4dd2 AniDB now updates its xml lists daily.
Added writeback cache for both rss feeds and name parser.
2014-07-14 23:55:52 -07:00

107 lines
2.9 KiB
Python

#!/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
import hashlib
import os
import xml.etree.cElementTree as etree
import time
# http://www.radicand.org/blog/orz/2010/2/21/edonkey2000-hash-in-python/
import requests
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)
if x:
yield x
else:
return
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")
else:
return md4_hash(reduce(lambda a, d: a + d, hashes, "")).hexdigest()
def get_file_size(path):
size = os.path.getsize(path)
return size
def get_anime_titles_xml(path):
daily_dump = requests.get("http://raw.githubusercontent.com/ScudLee/anime-lists/master/animetitles.xml")
with open(path, "wb") as f:
f.write(daily_dump.content)
def get_anime_list_xml(path):
daily_dump = requests.get("http://raw.githubusercontent.com/ScudLee/anime-lists/master/anime-list.xml")
with open(path, "wb") as f:
f.write(daily_dump.content)
def read_anidb_xml(filePath=None):
if not filePath:
filePath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "animetitles.xml")
if not os.path.isfile(filePath):
get_anime_titles_xml(filePath)
else:
mtime = os.path.getmtime(filePath)
if time.time() > mtime + 24 * 60 * 60:
get_anime_titles_xml(filePath)
return read_xml_into_etree(filePath)
def read_tvdb_map_xml(filePath=None):
if not filePath:
filePath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "anime-list.xml")
if not os.path.isfile(filePath):
get_anime_list_xml(filePath)
else:
mtime = os.path.getmtime(filePath)
if time.time() > mtime + 24 * 60 * 60:
get_anime_list_xml(filePath)
return read_xml_into_etree(filePath)
def read_xml_into_etree(filePath):
if not filePath:
return None
with open(filePath, "r") as f:
return etree.ElementTree(file=f)