forked from jordsti/rsswatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTorrentWatcher.py
More file actions
160 lines (104 loc) · 4.26 KB
/
Copy pathTorrentWatcher.py
File metadata and controls
160 lines (104 loc) · 4.26 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'''
Created on 2013-02-04
@author: JordSti
'''
import RulesFile
import RssWatcher
import TorrentHistory
import os
import urllib2
import shutil
import time
import WatcherSetting
import FeedUrlBuilder
import TorrentUrlBuilder
import TorrentLauncher
import Logger
DEFAULT = 0
DEBUG = 1
class TorrentWatcher:
def __init__(self, runMode = DEFAULT, rulesPath = 'rules.txt'):
self.runMode = runMode
self.settings = WatcherSetting.WatcherSetting()
builder = FeedUrlBuilder.FeedUrlBuilder(self.settings.rssUrl, self.settings.username, self.settings.passkey)
self.rssUrl = builder.buildUrl()
self.tmpFolder = self.settings.tmpFolder
#self.torrentFolder = self.settings.torrentFolder
self.rulesPath = rulesPath
self.downloadUrl = self.settings.downloadUrl
self.sleepTime = self.settings.sleepTime
self.count = 0
self.log = Logger.LogFile()
launcher = TorrentLauncher.TorrentLauncher(self.settings)
self.launcher = launcher.launcher
#def authenticate(self):
#removing this
#auth = SceneTimeAuth.SceneTimeAuth()
#self.urlOpener = auth.authenticate(self.settings.username, self.settings.password)
def initFolder(self):
if not os.path.exists(self.tmpFolder):
os.mkdir(self.tmpFolder)
#if not os.path.exists(self.torrentFolder):
#os.mkdir(self.torrentFolder)
def loadFiles(self):
self.rules = RulesFile.RulesFile(self.rulesPath)
self.log.write('Rules Loaded')
if self.runMode == DEBUG:
self.printRules()
self.history = TorrentHistory.TorrentHistory()
self.history.load()
self.log.write('History Loaded')
if self.runMode == DEBUG:
self.printHistory()
def printHistory(self):
print "Torrent History :"
for h in self.history.ids:
print h
print "end Torrent History"
def printRules(self):
print "Rules :"
for rule in self.rules.rules:
print rule
print "end Rules"
def fetchRss(self):
self.rss = RssWatcher.RssWatcher(self.rssUrl)
self.rss.fetchRss()
self.rss.parseXml()
self.log.write('Rss Fetched')
def getTorrent(self, torrent):
print torrent.link
torrentFile = torrent.getLinkTitle()
#url = self.downloadUrl + str(torrent.id) + '/' + torrentFile
builder = TorrentUrlBuilder.TorrentUrlBuilder(torrent, self.settings.passkey)
url = builder.buildUrl()
print url
localPath = os.path.join(self.tmpFolder,torrentFile)
#dest = os.path.join(self.torrentFolder,torrentFile)
g = urllib2.urlopen(url).read()
#g = self.urlOpener.open(request).read()
f = open(localPath, "wb")
f.write(g)
f.close()
self.launcher.launch(localPath)
#shutil.copy(localPath, dest)
def watchTorrents(self):
for torrent in self.rss.torrents:
for rule in self.rules.rules:
if torrent.match(rule):
if not self.history.contains(torrent.id):
print torrent.title
self.getTorrent(torrent)
self.history.add(torrent.id)
self.history.save()
def watchLoop(self):
while True:
if self.settings.reloadRules:
#reloading rules
self.log.write("Rules reloaded")
self.rules = RulesFile.RulesFile(self.rulesPath)
self.fetchRss()
self.watchTorrents()
self.count = self.count + 1
self.log.write("Check #"+str(self.count)+ ", done !")
self.log.write("Going to sleep...")
time.sleep(self.sleepTime)