-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release 0.2 Fixes Train Parsing Problems
- Loading branch information
1 parent
1508688
commit 5ce1967
Showing
15 changed files
with
405 additions
and
68 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Metadata-Version: 1.1 | ||
Name: RailIN | ||
Version: 0.2 | ||
Summary: Unofficial API for Indian Railways. | ||
Home-page: https://github.com/ash2shukla/RailIN | ||
Author: Ashish Shukla | ||
Author-email: ash2shukla@gmail.com | ||
License: MIT | ||
Download-URL: https://github.com/ash2shukla/RailIN/archive/0.1.tar.gz | ||
Description: UNKNOWN | ||
Keywords: Railways,API,IR,unofficial | ||
Platform: UNKNOWN |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
setup.cfg | ||
setup.py | ||
RailIN/CaptchaBreak.py | ||
RailIN/RailIN.py | ||
RailIN/__init__.py | ||
RailIN/prettify.py | ||
RailIN.egg-info/PKG-INFO | ||
RailIN.egg-info/SOURCES.txt | ||
RailIN.egg-info/dependency_links.txt | ||
RailIN.egg-info/requires.txt | ||
RailIN.egg-info/top_level.txt |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
bs4 | ||
requests==2.18.1 | ||
user_agent==0.1.8 | ||
pillow==4.2.1 | ||
pytesseract==0.1.7 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
RailIN |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from PIL import Image | ||
|
||
from os import remove | ||
|
||
from pytesseract import image_to_string as its | ||
|
||
class Captcha: | ||
def _add_background(self,image): | ||
data = image.convert('RGBA').getdata() | ||
to_White = [] | ||
for i in data: | ||
if i[3]==0: | ||
to_White.append((255,255,255,255)) | ||
else: | ||
to_White.append(i) | ||
image.putdata(to_White) | ||
return image | ||
|
||
def decode(self,raw_image): | ||
file = open('/tmp/x.png','wb') | ||
file.write(raw_image) | ||
file.close() | ||
data = Image.open('/tmp/x.png') | ||
# adding background after scaling will create blurry image | ||
data = self._add_background(data) | ||
# resize the image to 300 x 128 for better recognition by tesseract | ||
data = data.resize((300,128),Image.ANTIALIAS) | ||
remove('/tmp/x.png') | ||
# convert all pixel's alpha to 255 | ||
string = its(data) | ||
try: | ||
end = string.index('=') | ||
except ValueError: | ||
# if = doesn't exist in string then | ||
# sometimes = is recognized as semicolon | ||
end = string.index(':') | ||
finally: | ||
# return evaluated string before = or : | ||
# if neither = nor : is found then something wrong went | ||
# rather than raising an error return 0 | ||
try: | ||
return str(eval(string[:end])) | ||
except NameError: | ||
# if end was undefined i.e. = or : isn't defined. | ||
return '0' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from requests import get,post,Session | ||
from CaptchaBreak import Captcha | ||
from user_agent import generate_user_agent as gua | ||
from json import dumps,loads | ||
from prettify import Prettify | ||
from datetime import date | ||
''' | ||
This API scrapes data of Indian Railways from erail.in and parses into JSON for use in personal applications. | ||
Not intended for commercial use. | ||
1. Get Route ( X ) | ||
2. Get Availability ( X ) | ||
3. Get Fare ( X ) | ||
4. Get Train Status ( X ) | ||
5. Get StationCode ( X ) | ||
6. Get PNR ( X ) | ||
''' | ||
|
||
class RailIN: | ||
# Website asks for captcha which can be pre-generated due to the flaw | ||
def getPNR(self,PNR): | ||
if (len(str(PNR))<10) or (len(str(PNR))>10): | ||
return dumps({'error':'PNR must be 10 digit.'}) | ||
URL_captcha = 'http://www.indianrail.gov.in/enquiry/captchaDraw.png' | ||
# create a Session | ||
s = Session() | ||
s.headers['User-Agent'] = gua() | ||
probable_captcha = Captcha().decode(s.get(URL_captcha).content) | ||
try: | ||
URL = 'http://www.indianrail.gov.in/enquiry/CommonCaptcha?inputCaptcha='+probable_captcha+'&inputPnrNo='+PNR+'&inputPage=PNR' | ||
return loads(s.get(URL).text) | ||
except: | ||
return dumps({'error':'some captcha error occured'}) | ||
|
||
def getRoute(self,TN): | ||
ID = self.getTrain(TN) | ||
try: | ||
ID['error'] | ||
except KeyError: | ||
ID = ID['train_base']['train_id'] | ||
URL_Route = "https://erail.in/data.aspx?Action=TRAINROUTE&Password=2012&Data1="+ID+"&Data2=0&Cache=true" | ||
return Prettify().StationToJson(get(URL_Route).text) | ||
|
||
def getAllTrains(self,F,T): | ||
URL_Trains = "https://erail.in/rail/getTrains.aspx?Station_From="+F+"&Station_To="+T+"&DataSource=0&Language=0&Cache=true" | ||
return Prettify().TrainsToJson(get(URL_Trains,headers = {'User-Agent':gua()}).text) | ||
|
||
# Pass in date month and year | ||
def getTrainsOn(self,F,T,DD,MM,YYYY): | ||
retval = [] | ||
D = date(YYYY,MM,DD).weekday() | ||
for i in self.getAllTrains(F,T): | ||
if i['train_base']['running_days'][D]=='1': | ||
retval.append(i) | ||
return dumps(retval) | ||
|
||
def getTrain(self,TN): | ||
URL_Train = "https://erail.in/rail/getTrains.aspx?TrainNo="+str(TN)+"&DataSource=0&Language=0&Cache=true" | ||
try: | ||
return Prettify().TrainsToJson(get(URL_Train).text)[0] | ||
except: | ||
return {'error':'Unexpected Server Response'} | ||
|
||
def getAvailability(self,TN,SSTN,DSTN,CLS,QT,DD,MM): | ||
URL_Avail = "https://d.erail.in/AVL_Request?Key=" | ||
val = '_'.join([str(TN),SSTN,DSTN,CLS,QT,str(DD)+'-'+str(MM)]) | ||
return Prettify().AvailToJson(get(URL_Avail+val).text) | ||
|
||
|
||
def getFare(self,TN,F,T): | ||
URL_Fare = "https://erail.in/data.aspx?Action=GetTrainFare&train="+str(TN)+"&from="+F+"&to="+T | ||
return Prettify().FareToJson(get(URL_Fare).text) | ||
|
||
def getStatus(self,TN,DD,MMM,YYYY,STN): | ||
D = '-'.join([str(DD), MMM, str(YYYY)]) | ||
URL_Live = "https://data.erail.in/getIR.aspx?&jsonp=true&Data=RUNSTATUS~0_"+str(TN)+"_"+D+"_"+STN | ||
return loads(get(URL_Live).text.strip('()')) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from RailIN import RailIN | ||
|
||
__all__ = ['RailIN'] |
Oops, something went wrong.