-
Notifications
You must be signed in to change notification settings - Fork 22
/
run.py
87 lines (73 loc) · 2.83 KB
/
run.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
# -*- coding: utf-8 -*-
from __future__ import print_function
import click
import os
import sys
import json
import first_timers as FT
def updateDB(all_issues, db_path):
"""Truncate and write the new list of issues in the DB."""
with open(db_path, 'w') as dbFile:
json.dump(FT.limit_issues(all_issues), dbFile, indent=2)
@click.command()
@click.option('--only-save',
is_flag=True,
help='Do not post any tweets, just populate the DB.')
@click.option('--db-path',
prompt='Database file',
default='data/db.json',
help='Old issues in a JSON list.')
@click.option('--create',
is_flag=True,
help='Pass if the DB file should be created.')
@click.option('--creds-path',
prompt='Credentials file',
default='',
help='File which contains Twitter account credentials. '
'Not needed if only saving to DB.')
@click.option('--debug',
is_flag=True,
help='Run in debug mode (does not tweet).')
def run(only_save, db_path, create, creds_path, debug):
dbExists = os.path.exists(db_path)
if not dbExists and not create:
click.secho('DB file does not exist and argument'
'--create was not passed.', err=True, fg='red')
sys.exit(-1)
elif dbExists and not create:
with open(db_path, 'rb') as dbFile:
old_issues = json.load(dbFile)
elif not dbExists and create:
old_issues = []
else:
click.secho('DB file exists but --create was passed.',
err=True, fg='red')
sys.exit(-1)
# Getting the latest list of issues from GitHub
new_issues = FT.get_first_timer_issues()
fresh_issues = FT.get_fresh(old_issues, new_issues)
all_issues = fresh_issues + old_issues
if not only_save:
try:
if not os.path.exists(creds_path):
print('Credentials file does not exist.', file=sys.stdout)
sys.exit(-1)
with open(creds_path, 'r') as credsFile:
creds = json.load(credsFile)
click.secho('Tweeting {} tweet(s).'.format(len(fresh_issues)))
for issue in fresh_issues:
click.secho('\t URL: ' + issue['url'], fg='blue')
tweets = FT.tweet_issues(fresh_issues, creds, debug)
for tweet in tweets:
if tweet['error'] is None:
click.secho('\t' + tweet['tweet'], fg='green')
else:
click.secho('\t' + tweet['tweet'], fg='red')
click.secho('\t\tError: ' + str(tweet['error']), fg='red')
except UnicodeEncodeError as e:
click.secho('Unable to post tweets because: ' + str(e), fg='red')
updateDB(all_issues, db_path)
if len(fresh_issues) > 0:
click.echo('Database updated.')
if __name__ == '__main__':
run()