Advertisement
Guest User

notasMOODLE

a guest
Jun 20th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. import requests,time,json,re,os,sys,hashlib
  2.  
  3. USERNAME = 2171549
  4. PASSWORD =
  5. WAITING = 5 #10 minutos
  6.  
  7. UCS = [
  8. {'uc':'Fiscalidade I','url':'https://ead.ipleiria.pt/2017-18/enrol/index.php?id=2884'},
  9. {'uc':'CRF II','url':'https://ead.ipleiria.pt/2017-18/enrol/index.php?id=3714'},
  10. {'uc':'Direito Emp. II','url':'https://ead.ipleiria.pt/2017-18/enrol/index.php?id=2881'},
  11. ]
  12.  
  13. hashpath = os.path.join(sys.path[0],'hashfiles.dat')
  14. s = requests.Session()
  15.  
  16. def login():
  17. headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
  18. 'Accept-Encoding': 'gzip, deflate, br',
  19. 'Accept-Language': 'en-US,en;q=0.9',
  20. 'Cache-Control': 'max-age=0',
  21. 'Connection': 'keep-alive',
  22. 'Content-Type': 'application/x-www-form-urlencoded',
  23. 'Host': 'ead.ipleiria.pt',
  24. 'Origin': 'https://ead.ipleiria.pt',
  25. 'Referer': 'https://ead.ipleiria.pt/2017-18/login/index.php',
  26. 'Upgrade-Insecure-Requests': '1',
  27. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'}
  28.  
  29. data = {'username': USERNAME,
  30. 'password': PASSWORD,
  31. 'anchor': ''}
  32.  
  33. info = s.post('https://ead.ipleiria.pt/2017-18/login/index.php',headers=headers, data=data).text
  34. if '<div id="content-wrapper">' in info:
  35. print("Login OK")
  36. return True
  37. else:
  38. print("Login falhou")
  39. exit(0)
  40. #return False
  41.  
  42. def request_uc(url):
  43. headers={'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
  44. 'Accept-Encoding': 'gzip, deflate, br',
  45. 'Accept-Language': 'en-US,en;q=0.9',
  46. 'Connection': 'keep-alive',
  47. 'Host': 'ead.ipleiria.pt',
  48. 'Referer': 'https://ead.ipleiria.pt/2017-18/my/',
  49. 'Upgrade-Insecure-Requests': '1',
  50. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'}
  51.  
  52. info = s.get(url,headers=headers).text
  53.  
  54. if not '<div id="content-wrapper">' in info:
  55. login()
  56. info = s.get(url,headers=headers).text
  57.  
  58. content = re.compile('<section id="region-main">(.+?)</section>').findall(info.replace('\n','').replace('\r','').replace('\t',''))[0]
  59. for x in re.compile('<input(.+?)>').findall(content):
  60. content = content.replace(x,'')
  61. return hashlib.md5(content.encode('utf-8')).hexdigest()
  62.  
  63. def return_hashfile():
  64. if os.path.exists(hashpath):
  65. stream = open(hashpath,'r')
  66. var = stream.read()
  67. stream.close()
  68. return var
  69. else:
  70. return '{}'
  71.  
  72. def save_hashfile(content):
  73. stream = open(hashpath,'w')
  74. stream.write(content)
  75. stream.close()
  76.  
  77. if __name__ == "__main__":
  78. if login():
  79. while True:
  80. hashes = json.loads(return_hashfile())
  81. print("A verificar alterações... ", end = "")
  82. for x in UCS:
  83. hash = request_uc(x['url'])
  84. if x['uc'] in hashes:
  85. if hashes[x['uc']] != hash:
  86. print("\n>>> Houve alteracoes a " + str(x['uc']) + "!!!")
  87. hashes[x['uc']] = hash
  88.  
  89. save_hashfile(json.dumps(hashes))
  90. if WAITING < 5:
  91. print("a aguardar " + str(5) + " minutos")
  92. time.sleep(5 * 60)
  93. else:
  94. print("a aguardar " + str(WAITING) + " minutos")
  95. time.sleep(WAITING * 60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement