Last active
May 12, 2021 17:32
-
-
Save jhavard/7d738f328af6caed92200181895f0528 to your computer and use it in GitHub Desktop.
Prints USPS Informed Visibility data stored in mongodb, previous version used the raw json files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Now uses MongoDB | |
import os | |
import pathlib | |
import pprint | |
import json | |
import texttable | |
from pymongo import MongoClient | |
client = MongoClient() | |
db = client['mailthings'] | |
iv = db['ivmtr'] | |
for x in iv.find().sort('scanDatetime'): | |
try: | |
fstr = "%s %s %s %-11s %s %-31s %-24s %24s %2s %5s %s" | |
mid = x['imbMid'][6:9] | |
if (x['predictedDeliveryDate'] != None): | |
deldate = x['predictedDeliveryDate'][0:10] | |
else: | |
deldate = "" | |
if (x['mailPhase'] == None): | |
print(fstr % (x['scanDatetime'], mid, x['imbSerialNumber'], x['imbRoutingCode'], x['handlingEventType'], | |
"ZIP+4 Entry", "", "", "", "", deldate)) | |
else: | |
phase = x['mailPhase'].replace('Phase ', '').replace('Processing', 'Proc').replace('Destination', 'Dest').replace('Sequenced', 'Seq').replace('Sortation', 'Sort').replace('Cancellation', 'Cncl') | |
print(fstr % (x['scanDatetime'], mid, x['imbSerialNumber'], x['imbRoutingCode'], x['handlingEventType'], | |
phase, x['scanFacilityName'], x['scanFacilityCity'], x['scanFacilityState'], x['scanFacilityZip'], deldate)) | |
except Exception as e: | |
# pprint.pprint(x) | |
print("Exception:",e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
""" | |
Quick and dirty, but it gets the job done. | |
on my ingest host, they connect via sftp as user 'iv', which is all chrooted, so | |
the json files are initially saved in /home/iv/root/feed | |
From cron, I run this script and archive the ingested files like so: | |
0,30 * * * * ( cd /home/iv ; ./iv_feed_stuffer ; cd root/feed ; mv *.json ../archive 2> /dev/null ) | |
""" | |
import os | |
import pathlib | |
import pprint | |
import json | |
import texttable | |
import pymongo | |
client = pymongo.MongoClient() | |
db = client['mailthings'] | |
ivmtr = db['ivmtr'] | |
# go to the feed directory | |
os.chdir('/path/to/your/feed/files') | |
# load pathlib and then glob on *.json | |
pl = pathlib.PosixPath() | |
for jfile in pl.glob('*.json'): | |
try: | |
fp = jfile.open('r') | |
try: | |
data = json.load(fp) | |
except: | |
print("JSON LOAD FAILURE ON %s" % jfile) | |
data = [] | |
# stuff all data['events'] into mongo | |
ivmtr.insert_many(data['events']) | |
except Exception as e: | |
print("EXCEPTION ON %s: %s" % (jfile,e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment