-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdater.py
executable file
·67 lines (53 loc) · 1.64 KB
/
updater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
import gzip
import tempfile
import sys
import os
import urllib2
from datetime import datetime
homeDirPath = os.path.expanduser("~")
logFilePath = os.path.join(homeDirPath, '.ipfilter-updater.log')
logFile = open(logFilePath, 'a')
def log(s):
dt = datetime.now()
str = dt.strftime("%Y-%m-%d %H:%M:%S")
print str + "> " + s
logFile.write(str + "> " + s + "\n")
logFile.flush()
os.fsync(logFile.fileno())
def download(tmpFile):
response = urllib2.urlopen('http://tbg.iblocklist.com/Lists/ipfilter.dat.gz')
ipfilterDataGz = response.read()
response.close()
os.write(tmpFile, ipfilterDataGz)
os.close(tmpFile)
def update():
log('Downloading ipfilter data...')
tmpFile, tmpFilePath = tempfile.mkstemp('.gz', 'ipfilter-updater-')
try:
download(tmpFile)
log('ipfilter data downloaded to '+tmpFilePath)
f = gzip.open(tmpFilePath, 'rb')
ipfilterData = f.read()
f.close()
finally:
os.remove(tmpFilePath)
uTorrentDirPath = os.path.join(homeDirPath, 'Library', \
'Application Support', \
'uTorrent')
added = False
if os.path.isdir(uTorrentDirPath):
targetFile = open(os.path.join(uTorrentDirPath, 'ipfilter.dat'),'wb')
targetFile.write(ipfilterData)
targetFile.close()
log('Added to uTorrent')
added = True
if not added:
log('WARNING: not added to any app')
def main():
log('---------Session started-----------')
update()
log('---------Session ended----------')
logFile.close()
if __name__ == '__main__':
main()