Advertisement
Heeshung

PlaneCatcherv1

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