Last active
August 29, 2015 14:03
-
-
Save timrichardson/449c2f979bc6bd6da872 to your computer and use it in GitHub Desktop.
web-scraping technique to bulk export Kounta daily cash closings to Saasu. Currently there is no public Kounta API.
This file contains hidden or 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
username = '[email protected]' | |
password='asecret' |
This file contains hidden or 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
There is no API available for Kounta. Well, Kounta says there is one if you ask; I've asked a few times and was ignored. | |
This is a webscraping solution to mass exporting of daily takings reports into Saasu. |
This file contains hidden or 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
import requests | |
from bs4 import BeautifulSoup as bs | |
import re | |
import identity | |
def authenticate(s,url): | |
""" | |
return: request_object_after_post, and the original page with the form (which may have cookies) | |
""" | |
page = s.get(url) | |
#print page.content | |
soup=bs(page.content) | |
action = soup.form['action'] #but this is not needed, the action is determined by the UEL | |
#use Firefox to inspect the header sent to the server to get the form variables. | |
payload = { | |
'LoginForm[username]': identity.username, | |
'LoginForm[password]': identity.password, | |
'rid':1 | |
} | |
auth = s.post(url,data=payload) | |
return auth,page | |
if __name__ == '__main__': | |
s = requests.session() | |
url = r'https://my.kounta.com/website/login' | |
auth,page = authenticate(s,url) | |
url2 = r'https://my.kounta.com/integration/saasu' | |
exports_exhausted = False | |
code_re = re.compile('"(.*)"') | |
while not exports_exhausted: | |
page2=s.get(url2,cookies=page.cookies) | |
soup = bs(page2.content) | |
found_downloads = soup.find_all("a", {"class":"btnDownload"}) | |
if not found_downloads: | |
exports_exhausted = True | |
break | |
else: | |
#print page2.content | |
#now need to pass every <a> which is in class "btnDownload" | |
for download_link in found_downloads: | |
#print download_link['onclick'] | |
matched = code_re.search(download_link['onclick']) | |
if matched: | |
code = matched.group(1) | |
print code | |
"""type: 'GET', | |
url:"/integration/ajaxexportsaasu?id=" + pID,""" | |
payload = {'id':code} | |
r = s.get("https://my.kounta.com/integration/ajaxexportsaasu",params=payload,cookies=page.cookies) | |
print "Code: %s, status:%s" % (code,r.status_code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment