Advertisement
Heeshung

PlaneCatcherv1SMTP

Oct 6th, 2018
101
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.78 KB | None | 1 0
  1. #PlaneCatcher v1.0 created by Heeshung
  2.  
  3. #Aircraft registration calculation algorithms ported to Python from https://github.com/openskynetwork/dump1090-hptoa/blob/master/public_html/registra$
  4. #Algorithms licensed under GPL, v2 or later -  Copyright (C) 2012 by Salvatore Sanfilippo <[email protected]>
  5.  
  6. #Daemon script ported from https://web.archive.org/web/20160305151936/http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
  7.  
  8. import sys,time,smtplib
  9. from daemon import Daemon
  10. from email.mime.text import MIMEText
  11.  
  12. class catcher(Daemon):
  13.     def run(self):
  14.         def findcountry(hexcode):
  15.             countries=["DJ","SE","IR","ZW","GM","CH","IL","MZ","BF","TR","JO","ZA","ST",
  16.             "YU","LB","EG","DZ","CY","MY","LY","BS","IE","PH","MA","BB","IS","PK","TN",
  17.             "BZ","LU","SG","BW","CO","MT","LK","BI","CR","MC","SY","CM","CU","SM","CN",
  18.             "KM","SV","AU","CG","GT","AL","IN","CI","GY","HR","JP","GA","HT","LV","TH",
  19.             "ET","HN","LT","VN","GQ","VC","MD","YE","GH","JM","SK","BH","GN","NI","SI",
  20.             "BN","GW","PA","UZ","AE","LS","DO","UA","SB","KE","TT","BY","PG","LR","SR",
  21.             "EE","TW","MG","AG","MK","ID","MW","GD","BA","MV","MX","GE","MH","ML","VE",
  22.             "TJ","CK","MR","RU","AM","AS","MU","US","NE","NA","AZ","NA","ER","KG","CA",
  23.             "UG","TM","NZ","QA","IT","FJ","CF","ES","BT","NR","RW","FR","FM","LC","SN",
  24.             "DE","MN","TO","SC","UK","KZ","KI","SL","AT","PW","VU","SO","BE","AF","SZ",
  25.             "BG","BD","AR","SD","DK","MM","BR","TZ","FI","KW","CL","TD","GR","LA","EC",
  26.             "TG","HU","NP","PY","ZM","NO","OM","PE","CD","NL","KH","UY","AO","PL","SA",
  27.             "BO","BJ","PT","KR","CV","CZ","KP","DJ","RO","IQ"]
  28.  
  29.             hexranges=[0x098000,0x0983FF,0x4A8000,0x4AFFFF,0x730000,0x737FFF,0x004000,0x0043FF,0x09A000,0x09AFFF,0x4B0000,
  30.             0x4B7FFF,0x738000,0x73FFFF,0x006000,0x006FFF,0x09C000,0x09CFFF,0x4B8000,0x4BFFFF,0x740000,0x747FFF,0x008000,0x00FFFF,
  31.             0x09E000,0x09E3FF,0x4C0000,0x4C7FFF,0x748000,0x74FFFF,0x010000,0x017FFF,0x0A0000,0x0A7FFF,0x4C8000,0x4C83FF,0x750000,
  32.             0x757FFF,0x018000,0x01FFFF,0x0A8000,0x0A8FFF,0x4CA000,0x4CAFFF,0x758000,0x75FFFF,0x020000,0x027FFF,0x0AA000,0x0AA3FF,
  33.             0x4CC000,0x4CCFFF,0x760000,0x767FFF,0x028000,0x02FFFF,0x0AB000,0x0AB3FF,0x4D0000,0x4D03FF,0x768000,0x76FFFF,0x030000,
  34.             0x0303FF,0x0AC000,0x0ACFFF,0x4D2000,0x4D23FF,0x770000,0x777FFF,0x032000,0x032FFF,0x0AE000,0x0AEFFF,0x4D4000,0x4D43FF,
  35.             0x778000,0x77FFFF,0x034000,0x034FFF,0x0B0000,0x0B0FFF,0x500000,0x5003FF,0x780000,0x7BFFFF,0x035000,0x0353FF,0x0B2000,
  36.             0x0B2FFF,0x7C0000,0x7FFFFF,0x036000,0x036FFF,0x0B4000,0x0B4FFF,0x501000,0x5013FF,0x800000,0x83FFFF,0x038000,0x038FFF,
  37.             0x0B6000,0x0B6FFF,0x501C00,0x501FFF,0x840000,0x87FFFF,0x03E000,0x03EFFF,0x0B8000,0x0B8FFF,0x502C00,0x502FFF,0x880000,
  38.             0x887FFF,0x040000,0x040FFF,0x0BA000,0x0BAFFF,0x503C00,0x503FFF,0x888000,0x88FFFF,0x042000,0x042FFF,0x0BC000,0x0BC3FF,
  39.             0x504C00,0x504FFF,0x890000,0x890FFF,0x044000,0x044FFF,0x0BE000,0x0BEFFF,0x505C00,0x505FFF,0x894000,0x894FFF,0x046000,
  40.             0x046FFF,0x0C0000,0x0C0FFF,0x506C00,0x506FFF,0x895000,0x8953FF,0x048000,0x0483FF,0x0C2000,0x0C2FFF,0x507C00,0x507FFF,
  41.             0x896000,0x896FFF,0x04A000,0x04A3FF,0x0C4000,0x0C4FFF,0x508000,0x50FFFF,0x897000,0x8973FF,0x04C000,0x04CFFF,0x0C6000,
  42.             0x0C6FFF,0x510000,0x5103FF,0x898000,0x898FFF,0x050000,0x050FFF,0x0C8000,0x0C8FFF,0x511000,0x5113FF,0x899000,0x8993FF,
  43.             0x054000,0x054FFF,0x0CA000,0x0CA3FF,0x512000,0x5123FF,0x8A0000,0x8A7FFF,0x058000,0x058FFF,0x0CC000,0x0CC3FF,0x513000,
  44.             0x5133FF,0x05A000,0x05A3FF,0x0D0000,0x0D7FFF,0x514000,0x5143FF,0x900000,0x9003FF,0x05C000,0x05CFFF,0x0D8000,0x0DFFFF,
  45.             0x515000,0x5153FF,0x901000,0x9013FF,0x05E000,0x05E3FF,0x100000,0x1FFFFF,0x600000,0x6003FF,0x902000,0x9023FF,0x060000,
  46.             0x0603FF,0xA00000,0xADF7C8,0x062000,0x062FFF,0x201000,0x2013FF,0x600800,0x600BFF,0x064000,0x064FFF,0x202000,0x2023FF,
  47.             0x601000,0x6013FF,0xC00000,0xC0CDF9,0x068000,0x068FFF,0x601800,0x601BFF,0xC80000,0xC87FFF,0x06A000,0x06A3FF,0x300000,
  48.             0x33FFFF,0xC88000,0xC88FFF,0x06C000,0x06CFFF,0x340000,0x37FFFF,0x680000,0x6803FF,0xC8A000,0xC8A3FF,0x06E000,0x06EFFF,
  49.             0x380000,0x3BFFFF,0x681000,0x6813FF,0xC8C000,0xC8C3FF,0x070000,0x070FFF,0x3C0000,0x3FFFFF,0x682000,0x6823FF,0xC8D000,
  50.             0xC8D3FF,0x074000,0x0743FF,0x400000,0x43FFFF,0x683000,0x6833FF,0xC8E000,0xC8E3FF,0x076000,0x0763FF,0x440000,0x447FFF,
  51.             0x684000,0x6843FF,0xC90000,0xC903FF,0x078000,0x078FFF,0x448000,0x44FFFF,0x700000,0x700FFF,0x07A000,0x07A3FF,0x450000,
  52.             0x457FFF,0x702000,0x702FFF,0xE00000,0xE3FFFF,0x07C000,0x07CFFF,0x458000,0x45FFFF,0x704000,0x704FFF,0xE40000,0xE7FFFF,
  53.             0x080000,0x080FFF,0x460000,0x467FFF,0x706000,0x706FFF,0xE80000,0xE80FFF,0x084000,0x084FFF,0x468000,0x46FFFF,0x708000,
  54.             0x708FFF,0xE84000,0xE84FFF,0x088000,0x088FFF,0x470000,0x477FFF,0x70A000,0x70AFFF,0xE88000,0xE88FFF,0x08A000,0x08AFFF,
  55.             0x478000,0x47FFFF,0x70C000,0x70C3FF,0xE8C000,0xE8CFFF,0x08C000,0x08CFFF,0x480000,0x487FFF,0x70E000,0x70EFFF,0xE90000,
  56.             0xE90FFF,0x090000,0x090FFF,0x488000,0x48FFFF,0x710000,0x717FFF,0xE94000,0xE94FFF,0x094000,0x0943FF,0x490000,0x497FFF,
  57.             0x718000,0x71FFFF,0x096000,0x0963FF,0x498000,0x49FFFF,0x720000,0x727FFF,0x098000,0x0983FF,0x4A0000,0x4A7FFF,0x728000,
  58.             0x72FFFF]
  59.  
  60.             for a in range(0,len(hexranges),2):
  61.                 if (hexranges[a]<hexcode<hexranges[a+1]):
  62.                     return countries[a/2]
  63.             return "nocountry"
  64.  
  65.         def hexdecoder(hexcode):
  66.             setzs=[0x008011,676,26,"ZS-",0,0x00c4b8]
  67.             setfg=[0x390000,1024,32,"F-G",0,0x396739]
  68.             setfh=[0x398000,1024,32,"F-H",0,0x39e739]
  69.             setdaa=[0x3c4421,1024,32,"D-A",0,0x3c7f5a]
  70.             setdab=[0x3c0001,676,26,"D-A",10140,0x3c1d0c]
  71.             setdba=[0x3c8421,1024,32,"D-B",0,0x3cbf5a]
  72.             setdbb=[0x3c2001,676,26,"D-B",10140,0x3c3d0c]
  73.             setdc=[0x3cc000,676,26,"D-C",0,0x3d04a7]
  74.             setde=[0x3d04a8,676,26,"D-E",0,0x3d494f]
  75.             setdf=[0x3d4950,676,26,"D-F",0,0x3d8df7]
  76.             setdg=[0x3d8df8,676,26,"D-G",0,0x3dd29f]
  77.             setdh=[0x3dd2a0,676,26,"D-H",0,0x3e1747]
  78.             setdi=[0x3e1748,676,26,"D-I",0,0x3e5bef]
  79.             setoo=[0x448421,1024,32,"OO-",0,0x44eb5a]
  80.             setcf=[0xc00001,676,26,"C-F",0,0xc044a8]
  81.             setcg=[0xc044a9,676,26,"C-G",0,0xc08950]
  82.             setci=[0xc08951,676,26,"C-I",0,0xc0cdf8]
  83.             setoy=[0x458421,1024,32,"OY-",0,0x45eb5a]
  84.             setoh=[0x460000,676,26,"OH-",0,0x4644a7]
  85.             setsx=[0x468421,1024,32,"SX-",0,0x46eb5a]
  86.             setcs=[0x490421,1024,32,"CS-",0,0x496b5a]
  87.             setyr=[0x4a0421,1024,32,"YR-",0,0x4a6b5a]
  88.             settc=[0x4b8421,1024,32,"TC-",0,0x4beb5a]
  89.             setjy=[0x740421,1024,32,"JY-",0,0x746b5a]
  90.             setap=[0x760421,1024,32,"AP-",0,0x766b5a]
  91.             set9v=[0x768421,1024,32,"9V-",0,0x76eb5a]
  92.             setyk=[0x778421,1024,32,"YK-",0,0x77eb5a]
  93.             setvh=[0x7c0000,1296,36,"VH-",0,0x7c822d]
  94.             setlv=[0xe01041,4096,64,"LV-",0,0xe1a69a]
  95.  
  96.  
  97.             mappings=[setzs,setfg,setfh,setdaa,setdab,setdba,setdbb,setdc,setde,setdf,setdg,setdh,setdi,setoo,setcf,setcg,setci,setoy,setoh,
  98.             setsx,setcs,setyr,settc,setjy,setap,set9v,setyk,setvh,setlv]
  99.  
  100.             #regrange order: N-Number,RA,CU,HL1,HL2,HL3,JA
  101.             regrange=[0xa00001,0xadf7c7,0x140000,0x1586a0,0x0b03e8,0x0b07d0,0x71ba00,0x71bf99,0x71c000,0x71c099,0x71c200,0x71c299,0x840001,0x8781cf]
  102.  
  103.             if (regrange[0] <= hexcode <= regrange[1]):
  104.  
  105.                 alphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZ'
  106.  
  107.                 offset = hexcode-10485761
  108.  
  109.                 reg="N"
  110.  
  111.                 dig1=int(offset/101711)+1
  112.                 reg+=str(dig1)
  113.                 offset = offset%101711
  114.  
  115.                 if (offset>600):
  116.                     offset-=601
  117.                     dig2=int(offset/10111)
  118.                     reg+=str(dig2)
  119.                     offset = offset%10111
  120.  
  121.  
  122.                 if (offset>600):
  123.                     offset-=601
  124.                     dig3=int(offset/951)
  125.                     reg+=str(dig3)
  126.                     offset=offset%951
  127.  
  128.  
  129.  
  130.                 if (offset>600):
  131.                     offset-=601
  132.                     dig4=int(offset/35)
  133.                     reg+=str(int(round(dig4)))
  134.                     offset=offset%35
  135.                     if (offset>0):
  136.                         if (offset<=24):
  137.                             reg+=alphabet[offset-1]
  138.                         else:
  139.                             offset-=25
  140.                             reg+=str(int(round(offset)))
  141.  
  142.  
  143.                 elif (offset<=600 and offset >0):
  144.                     offset-=1
  145.                     alphaindex1 = int(offset/25)
  146.                     alphaindex2 = offset%25
  147.                     reg+=alphabet[alphaindex1]
  148.                     if (alphaindex2>0):
  149.                         alphaindex2-=1
  150.                         reg+=alphabet[alphaindex2]
  151.  
  152.                 return reg
  153.  
  154.             elif (regrange[2]<=hexcode<=regrange[3] or regrange[4]<=hexcode<=regrange[5]):
  155.                 if (regrange[2]<=hexcode<=regrange[3]):
  156.                     numpre="RA-"
  157.                     numstart=regrange[2]
  158.                     numfirst=0
  159.                 else:
  160.                     numpre="CU-T"
  161.                     numstart=regrange[4]
  162.                     numfirst=1000
  163.                 reg=numpre+str(hexcode-numstart+numfirst)
  164.                 return reg
  165.  
  166.             elif(regrange[6]<=hexcode<=regrange[11]):
  167.                 if (regrange[6]<=hexcode<=regrange[7]):
  168.                     reg="HL"+str(hex(hexcode-0x71ba00+0x7200))[2:]
  169.                 elif(regrange[8]<=hexcode<=regrange[9]):
  170.                     reg="HL"+str(hex(hexcode-0x71c000+0x8000))[2:]
  171.                 elif(regrange[10]<=hexcode<=regrange[11]):
  172.                     reg="HL"+str(hex(hexcode-0x71c200+0x8200))[2:]
  173.                 return reg
  174.  
  175.             elif(regrange[12]<=hexcode<=regrange[13]):
  176.                 limalphabet='ABCDEFGHJKLMNPQRSTUVWXYZ'
  177.                 offset=hexcode-0x840000
  178.                 reg="JA"
  179.  
  180.                 dig1=int(offset/22984)
  181.                 if (dig1<0 or dig1>9):
  182.                     return 0
  183.  
  184.                 reg+=str(dig1)
  185.                 offset=offset%22984
  186.  
  187.                 dig2=int(offset/916)
  188.                 if (dig2<0 or dig2>9):
  189.                     return 0
  190.  
  191.                 reg+=str(dig2)
  192.                 offset=offset%916
  193.  
  194.                 if (offset<340):
  195.                     dig3=int(offset/34)
  196.                     reg+=str(dig3)
  197.                     offset=offset%34
  198.  
  199.                     if (offset<10):
  200.                         return reg+str(offset)
  201.                     else:
  202.                         offset-=10
  203.                         return reg+limalphabet[offset]
  204.                 else:
  205.                     offset-=340
  206.                     let3=int(offset/24)
  207.                     return reg+limalphabet[let3]+limalphabet[offset%24]
  208.  
  209.             else:
  210.                 alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  211.                 for a in range(len(mappings)):
  212.                     if (mappings[a][0]<=hexcode<=mappings[a][5]):
  213.                         offset = hexcode-mappings[a][0]+mappings[a][4]
  214.                         vara=int(offset/mappings[a][1])
  215.                         offset=offset%mappings[a][1]
  216.                         varb=int(offset/mappings[a][2])
  217.                         offset=offset%mappings[a][2]
  218.                         varc=offset
  219.                         if (vara<0 or varb<0 or varc<0 or vara>=26 or varb>=26 or varc>=26):
  220.                             return 0
  221.                         else:
  222.                             reg=mappings[a][3]+alphabet[vara]+alphabet[varb]+alphabet[varc]
  223.                             return reg
  224.                 return 0
  225.  
  226.  
  227.         searchstr="flight"
  228.         filteredairlines=["AFR","KLM","BAW"]
  229.         filteredcountries=["US","CA"]
  230.  
  231.         desiredregs=["N123AB"]
  232.         desiredairlines=["PEX","KIA"]
  233.         desiredhexcodes=[""]
  234.  
  235.         planecache=[]
  236.         dayhold=time.strftime("%Y%m%d")
  237.         while 1:
  238.             f = open("////PATH TO /aircraft.json","r")
  239.             #iterate through raw lines
  240.             for line in f:
  241.                 #find lines with flight
  242.                 if (line.find(searchstr)!=-1):
  243.                     desiredtraits=False
  244.                     hasregistration=False
  245.                     process=True
  246.                     hexrawinput="0x"+line[12:18]
  247.                     #check hex validity
  248.                     try:
  249.                         hexinput=int(hexrawinput,16)
  250.                     except:
  251.                         continue
  252.  
  253.                     #find aircraft registration
  254.                     try:
  255.                         nreg=hexdecoder(hexinput)
  256.                     except:
  257.                         continue
  258.                     #find country from hexcode
  259.                     try:
  260.                         hexcountry=findcountry(hexinput)
  261.                     except:
  262.                         continue
  263.                     #check if registration can be found
  264.                     if (nreg!=0):
  265.                         hasregistration=True
  266.                     #check for desired traits
  267.                     if (any(line[30:33] in g for g in desiredairlines)==True):
  268.                         desiredtraits=True
  269.                     #check for desired registration
  270.                     if (hasregistration==True):
  271.                         if (any(nreg in s for s in desiredregs)==True):
  272.                             desiredtraits=True
  273.                     #filter out countries and common airlines
  274.                     if (desiredtraits==False):
  275.                         if (any(hexcountry in q for q in filteredcountries)==True or any(line[30:33] in n for n in filteredairlines)==True):
  276.                             process=False
  277.                     if (process==True):
  278.                         timestr = time.strftime("%Y%m%d")
  279.                         wrf = open("////DIRECTORY TO SAVE RAW FILE"+timestr+"-raw.dat","a+")
  280.                         wrf.write(line)
  281.                         wrf.close()
  282.                         #flush planecache when new day
  283.                         if (timestr!=dayhold):
  284.                             planecache=[]
  285.                             dayhold=timestr
  286.                         #if not in planecache add to unique flights
  287.                         if (any(line[12:18] in x for x in planecache)==False):
  288.                             fromaddr = "////[email protected]"
  289.                             toaddrs  = "////[email protected]"
  290.  
  291.                             planecache.append(line[12:18])
  292.                             orgf = open("////DIRECTORY TO SAVE FLIGHTS FILE"+timestr+"-flights.dat","a+")
  293.                             pushurl = "https://flightaware.com/live/modes/"+line[12:18]+"/redirect"
  294.                             #check if flight is blank
  295.                             if (line[30:38].isspace()==True):
  296.                                 if (hasregistration==True):
  297.                                     pushmessage = time.strftime("%H:%M:%S")+" "+line[12:18]+" ("+nreg+")"
  298.                                 else:
  299.                                     pushmessage = time.strftime("%H:%M:%S")+" "+line[12:18]
  300.                                 if (hexcountry!="nocountry"):
  301.                                     pushmessage+=" ["+hexcountry+"]"
  302.                                 msg=MIMEText(pushmessage)
  303.                                 orgf.write(pushmessage)
  304.                             else:
  305.                                 if (hasregistration==True):
  306.                                     pushmessage = time.strftime("%H:%M:%S")+" "+line[30:38]+" ("+nreg+")"
  307.                                 else:
  308.                                     pushmessage = time.strftime("%H:%M:%S")+" "+line[30:38]+" ("+line[12:18]+")"
  309.                                 if (hexcountry!="nocountry"):
  310.                                     pushmessage+=" ["+hexcountry+"]"
  311.                                 msg=MIMEText(pushmessage)
  312.                                 orgf.write(pushmessage)
  313.  
  314.                             msg['Subject']='////PlaneCatcher Notification'
  315.                             msg['From']=fromaddr
  316.                             msg['To']=toaddrs
  317.                             server = smtplib.SMTP('////smtp.mail.yahoo.com',587)
  318.                             try:
  319.                                 server.connect('////smtp.mail.yahoo.com',587)
  320.                             except:
  321.                                 server.connect('////smtp.mail.yahoo.com',587)
  322.                             server.ehlo()
  323.                             server.starttls()
  324.                             server.ehlo()
  325.                             server.login('////[email protected]','////YOURPASSWORDHERE')
  326.                             server.set_debuglevel(1)
  327.                             server.sendmail(fromaddr, toaddrs, msg.as_string())
  328.                             server.quit()
  329.                             orgf.close()
  330.             f.close()
  331.             time.sleep(2)
  332.  
  333. if __name__ == "__main__":
  334.     daemon = catcher('/tmp/planecatcher.pid')
  335.     if len(sys.argv) == 2:
  336.         if 'start' == sys.argv[1]:
  337.             daemon.start()
  338.         elif 'stop' == sys.argv[1]:
  339.             daemon.stop()
  340.         elif 'restart' == sys.argv[1]:
  341.             daemon.restart()
  342.         else:
  343.             print "Unknown command"
  344.             sys.exit(2)
  345.         sys.exit(0)
  346.     else:
  347.         print "usage: %s start|stop|restart" % sys.argv[0]
  348.         sys.exit(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement