From 03ceca162074f4ca774108e89b0a4f6a74d9a9f7 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Tue, 6 Feb 2018 10:23:40 -0800 Subject: [PATCH] POC Viewer for shipping that shows some status info and provides a url UPS only so far --- docker-compose.yml | 15 ++++- parsers/package-tracking/main.rb | 19 ++++++- viewers/main/Dockerfile | 8 +++ viewers/{package-tracking => main}/Readme.md | 0 viewers/main/docker-compose.yml | 10 ++++ .../requirements-dev.txt | 0 .../requirements.txt | 0 .../viewer/__init__.py | 0 .../{package-tracking => main}/viewer/main.py | 13 ++++- viewers/main/viewer/templates/base.html | 46 ++++++++++++++++ viewers/main/viewer/templates/shipping.html | 55 +++++++++++++++++++ viewers/package-tracking/Dockerfile | 16 ++++-- viewers/package-tracking/docker-compose.yml | 10 ++-- viewers/package-tracking/main.rb | 50 +++++++++++++++++ .../viewer/templates/base.html | 30 ---------- .../viewer/templates/shipping.html | 27 --------- 16 files changed, 226 insertions(+), 73 deletions(-) create mode 100644 viewers/main/Dockerfile rename viewers/{package-tracking => main}/Readme.md (100%) create mode 100644 viewers/main/docker-compose.yml rename viewers/{package-tracking => main}/requirements-dev.txt (100%) rename viewers/{package-tracking => main}/requirements.txt (100%) rename viewers/{package-tracking => main}/viewer/__init__.py (100%) rename viewers/{package-tracking => main}/viewer/main.py (59%) create mode 100644 viewers/main/viewer/templates/base.html create mode 100644 viewers/main/viewer/templates/shipping.html create mode 100644 viewers/package-tracking/main.rb delete mode 100644 viewers/package-tracking/viewer/templates/base.html delete mode 100644 viewers/package-tracking/viewer/templates/shipping.html diff --git a/docker-compose.yml b/docker-compose.yml index 189042e..e7eeca3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,13 +19,22 @@ services: build: ./parsers/package-tracking ports: - "8183:3000" - viewer_package_tracking: - build: ./viewers/package-tracking + viewer_main: + build: ./viewers/main volumes: - - ./viewers/package-tracking:/usr/src/app + - ./viewers/main:/usr/src/app links: - indexer + - viewer_package_tracking environment: INDEXER_URL: http://indexer:5000 ports: - "8184:5000" + viewer_package_tracking: + build: ./viewers/package-tracking + environment: + UPS_KEY: ${UPS_KEY} + UPS_USER_ID: ${UPS_USER_ID} + UPS_PASSWORD: ${UPS_PASSWORD} + ports: + - "8185:3000" diff --git a/parsers/package-tracking/main.rb b/parsers/package-tracking/main.rb index e827568..1d8244e 100644 --- a/parsers/package-tracking/main.rb +++ b/parsers/package-tracking/main.rb @@ -1,9 +1,21 @@ require 'sinatra' require 'tracking_number' -set :bind, "0.0.0.0" +set :bind, '0.0.0.0' set :port, 3000 + +def get_tracking_url(tracker) + tracking_urls = { + :ups => 'https://wwwapps.ups.com/WebTracking/track?track=yes&trackNums=%s', + } + tracking_url = tracking_urls[tracker.courier_code] + if tracking_url != nil + tracking_url = tracking_url % tracker.tracking_number + end + return tracking_url +end + # Simple status endpoint on root get '/' do 'OK' @@ -13,14 +25,15 @@ end # Returns [{"token": "extracted token", "type": "token type", "metadata": {}] post '/parse' do body = JSON.parse(request.body.read) - trackers = TrackingNumber.search(body["message"]) + trackers = TrackingNumber.search(body['message']) results = [] for tracker in trackers do results.push({ :token => tracker.tracking_number, - :type => "SHIPPING", + :type => 'SHIPPING', :metadata => { :carrier_name => tracker.courier_name, + :tracking_url => get_tracking_url(tracker), } }) end diff --git a/viewers/main/Dockerfile b/viewers/main/Dockerfile new file mode 100644 index 0000000..9e2910d --- /dev/null +++ b/viewers/main/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.6-onbuild + +ENV FLASK_DEBUG=1 + +EXPOSE 5000 + +# TODO: Track debug in env and use threads +CMD python -m viewer.main diff --git a/viewers/package-tracking/Readme.md b/viewers/main/Readme.md similarity index 100% rename from viewers/package-tracking/Readme.md rename to viewers/main/Readme.md diff --git a/viewers/main/docker-compose.yml b/viewers/main/docker-compose.yml new file mode 100644 index 0000000..aeb038d --- /dev/null +++ b/viewers/main/docker-compose.yml @@ -0,0 +1,10 @@ +version: '2' +services: + main: + build: . + environment: + INDEXER_URL: http://localhost:8181 + volumes: + - .:/usr/src/app + ports: + - "8184:5000" diff --git a/viewers/package-tracking/requirements-dev.txt b/viewers/main/requirements-dev.txt similarity index 100% rename from viewers/package-tracking/requirements-dev.txt rename to viewers/main/requirements-dev.txt diff --git a/viewers/package-tracking/requirements.txt b/viewers/main/requirements.txt similarity index 100% rename from viewers/package-tracking/requirements.txt rename to viewers/main/requirements.txt diff --git a/viewers/package-tracking/viewer/__init__.py b/viewers/main/viewer/__init__.py similarity index 100% rename from viewers/package-tracking/viewer/__init__.py rename to viewers/main/viewer/__init__.py diff --git a/viewers/package-tracking/viewer/main.py b/viewers/main/viewer/main.py similarity index 59% rename from viewers/package-tracking/viewer/main.py rename to viewers/main/viewer/main.py index 0d4b9e9..995ed58 100644 --- a/viewers/package-tracking/viewer/main.py +++ b/viewers/main/viewer/main.py @@ -25,8 +25,19 @@ def get_tokens(): params={'filter_type': 'SHIPPING'}, ) resp.raise_for_status() - print(resp.text, file=sys.stderr) tokens = resp.json().get('tokens') + for token in tokens: + try: + resp = requests.get( + 'http://viewer_package_tracking:3000/info/'+token['token'] + ) + resp.raise_for_status() + print('Response: ', resp.text, file=sys.stderr) + info = resp.json() + token['metadata'].update(info) + except Exception as e: + print('Error', e, file=sys.stderr) + pass return flask.render_template('shipping.html', trackers=tokens) diff --git a/viewers/main/viewer/templates/base.html b/viewers/main/viewer/templates/base.html new file mode 100644 index 0000000..86029a0 --- /dev/null +++ b/viewers/main/viewer/templates/base.html @@ -0,0 +1,46 @@ + + + + + + + + + + + {% block head %} + {% endblock %} + + Email Assistant - {% block title %}{% endblock %} + + + + + {% block body %} +
+ {% block container %} + {% endblock %} +
+ {% endblock %} + + + + + + + + diff --git a/viewers/main/viewer/templates/shipping.html b/viewers/main/viewer/templates/shipping.html new file mode 100644 index 0000000..6ea7f34 --- /dev/null +++ b/viewers/main/viewer/templates/shipping.html @@ -0,0 +1,55 @@ +{% extends "base.html" %} +{% block title %}Shipping{% endblock %} + +{% block navbar_nav %} + +{% endblock %} + +{% block container %} +
+
+

