forked from niryariv/opentaba-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_poster.py
More file actions
84 lines (65 loc) · 2.87 KB
/
sync_poster.py
File metadata and controls
84 lines (65 loc) · 2.87 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
#!/usr/bin/python
# allow ourselves to import from the parent and current directory
import sys
sys.path.insert(0, '../')
sys.path.insert(0, '.')
import os
import datetime
from optparse import OptionParser
from time import sleep
import requests
from lib.conn import *
import lib.helpers as helpers
from lib.sociallib import post
# can't communicate with poster service without these
if not all(param in os.environ.keys() for param in ['POSTER_SERVICE_URL', 'POSTER_ID']):
print 'Environment variables POSTER_SERVICE_URL and POSTER_ID must both be set!'
exit(1)
parser = OptionParser()
parser.add_option('-m', dest='min_date', help='minimum date for plans to be sent to poster service. if not supplied, ALL plans will be sent. format: 1/1/2015')
parser.add_option('-q', dest='quiet', default=False, action='store_true', help='quiet, don\'t prompt for user approval')
parser.add_option('-d', dest='dont_wait', default=False, action='store_true', help='don\'t wait for poster service to post everything')
(options, args) = parser.parse_args()
if options.min_date:
# make sure the min_date parses fine
try:
min_date = datetime.datetime.strptime(options.min_date, '%d/%m/%Y')
except:
print 'Invalid minimum date. Format is 1/1/2015'
exit(1)
# build min_date query
plans_query = {'$or': [ {'year': {'$gt': min_date.year}}, {'year': min_date.year, 'month': {'$gt': min_date.month}}, {'year': min_date.year, 'month': min_date.month, 'day': {'$gte': min_date.day}} ]}
else:
# no query
plans_query = {}
# get valid plans
plans = helpers._get_plans(query=plans_query)
# if not quiet, make sure the user is ok with this
if not options.quiet:
while 1:
if not options.min_date:
sys.stdout.write('No minimum date was supplied.\nAre you sure you want ALL %s plans to be synced? [y/N] ' % len(plans))
else:
sys.stdout.write('Are you sure you want %s plans to be synced? [y/N] ' % len(plans))
choice = raw_input().lower()
if choice == 'n' or choice == 'no':
exit()
elif choice == 'y' or choice == 'yes':
break
print 'Posting plans... (may take up to a few minutes, depending on how many are sent)'
# reverse the list so that we send the service the earlier plans first (service's queue is fifo)
for plan in reversed(plans):
post(plan)
if not options.dont_wait:
status = 10
while status > 1:
# wait for 15 seconds then poke
sleep(15)
print 'Poking poster service for status...'
# get the /status page and parse the number in the output
r = requests.get('%s/status' % os.environ['POSTER_SERVICE_URL'].rstrip('/'))
for s in r.text.split():
if s.isdigit():
status = int(s)
print 'Approximately %s posts remaining...' % status
print 'Poster done!'