<s> __author__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __license__ = "<STR_LIT>" <EOL> __version__ = "<STR_LIT>" <EOL> __email__ = "<STR_LIT>" <EOL> import requests <EOL> import os <EOL> import sys <EOL> import configparser <EOL> from html . parser import HTMLParser <EOL> class PacktFreeBookHtmlParser ( HTMLParser ) : <EOL> def __init__ ( self ) : <EOL> HTMLParser . __init__ ( self ) <EOL> self . claimAttrs = None <EOL> self . bookTitle = str ( ) <EOL> self . formBuildIdAttrs = None <EOL> self . bookTitleTagFound = False <EOL> self . loginFormTagFound = False <EOL> def handle_starttag ( self , tag , attrs ) : <EOL> if tag == '<STR_LIT:a>' : <EOL> for name , value in attrs : <EOL> if name == '<STR_LIT:class>' and value == '<STR_LIT>' : <EOL> self . claimAttrs = attrs <EOL> elif tag == '<STR_LIT>' : <EOL> for name , value in attrs : <EOL> if name == '<STR_LIT:class>' and value == '<STR_LIT>' : <EOL> self . bookTitleTagFound = True <EOL> elif tag == '<STR_LIT>' : <EOL> for name , value in attrs : <EOL> if name == '<STR_LIT:id>' and value == '<STR_LIT>' : <EOL> self . loginFormTagFound = True <EOL> elif self . loginFormTagFound and tag == '<STR_LIT:input>' : <EOL> for name , value in attrs : <EOL> if name == '<STR_LIT:name>' and value == '<STR_LIT>' : <EOL> self . formBuildIdAttrs = attrs <EOL> def handle_endtag ( self , tag ) : <EOL> if ( self . bookTitleTagFound and tag == '<STR_LIT>' ) : <EOL> self . bookTitleTagFound = False <EOL> self . bookTitle = self . bookTitle . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> elif self . loginFormTagFound and tag == '<STR_LIT>' : <EOL> self . loginFormTagFound = False <EOL> pass <EOL> def handle_data ( self , data ) : <EOL> if ( self . bookTitleTagFound ) : <EOL> self . bookTitle += data <EOL> def getClaimUrl ( self ) : <EOL> claimUrl = None <EOL> if self . claimAttrs is not None : <EOL> for name , value in self . claimAttrs : <EOL> if name == '<STR_LIT>' : <EOL> claimUrl = value <EOL> return claimUrl <EOL> def getFormBuildId ( self ) : <EOL> formBuildId = str ( ) <EOL> if self . formBuildIdAttrs is not None : <EOL> for name , value in self . formBuildIdAttrs : <EOL> if name == '<STR_LIT:value>' : <EOL> formBuildId += value <EOL> return formBuildId <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> '''<STR_LIT>''' <EOL> config = configparser . ConfigParser ( ) <EOL> try : <EOL> if ( not config . read ( "<STR_LIT>" ) ) : <EOL> raise configparser . Error ( '<STR_LIT>' ) <EOL> email = config . get ( "<STR_LIT>" , '<STR_LIT:email>' ) <EOL> password = config . get ( "<STR_LIT>" , '<STR_LIT:password>' ) <EOL> downloadBooksAfterClaim = config . get ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> except configparser . Error as e : <EOL> print ( "<STR_LIT>" + str ( e ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> freeLearningUrl = "<STR_LIT>" <EOL> packtpubUrl = '<STR_LIT>' <EOL> reqHeaders = { '<STR_LIT:Content-Type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> formData = { '<STR_LIT:email>' : email , <EOL> '<STR_LIT:password>' : password , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> print ( "<STR_LIT>" ) <EOL> try : <EOL> r = requests . get ( freeLearningUrl , timeout = <NUM_LIT:10> ) <EOL> except requests . exceptions . RequestException as exception : <EOL> print ( "<STR_LIT>" % exception ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> strContent = str ( r . content ) <EOL> htmlParser = PacktFreeBookHtmlParser ( ) <EOL> try : <EOL> htmlParser . feed ( strContent ) <EOL> r = requests . get ( packtpubUrl + htmlParser . getClaimUrl ( ) , timeout = <NUM_LIT:10> ) <EOL> except TypeError as typeError : <EOL> print ( "<STR_LIT>" % typeError ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> except requests . exceptions . RequestException as exception : <EOL> print ( "<STR_LIT>" % exception ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if ( r . status_code is not <NUM_LIT:200> ) : <EOL> print ( "<STR_LIT>" ) <EOL> formData [ '<STR_LIT>' ] = htmlParser . getFormBuildId ( ) <EOL> session = requests . Session ( ) <EOL> try : <EOL> rPost = session . post ( freeLearningUrl , headers = reqHeaders , data = formData ) <EOL> if ( rPost . status_code is not <NUM_LIT:200> ) : <EOL> raise requests . exceptions . RequestException ( "<STR_LIT>" ) <EOL> r = session . get ( packtpubUrl + htmlParser . getClaimUrl ( ) , timeout = <NUM_LIT:10> ) <EOL> except requests . exceptions . RequestException as exception : <EOL> print ( "<STR_LIT>" % exception ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if ( r . status_code is <NUM_LIT:200> ) : <EOL> print ( "<STR_LIT>" + htmlParser . bookTitle + "<STR_LIT>" ) <EOL> if downloadBooksAfterClaim == "<STR_LIT>" : <EOL> from packtFreeBookDownloader import MyPacktPublishingBooksDownloader <EOL> downloader = MyPacktPublishingBooksDownloader ( session ) <EOL> downloader . getDataOfAllMyBooks ( ) <EOL> downloader . downloadBooks ( [ htmlParser . bookTitle ] , downloader . downloadFormats ) <EOL> else : <EOL> print ( "<STR_LIT>" + htmlParser . bookTitle + "<STR_LIT>" + str ( r . status_code ) ) </s>
