-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
115 lines (95 loc) · 4.06 KB
/
util.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
"""
author: Mookeun Ji, [email protected]
"""
import json
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
from .exception import *
DEV = False
current_dir = os.path.dirname(os.path.abspath(__file__))
def url_join(main_url, sub_url):
"""
:param main_url: main url that specify host and sub directory
:param sub_url: sub directory url to be concatenated to main_url
:return: concatenated url
"""
if main_url[-1] is '/' and sub_url[0] is '/':
return main_url[0:-1] + sub_url
elif main_url[-1] is not '/' and sub_url[0] is not '/':
return main_url + '/' + sub_url
else:
return main_url + sub_url
def get_settings():
if DEV:
with open(os.path.join(current_dir, 'dev_setting.json'), 'r') as settings_json:
return json.load(settings_json)
else:
with open(os.path.join(current_dir, 'setting.json'), 'r') as settings_json:
return json.load(settings_json)
def get_locators():
with open(os.path.join(current_dir, 'locator.json'), 'r', encoding='utf-8') as locators_json:
return json.load(locators_json)
def get_settings_and_locators():
"""
get setting and locators specified in setting.json
:return: 2 dictionaries that contains setting and locators
"""
return get_settings(), get_locators()
# utils for crawling
def get_driver(mode='phantom'):
"""
:param mode: run mode('chrome' or 'phantom')
:exception raise NoSuchDriverException if mode is incorrect
:return: new web selenium driver
"""
if mode is 'chrome':
driver = webdriver.Chrome(get_settings()['chrome_driver_path'])
elif mode is 'phantom':
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/60.0.3112.113 Safari/537.36')
driver = webdriver.PhantomJS(executable_path=get_settings()['phantom_driver_path'],
desired_capabilities=dcap,
service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
driver.set_window_size(1920, 1080)
else:
raise NoSuchDriverException()
return driver
def wait_until_load(driver, by, locator, timeout=30):
"""
:param driver: selenium driver loading page
:param by: locating method 'id', 'xpath', 'class'
:param locator: proper locator according to param by
:param timeout: maximum time to wait. After this time elapsed, TimeoutException will be raised.
:return: web element object waiting for
"""
if by is 'id':
return WebDriverWait(driver, timeout).until(ec.presence_of_element_located((By.ID, locator)))
elif by is 'xpath':
return WebDriverWait(driver, timeout).until(ec.presence_of_element_located((By.XPATH, locator)))
elif by is 'class':
return WebDriverWait(driver, timeout).until(ec.presence_of_element_located((By.CLASS_NAME, locator)))
else:
raise NoSuchLocateMethodException()
def login_pf_center(driver):
settings, locators = get_settings_and_locators()
to_login_page(driver)
wait_until_load(driver, 'id', locators['login_email_input_id']).send_keys(settings['admin_info']['email'])
wait_until_load(driver, 'id', locators['login_pw_input_id']).send_keys(settings['admin_info']['pw'])
wait_until_load(driver, 'id', locators['login_submit_btn_id']).click()
return driver
def to_login_page(driver):
driver.get(get_locators()['pf_login_url'])
def to_pf_center(driver):
# 'center' refers to the main page of plus friend center
driver.get(get_locators()['pf_center_url'])
return driver
def to_pf_home(driver):
# 'home' refers to the home page of plus friend specified in setting.json
driver.get(get_settings()['pf_home_url'])
return driver