-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsingle_task.py
executable file
·82 lines (68 loc) · 2.26 KB
/
single_task.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# TinyBack - A tiny web scraper
# Copyright (C) 2012 David Triendl
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import sys
import time
import tinyback
import tinyback.tracker
username = tmp_dir = None
tracker = "http://urlteam.terrywri.st/"
for i, value in enumerate(sys.argv):
if i == 1:
username = value
elif i == 2:
tmp_dir = value
elif i == 3:
tracker = value
class StreamHandlerWithProgress(logging.StreamHandler):
"""For use with Seesaw to output progress"""
def __init__(self):
self._last_newline = True
logging.StreamHandler.__init__(self)
def emit(self, record):
if not getattr(record, 'progress', None):
if not self._last_newline:
sys.stdout.write('\n')
self._last_newline = True
return logging.StreamHandler.emit(self, record)
try:
self._last_newline = False
message = record.getMessage()
sys.stdout.write('\r ')
sys.stdout.write(message)
sys.stdout.flush()
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
handler = StreamHandlerWithProgress()
handler.setFormatter(logging.Formatter("%(asctime)s %(name)s %(levelname)s: %(message)s"))
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.INFO)
tracker = tinyback.tracker.Tracker(tracker)
try:
task = tracker.fetch()
except:
sys.exit(1)
if not task:
time.sleep(300)
sys.exit(0)
reaper = tinyback.Reaper(task, progress=True)
fileobj = reaper.run(tmp_dir)
tracker.put(task, fileobj, username)
fileobj.close()