Created
September 24, 2017 19:02
-
-
Save ericzolf/2ba37153741b24a774820d7d390da3c8 to your computer and use it in GitHub Desktop.
Script to download book files from Oldenbourg Stars Ting according to IDs in file $ting/TBD.TXT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
""" | |
Script to download book files from Oldenbourg Stars Ting according to IDs in file $ting/TBD.TXT | |
Mostly stolen from https://forum.ubuntuusers.de/topic/synchronisation-von-ting-dem-hoerstift-von-bro/ | |
""" | |
import urllib | |
import os | |
import re | |
import hashlib | |
import commands | |
from sys import platform as _platform | |
# Check linux platform and find mountpoint | |
if _platform == "linux" or _platform == "linux2": | |
# Execute mount command | |
mount = commands.getoutput('mount -v') | |
lines = mount.split('\n') | |
points = map(lambda line: line.split()[2], lines) | |
# Defining standard mount point | |
MountedTingPath = "/media/TING/$ting" | |
# Check all found mount points | |
n = -1 | |
for a in enumerate(points): | |
if 'TING' in a[1]: | |
n =a[0] | |
MountedTingPath = a[1]+'/$ting' | |
print('Found Ting mounted to "'+MountedTingPath+'"') | |
if n ==-1: | |
print('Could not find mounted Ting. Please connect Ting to System.') | |
else: | |
print('Could not find Linux system!') | |
#set global parameters | |
TingURL = "system.ting.eu/book-files" | |
TingFileTypes = ["Thumb", "File", "Script"] | |
TingFileDestDict = {} | |
TingFileDestDict["Thumb"] = MountedTingPath + "/{}_en.png" | |
TingFileDestDict["File"] = MountedTingPath + "/{}_en.ouf" | |
TingFileDestDict["Script"] = MountedTingPath + "/{}_en.src" | |
TingFileSourceDict = {} | |
TingFileSourceDict["Thumb"] = "http://"+TingURL+"/get/id/{}/area/en/type/thumb" | |
TingFileSourceDict["File"] = "http://"+TingURL+"/get/id/{}/area/en/type/archive" | |
TingFileSourceDict["Script"] = "http://"+TingURL+"/get/id/{}/area/en/type/script" | |
def GetBookIDs(TBDFilePath=MountedTingPath+"/TBD.TXT"): | |
TBDFile = open(TBDFilePath, "r") | |
BookIDs = TBDFile.readlines() | |
for i, BookID in enumerate(BookIDs): | |
BookIDs[i] = BookID.strip() | |
return BookIDs | |
def CheckForBookDescriptionFile(BookIDAsString, TingPath=MountedTingPath): | |
BookExists = os.path.exists(TingPath+"/"+BookIDAsString+"_en.txt") | |
return BookExists | |
def GetBookMD5Sums(BookID, TingPath=MountedTingPath): | |
ResultDict = dict.fromkeys(TingFileTypes) | |
aFile = open(TingPath+"/"+BookID+"_en.txt") | |
Lines = aFile.readlines() | |
aFile.close() | |
for key in ResultDict.iterkeys(): | |
for Line in Lines: | |
Match = re.findall(key+"MD5: ([0-9,a-f]+)", Line) | |
if Match: | |
ResultDict[key] = Match[0] | |
break | |
return ResultDict | |
def CheckForBookFileValid(BookID, FileType, MD5SUM, FileDestDict=TingFileDestDict): | |
LocalFilePath = FileDestDict[FileType].format(BookID) | |
isValid = True | |
isValid = isValid and os.path.exists(LocalFilePath) | |
if isValid: | |
md5 = hashlib.md5() | |
File = open(LocalFilePath, "r") | |
md5.update(File.read()) | |
isValid = isValid and (MD5SUM == md5.hexdigest()) | |
print "Book: ", BookID, FileType, MD5SUM, isValid | |
return isValid | |
def GetBookFile(BookID, FileType, FileSourceDict=TingFileSourceDict, FileDestDict=TingFileDestDict): | |
LocalFilePath = FileDestDict[FileType].format(BookID) | |
FullURL = FileSourceDict[FileType].format(BookID) | |
print "Start Download from", FullURL | |
urllib.urlretrieve(FullURL, LocalFilePath) | |
return | |
def GetBookDescriptionFile(BookIDAsString, URL=TingURL, TingPath=MountedTingPath): | |
FullURL = "http://" | |
FullURL += URL | |
FullURL += "/get-description/id/" | |
FullURL += BookIDAsString | |
FullURL += "/area/en" | |
LocalFile = TingPath | |
LocalFile += "/" | |
LocalFile += BookIDAsString | |
LocalFile += "_en.txt" | |
urllib.urlretrieve(FullURL, LocalFile) | |
return | |
BookIDs = GetBookIDs() | |
print "Found followings BookIDs for processing:" | |
print BookIDs | |
for BookID in BookIDs: | |
if not(CheckForBookDescriptionFile(BookID)): | |
GetBookDescriptionFile(BookID) | |
MD5Sums = GetBookMD5Sums(BookID) | |
for key, value in MD5Sums.iteritems(): | |
if value != None: | |
if not(CheckForBookFileValid(BookID, key, value)): | |
GetBookFile(BookID, key) | |
if not(CheckForBookFileValid(BookID, key, value)): | |
print "Error: download failed for Book", BookID, key | |
print "Job done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment