POC Viewer for shipping that shows some status info and provides a url UPS only so far

This commit is contained in:
IamTheFij 2018-02-06 10:23:40 -08:00
parent 3085a45ac9
commit 03ceca1620
16 changed files with 226 additions and 73 deletions

View File

@ -19,13 +19,22 @@ services:
build: ./parsers/package-tracking build: ./parsers/package-tracking
ports: ports:
- "8183:3000" - "8183:3000"
viewer_package_tracking: viewer_main:
build: ./viewers/package-tracking build: ./viewers/main
volumes: volumes:
- ./viewers/package-tracking:/usr/src/app - ./viewers/main:/usr/src/app
links: links:
- indexer - indexer
- viewer_package_tracking
environment: environment:
INDEXER_URL: http://indexer:5000 INDEXER_URL: http://indexer:5000
ports: ports:
- "8184:5000" - "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"

View File

@ -1,9 +1,21 @@
require 'sinatra' require 'sinatra'
require 'tracking_number' require 'tracking_number'
set :bind, "0.0.0.0" set :bind, '0.0.0.0'
set :port, 3000 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 # Simple status endpoint on root
get '/' do get '/' do
'OK' 'OK'
@ -13,14 +25,15 @@ end
# Returns [{"token": "extracted token", "type": "token type", "metadata": {}] # Returns [{"token": "extracted token", "type": "token type", "metadata": {}]
post '/parse' do post '/parse' do
body = JSON.parse(request.body.read) body = JSON.parse(request.body.read)
trackers = TrackingNumber.search(body["message"]) trackers = TrackingNumber.search(body['message'])
results = [] results = []
for tracker in trackers do for tracker in trackers do
results.push({ results.push({
:token => tracker.tracking_number, :token => tracker.tracking_number,
:type => "SHIPPING", :type => 'SHIPPING',
:metadata => { :metadata => {
:carrier_name => tracker.courier_name, :carrier_name => tracker.courier_name,
:tracking_url => get_tracking_url(tracker),
} }
}) })
end end

8
viewers/main/Dockerfile Normal file
View File

@ -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

View File

@ -0,0 +1,10 @@
version: '2'
services:
main:
build: .
environment:
INDEXER_URL: http://localhost:8181
volumes:
- .:/usr/src/app
ports:
- "8184:5000"

View File

@ -25,8 +25,19 @@ def get_tokens():
params={'filter_type': 'SHIPPING'}, params={'filter_type': 'SHIPPING'},
) )
resp.raise_for_status() resp.raise_for_status()
print(resp.text, file=sys.stderr)
tokens = resp.json().get('tokens') 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) return flask.render_template('shipping.html', trackers=tokens)

View File

@ -0,0 +1,46 @@
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
{% block head %}
{% endblock %}
<title>Email Assistant - {% block title %}{% endblock %}</title>
</head>
<body>
<!-- As a heading -->
<nav class="navbar navbar-light bg-light navbar-expand-md">
<span class="navbar-brand mb-0 h1">Email Assistant</span>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
{% block navbar_nav %}
<li class="nav-item active">
<a class="nav-link" href="#">Home</a>
</li>
{% endblock %}
</ul>
</div>
</nav>
{% block body %}
<main class="container">
{% block container %}
{% endblock %}
</main>
{% endblock %}
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

View File

@ -0,0 +1,55 @@
{% extends "base.html" %}
{% block title %}Shipping{% endblock %}
{% block navbar_nav %}
<li class="nav-item active">
<a class="nav-link" href="/shipping">Shipping</a>
</li>
{% endblock %}
{% block container %}
<div class="row mt-3">
<div class="col">
<h2>Shipping Info</h2>
<p>Current tracking information from your email</p>
</div>
</div>
<div class="row mt-2">
<div class="col">
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Tracking Number</th>
<th scope="col">Carrier</th>
<th scope="col">Status</th>
<th scope="col">Location</th>
<th scope="col">Subject</th>
<th scope="col">Disabled</th>
</tr>
</thead>
<tbody>
{% for row in trackers %}
<tr>
<th scope="row">{{ row['id'] }}</th>
<td>
{% if row['metadata'].get('tracking_url') %}
<a href="{{ row['metadata'].get('tracking_url') }}">
{{ row['token'] }}
</a>
{% else %}
{{ row['token'] }}
{% endif %}
</td>
<td>{{ row['metadata']['carrier_name'] }}</td>
<td>{{ row['metadata'].get('events', [{}])[0].get('description', '') }}</td>
<td>{{ row['metadata'].get('events', [{}])[0].get('location', '') }}</td>
<td>{{ row['subject'] }}</td>
<td>{{ row['disabled'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}

View File

@ -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 RUN mkdir -p /src
CMD python -m viewer.main WORKDIR /src
COPY main.rb /src/
CMD ruby main.rb

View File

@ -2,9 +2,11 @@ version: '2'
services: services:
main: main:
build: . build: .
environment:
INDEXER_URL: http://localhost:8181
volumes: volumes:
- .:/usr/src/app - .:/src
environment:
UPS_KEY: ${UPS_KEY}
UPS_USER_ID: ${UPS_USER_ID}
UPS_PASSWORD: ${UPS_PASSWORD}
ports: ports:
- "8184:5000" - "8185:3000"

View File

@ -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

View File

@ -1,30 +0,0 @@
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
{% block head %}
{% endblock %}
<title>{% block title %}Email Assistant{% endblock %}</title>
</head>
<body>
{% block body %}
<div class="container">
{% block container %}
{% endblock %}
</div>
{% endblock %}
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

View File

@ -1,27 +0,0 @@
{% extends "base.html" %}
{% block title %}This is an example page{% endblock %}
{% block container %}
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Tracking Number</th>
<th scope="col">Carrier</th>
<th scope="col">Subject</th>
<th scope="col">Disabled</th>
</tr>
</thead>
<tbody>
{% for row in trackers %}
<tr>
<th scope="row">{{ row['id'] }}</th>
<td>{{ row['token'] }}</td>
<td>{{ row['metadata']['carrier_name'] }}</td>
<td>{{ row['subject'] }}</td>
<td>{{ row['disabled'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}