1 | #!/usr/bin/python -S |
---|
2 | # |
---|
3 | # python script to genereate a hash for a video file in MythVideo |
---|
4 | # |
---|
5 | # @url $$ |
---|
6 | # @date $$ |
---|
7 | # @version $$ |
---|
8 | # @author $$ |
---|
9 | # |
---|
10 | |
---|
11 | import os, struct, sys |
---|
12 | |
---|
13 | def hashFile(filename): |
---|
14 | '''Create metadata hash values for mythvideo files |
---|
15 | return a hash value |
---|
16 | return u'' if the was an error with the video file or the video file length was zero bytes |
---|
17 | ''' |
---|
18 | # generate a hash for a file on the local system |
---|
19 | # For original code: http://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes#Python |
---|
20 | try: |
---|
21 | longlongformat = 'q' # long long |
---|
22 | bytesize = struct.calcsize(longlongformat) |
---|
23 | f = open(filename, "rb") |
---|
24 | filesize = os.path.getsize(filename) |
---|
25 | hash = filesize |
---|
26 | if filesize < 65536 * 2: # Video file is too small |
---|
27 | return u'' |
---|
28 | for x in range(65536/bytesize): |
---|
29 | buffer = f.read(bytesize) |
---|
30 | (l_value,)= struct.unpack(longlongformat, buffer) |
---|
31 | hash += l_value |
---|
32 | hash = hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number |
---|
33 | f.seek(max(0,filesize-65536),0) |
---|
34 | for x in range(65536/bytesize): |
---|
35 | buffer = f.read(bytesize) |
---|
36 | (l_value,)= struct.unpack(longlongformat, buffer) |
---|
37 | hash += l_value |
---|
38 | hash = hash & 0xFFFFFFFFFFFFFFFF |
---|
39 | f.close() |
---|
40 | returnedhash = "%016x" % hash |
---|
41 | return returnedhash |
---|
42 | |
---|
43 | except(IOError): # Accessing to this video file caused and error |
---|
44 | return u'' |
---|
45 | # end hashFile() |
---|
46 | |
---|
47 | print hashFile(sys.argv[1]) |
---|