-
Notifications
You must be signed in to change notification settings - Fork 13
/
setup.py
executable file
·115 lines (91 loc) · 3.81 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#! /usr/bin/env python3
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from configparser import ConfigParser
import os
import sys
from bs4 import BeautifulSoup
import json
filename = "temporaryFile.html"
def amazonUpdate():
# Initialising the parser
userParser = ConfigParser()
userParser.read('userconfig.ini')
userParser.optionxform = str
parsingDictionary = {"service": "AMAZON"}
# Required variables for filling in config file
baseurl = userParser.get(parsingDictionary['service'], 'url')
username = userParser.get(parsingDictionary['service'], 'username')
password = userParser.get(parsingDictionary['service'], 'password')
parser = ConfigParser()
parser.read('config.ini')
parser.optionxform = str
customerUrl = parser.get(parsingDictionary['service'], 'customerurl')
tokenUrl = parser.get(parsingDictionary['service'], 'tokenurl')
xpaths = {'usernameBox': "//*[@id='ap_email']",
'passwordBox': "//*[@id='ap_password']",
'submitButton': "//*[@id='signInSubmit']"
}
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference('permissions.default.stylesheet', 2)
firefox_profile.set_preference('permissions.default.image', 2)
firefox_profile.set_preference(
'dom.ipc.plugins.enabled.libflashplayer.so', 'false')
amazonDriver = webdriver.Chrome()
amazonDriver.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:36.0) Gecko/20100101 Firefox/36.0 WebKit'
amazonDriver.cookiesEnabled = True
amazonDriver.javascriptEnabled = True
amazonDriver.get(baseurl)
# Clearing Username TextBox
amazonDriver.find_element_by_xpath(xpaths['usernameBox']).clear()
# Typing in the username as obtained from config file
amazonDriver.find_element_by_xpath(
xpaths['usernameBox']).send_keys(username)
# Clearing password field
amazonDriver.find_element_by_xpath(xpaths['passwordBox']).clear()
# Typing in the password
amazonDriver.find_element_by_xpath(
xpaths['passwordBox']).send_keys(password)
# Clicking on Submit button
amazonDriver.find_element_by_xpath(xpaths['submitButton']).click()
amazonDriver.get(customerUrl)
pageSource = amazonDriver.page_source
pageSource = BeautifulSoup(pageSource, "lxml", from_encoding="utf8").pre.text
# print(pageSource)
# Removing escaped characters like \n \t and literal double quotes
pageSource = pageSource.replace(
'\\"', "").replace('\\n', "").replace('\\t', "")
obtainFrom = ["customerInfo", "customerID"]
try:
parsedSource = json.loads(pageSource)
customerID = parsedSource[obtainFrom[0]][obtainFrom[1]]
# print(customerID)
parser.set(parsingDictionary['service'], 'customerid', customerID)
amazonDriver.get(tokenUrl)
pageSource = amazonDriver.page_source
token = BeautifulSoup(pageSource, "lxml", from_encoding="utf8").pre.text
tokenId = token.replace("onWebToken_fccab172c7f94fe78ff8dc7d985dd3e4", "").replace(
'(', "").replace(")", "").replace(';', "")
token = json.loads(tokenId)
token = token['token']
parser.set(parsingDictionary['service'], 'token', token)
fileHandler = open('config.ini', "w")
parser.write(fileHandler)
fileHandler.close()
except ZeroDivisionError:
print("An error ocurred. Please report the issue.")
pass
amazonDriver.close()
def main():
updateServices = {"amazon": amazonUpdate}
for services in updateServices:
updateServices[services]()
# try:
# os.remove(filename)
# except:
# pass
pass
if __name__ == "__main__":
main()