From 480a2935e0eba6fe2577b10f317f2fdbea55d7ce Mon Sep 17 00:00:00 2001 From: Lionel Date: Tue, 16 Oct 2018 23:09:26 +0200 Subject: presque fini wlh --- README.md | 1 + errmsg.py | 2 +- foldercreating.py | 62 ++++++++++++++++++++++++++++++++++--------------------- inputs.py | 24 +++++++++++++++------ main.py | 54 +++++++++++++++++++++++------------------------- mapsparsing.py | 18 ++++++++-------- randoweb.py | 38 ++++++++++++++++++++++++---------- 7 files changed, 121 insertions(+), 78 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..1255426 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# randopitons diff --git a/errmsg.py b/errmsg.py index 25e0641..8e67d42 100644 --- a/errmsg.py +++ b/errmsg.py @@ -3,5 +3,5 @@ os=clc.fail+"Operating System error:"+clc.endc direxist=clc.warn+clc.bold+"The folder already exists !"+clc.endc formaterr=clc.fail+"Something occured with the formating. File is not retrievable, passing on next download."+clc.endc sslerr=clc.fail+clc.bold+"SSL Certificate Error. Please check the time on your computer, adjust it accordingly, or wait a bit before retrying the downloads."+clc.endc -sigkill=clc.fail+"\n\nProcess interrupted by CTRL+C or system."clc.endc +sigkill=clc.fail+"\n\nProcess interrupted by CTRL+C or system."+clc.endc "\n\nProcess interrupted by CTRL+C or system." diff --git a/foldercreating.py b/foldercreating.py index c1f4b23..a4497d2 100644 --- a/foldercreating.py +++ b/foldercreating.py @@ -3,49 +3,65 @@ import os import time import colorcode as clc import errmsg as e +import randoweb as rdo #global foldernames -#(foldernames[0].split(' ')[0]+"-"+foldernames[0].split(' ')[2]).lower() => cirque-cilaos . ce formating sera pratique lors de la récupération sur site web thehome=os.path.expanduser("~") defoldername="/Randopitons" def_folder=thehome+defoldername -fullpath=thehome+"/"+foldernames[regionptr] + + + + +def defolder(): + try: + print "\nCreating default folder "+def_folder+" ..." + os.mkdir(def_folder) + except OSError,er: + print e.os + print format(er) + def mainfolder(): try: a_folder=input("\n\nWhich folder would you want to download the files to [Default to home directory "+def_folder+"]") + os.mkdir(a_folder) print "Folder "+a_folder+" was created successfully" - except OSError, e: + except OSError, er: #errors out if folder exists else print the error (can be permissions or anything else) - if e.errno == os.errno.EEXIST: - print e.direxist + if e.errno != os.errno.EEXIST: + raise else: print e.os - print format(e) - pass + print format(er) + pass + except SyntaxError: - print "\nCreating default folder "+def_folder+" ..." + os.mkdir(def_folder) + except KeyboardInterrupt: + print e.sigkill -def mkfolder(choice): - regionptr=choice-1 +def mkfolder(MAIL,PSW,regionnames,foldernames,maptype,ptr,choice,bfn): + try: - os.mkdir(os.path.join(thehome,foldernames[regionptr])) - except OSError, e: + os.mkdir(os.path.join(thehome,foldernames[ptr])) + rdo.randoweb(MAIL,PSW,regionnames[ptr],filetype,bfn) + except OSError, er: print e.os - print format(e) + print format(er) -def mkallfolder(choice): +def mkallfolder(MAIL,PSW,regionnames,foldernames,maptype,bfn): + i=0 try: - if choice == 10: - for foldernames in range(9): - os.mkdir(os.path.join(thehome,str(foldernames))) - #subprocess.call("rm -d "+fullpath) - print "All folders were created successfully" - except OSError, e: + for foldernames in foldernames: + os.mkdir(os.path.join(thehome,str(foldernames))) + rdo.randoweb(MAIL,PSW,regionnames[i],maptype,bfn) + i=i+1 + #subprocess.call("rm -d "+fullpath) + print "All folders were created successfully" + except OSError, er: print e.os - print format(e) - + print format(er) -#print "Program ended." diff --git a/inputs.py b/inputs.py index cc00de3..3b47bef 100644 --- a/inputs.py +++ b/inputs.py @@ -2,10 +2,12 @@ import getpass import colorcode as clc import errmsg as e + def mapfileinput(): maptype = 0 print clc.bold+"\n Which map file extension you would want to download ?"+clc.endc - print """1. gpx (GPS Exchange Format) + print """ + 1. gpx (GPS Exchange Format) 2. trk (CompeGPS Land Track File) 3. kml (Keyhole Markup Language)\n""" @@ -16,13 +18,17 @@ def mapfileinput(): except (KeyboardInterrupt, SystemExit): print e.sigkill exit() + except SyntaxError: + print "Defaulting to gpx. If this isn't what you wanted, you can always interrupt the script by pressing "+clc.bold+clc.okblue+"CTRL+C"+clc.endc + maptype=1 return maptype def regioninput(): zone = 0 print clc.bold+"\n Which zone you would want to download ?"+clc.endc - print """1. Cirque de Cilaos (Cilaos Circus) + print """ + 1. Cirque de Cilaos (Cilaos Circus) 2. Cirque de Mafate (Mafate's Circus) 3. Cirque de Salazie (Salazie's Circus) 4. Est (East) @@ -41,7 +47,7 @@ def regioninput(): print e.sigkill exit() except SyntaxError: - print("\nDefaulting to All. If this isn't what you wanted, you can always interrupt the script by pressing "+clc.bold+"CTRL+C"+clc.endc) + print("\nDefaulting to All. If this isn't what you wanted, you can always interrupt the script by pressing "+clc.bold+clc.okblue+"CTRL+C"+clc.endc) zone = 10; except NameError: print "\nPlease enter a number between 1 and 10." @@ -49,9 +55,15 @@ def regioninput(): return zone def loginmail(): - MAIL=raw_input("Your email to log in to randopitons.re : ") + try: + MAIL=raw_input("Your email to log in to randopitons.re : ") + except KeyboardInterrupt: + print e.sigkill return MAIL def loginpass(): - PSW=getpass.getpass(prompt="Your password to log in to randopitons.re: ") - return PASS + try: + PSW=getpass.getpass(prompt="Your password to log in to randopitons.re: ") + except KeyboardInterrupt: + print e.sigkill + return PSW diff --git a/main.py b/main.py index 6cabb96..fd462be 100644 --- a/main.py +++ b/main.py @@ -1,43 +1,41 @@ import os -import inputs -import mapsparsing -import colorcode as clc -import foldercreating as fdc +import inputs as io +import colorcode as clc foldernames=["Cirque de Cilaos","Cirque de Mafate","Cirque de Salazie","Est","Nord","Ouest","Sud","Volcan","Ailleurs","All"] -randonb=[] +webregionnames=[] +for i in range(9): + try: + leftside=foldernames[i].split(' ')[0].lower()+"-" + rightside=foldernames[i].split(' ')[2].lower() + webregionnames.append(leftside+rightside) + except IndexError,ierr: + oneside=foldernames[i].split(' ')[0].lower() + webregionnames.append(oneside) + pass +import foldercreating as fdc basicfilename="" +randonb=[] print clc.bold+"\n\nWelcome ! \nWith this script, you will be able to download gpx,trk and kml traces for hitchiking on the Reunion Isle."+clc.endc +zonechoice=io.regioninput() +zoneptr=zonechoice-1 +maptype=io.mapfileinput() +MAIL=io.loginmail() +PSW=io.loginpass() -loginmail() -logipass() -filetype=mapfileinput() -zonechoice=regioninput() -if filetype==1: +if maptype==1: basicfilename="f.gpx" -elif filetype==2: +elif maptype==2: basicfilename="f.trk" -else filetype==3: +elif maptype==3: basicfilename="f.kml" -mapparsing(filetype,basicfilename) -mkfolder(filetype,zonechoice) - -payload = { - 'mail': MAIL, - 'password': PSW -} - -randoweb() - - - - - - - +fdc.mainfolder() +fdc.mkfolder(MAIL,PSW,webregionnames,foldernames,maptype,zoneptr,basicfilename) +if zonechoice == 10: + fdc.mkallfolder(MAIL,PSW,webregionnames,foldernames,maptype,zoneptr,basicfilename) diff --git a/mapsparsing.py b/mapsparsing.py index 7fb6edd..7128f62 100644 --- a/mapsparsing.py +++ b/mapsparsing.py @@ -2,27 +2,27 @@ import xml.etree.ElementTree as xmlp import re import errmsg as e -def mapparsing(filetype,basicfilename): +def mapparsing(maptype,basicfilename): try: root=tree.getroot() - if filetype==1: + if maptype==1: tree=xmlp.parse(basicfilename) filename=root[1][0].text+".gpx" - else filetype==2: - tree=xmlp.parse(basicfilename) - filename= root[0][10][1][0].text+".kml" - elif filetype==3: + elif maptype==2: thefile=open(basicfilename) for i in range(3): thefile.readline() - filename=re.findall(r'\|(.*?)\|', thefile.readline())[0] + filename=re.findall(r'\|(.*?)\|', thefile.readline())[0]+".trk" + elif maptype==3: + tree=xmlp.parse(basicfilename) + filename= root[0][10][1][0].text+".kml" os.rename(basicfilename,filename) except OSError, e: print e.os - except xml.etree.ElementTree.ParseError, e: + except xmlp.ParseError, e: print e.formaterr print format(e) os.remove(basicfilename) pass - return filename + return filename diff --git a/randoweb.py b/randoweb.py index 332bdad..5955f14 100644 --- a/randoweb.py +++ b/randoweb.py @@ -3,10 +3,20 @@ import inputs from bs4 import BeautifulSoup as bs import colorcode as clc import errmsg as e +import mapsparsing as mpp -def randoweb(): + + + +def randoweb(MAIL,PSW,region,maptype,bfn): + payload = { + 'mail': MAIL, + 'password': PSW + } + with session() as c: + try: post('https://randopitons.re/connexion', data=payload) region = c.get('https://randopitons.re/randonnees/region/'+region) @@ -15,23 +25,29 @@ def randoweb(): randonb.append(i.get("rid")) except SSLError, sslerr: print e.sslerr - - - - + try: for i in randonb: - dwnld = c.get('https://randopitons.re/randonnee/'+i+'/trace/'+maptype) + dwnld="all the interwebz" + if maptype==1: + dwnld = c.get('https://randopitons.re/randonnee/'+i+'/trace/gpx') + gpxf=open(bfn,'a+') + elif maptype==2: + dwnld = c.get('https://randopitons.re/randonnee/'+i+'/trace/kml') + gpxf=open(bfn,'a+') + else: + dwnld = c.get('https://randopitons.re/randonnee/'+i+'/trace/trk') + gpxf=open(bfn,'a+') + gudencoding=dwnld.text.encode('utf-8') - - gpxf=open('f.gpx','a+') gpxf.write(gudencoding) gpxf.close() - + + mpp.mapparsing(maptype,bfn) print("Finished writing files.") - except OSError, e: + except OSError, er: print e.os - print format(e) + print format(er) -- cgit v1.2.3-70-g09d2