Shipping Info

+

Current tracking information from your email

+
+
+
+
+ + + + + + + + + + + + + + {% for row in trackers %} + + + + + + + + + + {% endfor %} + +
#Tracking NumberCarrierStatusLocationSubjectDisabled
{{ row['id'] }} + {% if row['metadata'].get('tracking_url') %} + + {{ row['token'] }} + + {% else %} + {{ row['token'] }} + {% endif %} + {{ row['metadata']['carrier_name'] }}{{ row['metadata'].get('events', [{}])[0].get('description', '') }}{{ row['metadata'].get('events', [{}])[0].get('location', '') }}{{ row['subject'] }}{{ row['disabled'] }}
+
+
+{% endblock %} diff --git a/viewers/package-tracking/Dockerfile b/viewers/package-tracking/Dockerfile index 9e2910d..4177f90 100644 --- a/viewers/package-tracking/Dockerfile +++ b/viewers/package-tracking/Dockerfile @@ -1,8 +1,14 @@ -FROM python:3.6-onbuild +FROM ruby:2.5.0 -ENV FLASK_DEBUG=1 +# TODO: Move to Gemfile +RUN gem install trackerific -v 0.8.0 +RUN gem install sinatra -v 2.0 -EXPOSE 5000 +EXPOSE 3000 -# TODO: Track debug in env and use threads -CMD python -m viewer.main +RUN mkdir -p /src +WORKDIR /src + +COPY main.rb /src/ + +CMD ruby main.rb diff --git a/viewers/package-tracking/docker-compose.yml b/viewers/package-tracking/docker-compose.yml index aeb038d..ba6a747 100644 --- a/viewers/package-tracking/docker-compose.yml +++ b/viewers/package-tracking/docker-compose.yml @@ -2,9 +2,11 @@ version: '2' services: main: build: . - environment: - INDEXER_URL: http://localhost:8181 volumes: - - .:/usr/src/app + - .:/src + environment: + UPS_KEY: ${UPS_KEY} + UPS_USER_ID: ${UPS_USER_ID} + UPS_PASSWORD: ${UPS_PASSWORD} ports: - - "8184:5000" + - "8185:3000" diff --git a/viewers/package-tracking/main.rb b/viewers/package-tracking/main.rb new file mode 100644 index 0000000..658202f --- /dev/null +++ b/viewers/package-tracking/main.rb @@ -0,0 +1,50 @@ +require 'sinatra' +require 'trackerific' + +set :bind, "0.0.0.0" +set :port, 3000 + +Trackerific.configure do |config| + if [ + ENV['FEDEX_KEY'].present?, + ENV['FEDEX_PASSWORD'].present?, + ENV['FEDEX_ACCOUNT_NUMBER'].present?, + ENV['FEDEX_METER_NUMBER'].present?, + ].all? + config.fedex = { + key: ENV['FEDEX_KEY'], + password: ENV['FEDEX_PASSWORD'], + account_number: ENV['FEDEX_ACCOUNT_NUMBER'], + meter_number: ENV['FEDEX_METER_NUMBER'], + } + end + + if [ + ENV['UPS_KEY'].present?, + ENV['UPS_USER_ID'].present?, + ENV['UPS_PASSWORD'].present?, + ].all? + config.ups = { + key: ENV['UPS_KEY'], + user_id: ENV['UPS_USER_ID'], + password: ENV['UPS_PASSWORD'], + } + end + if [ + ENV['USPS_USER_ID'].present?, + ].all? + config.usps = { + user_id: ENV['USPS_USER_ID'], + } + end +end + +# Simple status endpoint on root +get '/' do + 'OK' +end + +get '/info/:token' do |token| + details = Trackerific.track(token) + details[0].to_json() +end diff --git a/viewers/package-tracking/viewer/templates/base.html b/viewers/package-tracking/viewer/templates/base.html deleted file mode 100644 index 44eaa88..0000000 --- a/viewers/package-tracking/viewer/templates/base.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - - - {% block head %} - {% endblock %} - - {% block title %}Email Assistant{% endblock %} - - - {% block body %} -
- {% block container %} - {% endblock %} -
- {% endblock %} - - - - - - - - diff --git a/viewers/package-tracking/viewer/templates/shipping.html b/viewers/package-tracking/viewer/templates/shipping.html deleted file mode 100644 index 34dc813..0000000 --- a/viewers/package-tracking/viewer/templates/shipping.html +++ /dev/null @@ -1,27 +0,0 @@ -{% extends "base.html" %} -{% block title %}This is an example page{% endblock %} - -{% block container %} - - - - - - - - - - - - {% for row in trackers %} - - - - - - - - {% endfor %} - -
#Tracking NumberCarrierSubjectDisabled
{{ row['id'] }}{{ row['token'] }}{{ row['metadata']['carrier_name'] }}{{ row['subject'] }}{{ row['disabled'] }}
-{% endblock %}