8000 Add dotenv, remove redundant LOC, fix encoding problem, change URLs to ERP3 by icyflame · Pull Request #2 · metakgp/mftp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add dotenv, remove redundant LOC, fix encoding problem, change URLs to ERP3 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 6, 2016
29 changes: 23 additions & 6 deletions erp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
from bs4 import BeautifulSoup as bs
import sys
import re
import settings

ERP_HOMEPAGE_URL = 'https://erp.iitkgp.ernet.in/IIT_ERP2/welcome.jsp'
ERP_HOMEPAGE_URL = 'https://erp.iitkgp.ernet.in/IIT_ERP3/welcome.jsp'
ERP_LOGIN_URL = 'https://erp.iitkgp.ernet.in/SSOAdministration/auth.htm'
ERP_SECRET_QUESTION_URL = 'https://erp.iitkgp.ernet.in/SSOAdministration/getSecurityQues.htm'
ERP_CDC_MODULE_URL = 'https://erp.iitkgp.ernet.in/IIT_ERP2/welcome.jsp?module_id=26&menu_id=11&delegated_by=&parent_menu_id=10'
ERP_CDC_MODULE_URL = 'https://erp.iitkgp.ernet.in/IIT_ERP3/menulist.htm?module_id=26'
ERP_TPSTUDENT_URL = 'https://erp.iitkgp.ernet.in/TrainingPlacementSSO/TPStudent.jsp'


req_args = {
'timeout': 20,
'headers': {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36',
'Referer': 'https://erp.iitkgp.ernet.in/SSOAdministration/login.htm?sessionToken=595794DC220159D1CBD10DB69832EF7E.worker3&requestedUrl=https://erp.iitkgp.ernet.in/IIT_ERP2/welcome.jsp',
'Referer':
'https://erp.iitkgp.ernet.in/SSOAdministration/login.htm?sessionToken=595794DC220159D1CBD10DB69832EF7E.worker3',
},
'verify': False
}
Expand All @@ -25,17 +27,27 @@ def erp_login(func):

def wrapped_func(*args, **kwargs):

print "Started erp_login!"

s = requests.Session()

r = s.get(ERP_HOMEPAGE_URL, **req_args)
soup = bs(r.text, 'html.parser')
sessionToken = soup.find_all(id='sessionToken')[0].attrs['value']

print "Length of the fetched HTML: " + str(len(str(r.text)))
# print str(r.text)
if soup.find(id='sessionToken'):
sessionToken = soup.find(id='sessionToken').attrs['value']
else:
raise Exception("Could not get the sessionToken!")

r = s.post(ERP_SECRET_QUESTION_URL, data={'user_id': env['ERP_USERNAME']},
**req_args)
secret_question = r.text
print "Secret question from the ERP: " + secret_question
secret_answer = None
for i in xrange(1, 4):
print env['ERP_Q%d' % i]
if env['ERP_Q%d' % i] == secret_question:
secret_answer = env['ERP_A%d' % i]
break
Expand All @@ -49,13 +61,17 @@ def wrapped_func(*args, **kwargs):
'password': env['ERP_PASSWORD'],
'answer': secret_answer,
'sessionToken': sessionToken,
'requestedUrl': 'https://erp.iitkgp.ernet.in/IIT_ERP2/welcome.jsp',
'requestedUrl': 'https://erp.iitkgp.ernet.in/IIT_ERP3/welcome.jsp',
}


r = s.post(ERP_LOGIN_URL, data=login_details,
**req_args)
ssoToken = re.search(r'\?ssoToken=(.+)$',
r.history[1].headers['Location']).group(1)
r.history[1].headers['Location']).group(1)

print "ERP Login completed!"
r = s.get("https://erp.iitkgp.ernet.in/IIT_ERP3/?%s" % ssoToken, **req_args)

func(session=s, sessionData={'ssoToken': ssoToken,
'sessionToken': sessionToken},
Expand All @@ -73,6 +89,7 @@ def wrapped_func(session, sessionData, *args, **kwargs):
session.post(ERP_TPSTUDENT_URL, # headers=headers,
data=dict(ssoToken=ssoToken, menu_id=11, module_id=26),
**req_args)
print "TNP Login completed!"
func(session=session, sessionData=sessionData, *args, **kwargs)

return wrapped_func
10 changes: 4 additions & 6 deletions hooks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from os import environ as env
import requests

import settings

from erp import req_args

if 'NOTICES_EMAIL_ADDRESS' not in env:
Expand All @@ -15,8 +17,6 @@ def make_text(company):

def companies_updated(companies):
message = {
'api_user': env['SENDGRID_USERNAME'],
'api_key': env['SENDGRID_PASSWORD'],
'to': env['EMAIL_ADDRESS'],
'from': 'no-reply@mftp.herokuapp.com',
'fromname': 'MFTP',
Expand All @@ -42,8 +42,6 @@ def companies_updated(companies):
def notices_updated(notices):
for notice in notices:
message = {
'api_user': env['SENDGRID_USERNAME'],
'api_key': env['SENDGRID_PASSWORD'],
'to': env['NOTICES_EMAIL_ADDRESS'],
'from': 'no-reply@mftp.herokuapp.com',
'fromname': 'MFTP',
Expand All @@ -65,8 +63,8 @@ def notices_updated(notices):
data={
'from': 'MFTP <no-reply@%s>' % env['MAILGUN_DOMAIN'],
'to': [env['NOTICES_EMAIL_ADDRESS']],
'subject': message['subject'],
'html': message['html']
'subject': message['subject'].encode("utf-8"),
'html': message['html'].encode("utf-8")
}, files=files, verify=False)

# r = requests.post('https://api.sendgrid.com/api/mail.send.json',
Expand Down
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import tornado.ioloop
from tornado import gen
import requests
import settings

requests.packages.urllib3.disable_warnings()

Expand All @@ -24,6 +25,7 @@ def func():
print 'Checking notices...'
update.check_notices()
except Exception as e:
print "There was an error!"
print e
yield executor.submit(func)
print 'run_updates done'
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ singledispatch==3.4.0.3
six==1.10.0
tornado==4.3
wheel==0.24.0
python-dotenv==0.5.1
6 changes: 6 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# settings.py
from os.path import join, dirname
from dotenv import load_dotenv

dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
11 changes: 10 additions & 1 deletion update.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import re
import hashlib

import settings

from erp import tnp_login, req_args
import hooks

NUM_NOTICES_DIFFED = 5

mc = MongoClient(env['MONGOLAB_URI'])

ERP_COMPANIES_URL = 'https://erp.iitkgp.ernet.in/TrainingPlacementSSO/ERPMonitoring.htm?action=fetchData&jqqueryid=37&_search=false&nd=1448725351715&rows=20&page=1&sidx=&sord=asc&totalrows=50'
Expand All @@ -20,11 +24,16 @@
def check_notices(session, sessionData):
r = session.get(ERP_NOTICEBOARD_URL, **req_args)
r = session.get(ERP_NOTICES_URL, **req_args)

print "ERP and TNP login completed!"

notices_list = bs(r.text, 'html.parser')

print "Total number of notices fetched: %d" % len(notices_list)

notices = []
# Only check the first 50 notices
for row in notices_list.find_all('row')[:50]:
for row in notices_list.find_all('row')[:NUM_NOTICES_DIFFED]:
notice = {}

cds = filter(lambda x: isinstance(x, CData), row.find_all(text=True))
Expand Down
0