Skip to content

Commit

Permalink
Fix the issue where the displayed bus passages were duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
paraita committed Dec 20, 2016
1 parent 6047440 commit 221e5e9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
28 changes: 18 additions & 10 deletions billybob/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@


class BillyBob:
def wit_send(self, request, response):

@classmethod
def wit_send(cls, request, response):
print 'Getting from user... {0}'.format(request)
print 'Sending to user... {0}'.format(response['text'])

def _post_simple(self, channel, msg):
Expand All @@ -28,10 +31,12 @@ def _post_fmt(self, channel, text, attachments):
attachments=attachments,
as_user=True)

def _check_intent(self, intent, key_intent):
@classmethod
def _check_intent(cls, intent, key_intent):
return intent['entities'] and intent['entities'][key_intent]

def _fmt_prs(self, prs, repo):
@classmethod
def _fmt_prs(cls, prs, repo):
return [
{'text': '<{0}|{1}>: <{2}|{3}>'.format(p['url'],
p['repo_name'],
Expand All @@ -53,7 +58,7 @@ def wit_get_prs(self, params):
if repo == 'github':
prs = fetch_pr.get_prs('github')
prs_fmt = self._fmt_prs(prs, 'github')
self._post_fmt(chan, 'You asked for the PRs from github !', prs_fmt)
self._post_fmt(chan, 'Here are the open pull requests on Github !', prs_fmt)
elif repo == 'bitbucket':
prs = fetch_pr.get_prs('bitbucket')
prs_fmt = self._fmt_prs(prs, 'bitbucket')
Expand All @@ -79,25 +84,28 @@ def wit_get_bus_tt(self, params):
attachments = []
tz = gettz('Europe/Paris')
time_now = datetime.datetime.now(tz=tz)
attachment = {}

for passage in buses:
attachment = {}
bus_time = passage['bus_time']
dest = passage['dest']
diff_t = bus_time.replace(tzinfo=tz) - time_now
att_str = 'In {0} min (at {1})'.format(int(diff_t.total_seconds() / 60),
bus_time)
minutes = int(diff_t.total_seconds() / 60)
att_str = 'In {0} min (at {1}) towards {2}'.format(minutes,
bus_time.strftime("%H:%M"),
dest)
if passage['is_real_time']:
attachment['color'] = red_color
else:
attachment['color'] = green_color
attachment['text'] = att_str
attachment['fallback'] = att_str
attachments.append(attachment)
self._post_fmt(chan, "Here are the next bus passages !", attachments)
self._post_fmt(chan, "Here are the next bus passages cowboy !", attachments)
return {}

def __init__(self, slack_token=None, wit_token=None, **kargs):
self.BOT_NAME = 'BillyBob'
# TODO: get the BOT_ID at runtime
self.BOT_ID = 'U1ZUKEDT4'
if slack_token is None:
self.slack_token = os.environ['SLACK_API_TOKEN']
Expand Down Expand Up @@ -148,4 +156,4 @@ def start_service(self):
self.handle_command(command, chan)
time.sleep(1)
else:
print "Connection failed. Invalid Slack token or bot ID?"
print "Connection failed. Invalid tokens ?"
13 changes: 9 additions & 4 deletions billybob/fetch_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ def _get_prs_from_github():
r = requests.get(GITHUB_URL, params=GITHUB_QUERY)
json_response = r.json()
list_pr = json_response['items']
api_url = 'https://api.github.com/repos/'
repo_url_good = 'https://github.com/'
gh_url = 'https://github.com/'
return [
{
'url': pr['repository_url'],
'repo_name': pr['repository_url'].replace('https://api.github.com/repos/', ''),
'url': pr['repository_url'].replace(api_url, repo_url_good),
'repo_name': pr['repository_url'].replace(api_url, ''),
'title': pr['title'],
'pr_url': pr['pull_request']['url']
'pr_url': pr['pull_request']['url'].replace(api_url,
gh_url).replace('pulls',
'pull'),
}
for pr in list_pr
]
]


def _get_prs_from_bitbucket():
Expand Down

0 comments on commit 221e5e9

Please sign in to comment.