Skip to content

Instantly share code, notes, and snippets.

@developerfromjokela
Last active September 21, 2020 11:24
Show Gist options
  • Save developerfromjokela/c5e0a1e6ceda34957f658aa7c9f9458d to your computer and use it in GitHub Desktop.
Save developerfromjokela/c5e0a1e6ceda34957f658aa7c9f9458d to your computer and use it in GitHub Desktop.
Skripti luo kokeita jotta saa abitin sanavaraston talteen
import requests
import requests.utils
import os.path
"""Filename"""
filename = "sanavarasto.txt"
abittiUsername = ""
abittiPassword = ""
alreadyAddedWords = []
if os.path.exists(filename):
savedFile = open(filename, 'r')
alreadyAddedWords = savedFile.readlines()
alreadyAddedWords = [x.strip() for x in alreadyAddedWords]
sClient = requests.Session()
def getHeaders():
headers = requests.utils.default_headers()
headers['User-Agent'] = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0'
return headers
"""Login to abitti"""
loginResult = sClient.post("https://oma.abitti.fi/kurko-api/user/login",
json={'username': abittiUsername, 'password': abittiPassword}, headers=getHeaders())
def createExamAndGetId():
createResult = None
try:
createResult = sClient.post("https://oma.abitti.fi/kurko-api/exam/exam-event", json={'title': 'Uusi koe'},
headers=getHeaders())
except Exception as e:
print("Exception " + str(e))
if createResult is not None:
if createResult.status_code == 201:
return createResult.json().get('examUuid', None)
else:
raise Exception("Status code " + str(createResult.status_code) + " when adding an exam")
else:
print("Network error, continuing...")
def getExamDetails(uuid):
detailsResult = None
try:
detailsResult = sClient.get("https://oma.abitti.fi/exam-api/exams/" + uuid + "/exam", headers=getHeaders())
except Exception as e:
print("Exception " + str(e))
if detailsResult is not None:
if detailsResult.status_code == 200:
return detailsResult.json()
else:
raise Exception("Status code " + str(detailsResult.status_code) + " when tried to fetch exam details")
else:
print("Network error, continuing...")
def checkPasswordArray(password_array):
toBeAdded = []
for word in password_array:
if word.strip() not in alreadyAddedWords:
print(word.strip())
toBeAdded.append(word.strip() + "\n")
"""Adding to the file"""
writeWordsToFile(toBeAdded)
"""Adding to alreadyAddedWords array"""
alreadyAddedWords.extend(toBeAdded)
def writeWordsToFile(words):
file_object = open(filename, 'a')
# Append words at the end of file
file_object.writelines(words)
# Close the file
file_object.close()
def deleteExam(uuid):
deleteResult = None
try:
deleteResult = sClient.delete("https://oma.abitti.fi/kurko-api/exam/exam-event/" + uuid, headers=getHeaders())
except Exception as e:
print("Exception " + str(e))
if deleteResult is not None:
if deleteResult.status_code != 200:
print("Deleting failed with status code " + str(deleteResult.status_code) + " for exam " + uuid)
else:
print("Network error, continuing...")
if loginResult.status_code == 204:
"""Login succeeded, let's continue by fetching user's name"""
userResult = sClient.get("https://oma.abitti.fi/kurko-api/user", headers=getHeaders())
userObject = userResult.json()
userName = userObject.get('userName', 'unknown')
print("Welcome, " + userName)
print("Let's start capturing words")
while True:
"""Creating new exam"""
examId = createExamAndGetId()
examDetails = getExamDetails(examId)
"""Extracting exam password"""
examPassword = examDetails.get('password', None)
if examPassword is not None:
examPasswordWords = examPassword.split()
checkPasswordArray(examPasswordWords)
deleteExam(examId)
else:
print("Password not found in exam, skipping")
deleteExam(examId)
else:
print("Status code " + str(loginResult.status_code))
print("Please check login details and try again")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment