Browse Source

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

imbox
IamTheFij 1 year ago
parent
commit
03ceca1620
16 changed files with 226 additions and 73 deletions
  1. +12
    -3
      docker-compose.yml
  2. +16
    -3
      parsers/package-tracking/main.rb
  3. +8
    -0
      viewers/main/Dockerfile
  4. +0
    -0
      viewers/main/Readme.md
  5. +10
    -0
      viewers/main/docker-compose.yml
  6. +0
    -0
      viewers/main/requirements-dev.txt
  7. +0
    -0
      viewers/main/requirements.txt
  8. +0
    -0
      viewers/main/viewer/__init__.py
  9. +12
    -1
      viewers/main/viewer/main.py
  10. +46
    -0
      viewers/main/viewer/templates/base.html
  11. +55
    -0
      viewers/main/viewer/templates/shipping.html
  12. +11
    -5
      viewers/package-tracking/Dockerfile
  13. +6
    -4
      viewers/package-tracking/docker-compose.yml
  14. +50
    -0
      viewers/package-tracking/main.rb
  15. +0
    -30
      viewers/package-tracking/viewer/templates/base.html
  16. +0
    -27
      viewers/package-tracking/viewer/templates/shipping.html

+ 12
- 3
docker-compose.yml View File

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

+ 16
- 3
parsers/package-tracking/main.rb View File

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

+ 8
- 0
viewers/main/Dockerfile 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

viewers/package-tracking/Readme.md → viewers/main/Readme.md View File


+ 10
- 0
viewers/main/docker-compose.yml View File

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

viewers/package-tracking/requirements-dev.txt → viewers/main/requirements-dev.txt View File


viewers/package-tracking/requirements.txt → viewers/main/requirements.txt View File


viewers/package-tracking/viewer/__init__.py → viewers/main/viewer/__init__.py View File


viewers/package-tracking/viewer/main.py → viewers/main/viewer/main.py View File

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



+ 46
- 0
viewers/main/viewer/templates/base.html 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>

+ 55
- 0
viewers/main/viewer/templates/shipping.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 %}

+ 11
- 5
viewers/package-tracking/Dockerfile 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
CMD python -m viewer.main
RUN mkdir -p /src
WORKDIR /src

COPY main.rb /src/

CMD ruby main.rb

+ 6
- 4
viewers/package-tracking/docker-compose.yml View File

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

+ 50
- 0
viewers/package-tracking/main.rb 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

+ 0
- 30
viewers/package-tracking/viewer/templates/base.html 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>

+ 0
- 27
viewers/package-tracking/viewer/templates/shipping.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 %}

Loading…
Cancel
Save