forked from gundambox/PttCrawler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschedule.py
171 lines (134 loc) · 5.26 KB
/
schedule.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
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
161
162
163
164
165
166
167
168
169
170
171
import os
import argparse
import sys
from datetime import datetime, timedelta
from enum import Enum
from typing import Dict, List
from crontab import CronTab
from utils import load_config, valid_datetime_type
class ScheduleAction(Enum):
update = 1
remove = 2
def __str__(self):
return self.name
@staticmethod
def from_string(s):
try:
return ScheduleAction[s]
except KeyError:
raise ValueError()
class CrawlerModule(Enum):
article_index = 1
article = 2
asn = 3
user = 4
def __str__(self):
return self.name
@staticmethod
def from_string(s):
try:
return CrawlerModule[s]
except KeyError:
raise ValueError()
class Platform(Enum):
windows = 1
linux = 2
mac = 3
class ScheduleHelper(object):
def __init__(self):
if sys.platform.startswith('linux'):
self.platform = Platform['linux']
elif sys.platform.startswith('win'):
self.platform = Platform['windows']
else:
self.platform = Platform['mac']
def _init_config(self, arguments: Dict):
config_path = (arguments['config_path']
if arguments['config_path']
else 'config.ini')
self.config = load_config(config_path)
def go(self, arguments: Dict):
self._init_config(arguments)
action = ScheduleAction.from_string(arguments['action'])
if self.platform == Platform.linux:
cron = CronTab(user=True)
cron.env['PATH'] = os.environ['PATH'] + ':' + os.getcwd()
crawler_module = arguments['crawler_module']
is_venv = arguments['virtualenv']
jobs = list(cron.find_command(str(crawler_module)))
cmd_args = arguments['args']
jobs = [job for job in jobs if cmd_args in (job.command)]
cwd = os.getcwd()
wrapper_path = os.path.join(cwd, 'env_wrapper.sh')
module_command = '{wrapper_path} "{cwd}" "{env}" -m crawler {module} {args} >/dev/null 2>&1'.format(
wrapper_path=wrapper_path,
cwd=cwd,
env=is_venv,
module=crawler_module,
args=str(cmd_args))
if len(jobs) > 0:
job = jobs[0]
job.set_command(module_command)
else:
job = cron.new(command=module_command)
if action == ScheduleAction.update:
start_datetime = arguments['start_datetime']
cycle_time = int(arguments['cycle_time'])
job.minute.on(start_datetime.minute)
job.hour.on(start_datetime.hour)
job.dom.every(cycle_time)
job.enable()
print(job)
elif action == ScheduleAction.remove:
cron.remove(job)
cron.write()
elif self.platform == Platform.windows:
raise NotImplementedError()
elif self.platform == Platform.mac:
raise NotImplementedError()
def parse_argument():
base_subparser = argparse.ArgumentParser(add_help=False)
base_subparser.add_argument('--verbose',
action='store_true',
help='Show more debug messages.')
base_subparser.add_argument('--config-path',
type=str,
help='Config ini file path.')
base_subparser.add_argument('--virtualenv',
metavar='VIRTUALENV_PATH',
type=str,
default='')
parser = argparse.ArgumentParser(parents=[base_subparser])
subparsers = parser.add_subparsers(dest='action',
help='cmd help')
subparsers.required = True
update_subparser = subparsers.add_parser('update')
update_subparser.add_argument(dest='crawler_module',
type=CrawlerModule.from_string,
choices=list(CrawlerModule))
update_subparser.add_argument('-c', '--cycle-time',
dest='cycle_time',
type=int,
required=True)
update_subparser.add_argument('-s', '--start-datetime',
dest='start_datetime',
type=valid_datetime_type,
default=datetime.now()+timedelta(minutes=1),
help='start datetime in format "YYYY-MM-DD HH:mm"')
update_subparser.add_argument('--args', type=str,
required=True)
remove_subparser = subparsers.add_parser('remove')
remove_subparser.add_argument(dest='crawler_module',
type=CrawlerModule.from_string,
choices=list(CrawlerModule))
remove_subparser.add_argument('--args', type=str,
required=True)
args = parser.parse_args()
arguments = vars(args)
return arguments
def main():
args = parse_argument()
helper = ScheduleHelper()
helper.go(args)
if __name__ == "__main__":
main()