add user-agent list

This commit is contained in:
2022-03-21 17:13:11 +01:00
parent 68680af13b
commit a5b172d4d6
3 changed files with 113 additions and 72 deletions
+99 -70
View File
@@ -1,15 +1,18 @@
import asyncio import asyncio
import subprocess import logging
import random
import threading
import time import time
from concurrent.futures import ProcessPoolExecutor
import playwright
from playwright.async_api import async_playwright
from playwright.sync_api import sync_playwright from playwright.sync_api import sync_playwright
from pojo.contact_pojo import ContactPojo from pojo.contact_pojo import ContactPojo
from utils.excel_reader import ExcelHelper
# RDV_URL = "https://rendezvousparis.hermes.com/client/register" RDV_URL = "https://rendezvousparis.hermes.com/client/register"
RDV_URL = "file:///Users/lpan/Downloads/test_appointment.html"
# RDV_URL = "file:///Users/lpan/Downloads/test_appointment.html"
# RDV_URL = "https://www.google.fr" # RDV_URL = "https://www.google.fr"
@@ -17,103 +20,129 @@ RDV_URL = "file:///Users/lpan/Downloads/test_appointment.html"
# RDV_URL = "https://api.ipify.org" # RDV_URL = "https://api.ipify.org"
global_page = None
otp_value = None
user_agent_list = ExcelHelper().read_user_agent_list()
class CommandorPage: class CommandorPage:
def __init__(self): def __init__(self):
pass pass
async def start_page(self, proxy, contact: ContactPojo): def _run(self, e: threading.Event, proxy, contact):
with sync_playwright() as pwright:
self.browser = pwright.chromium.launch(headless=False, timeout=90000, proxy=proxy)
self.page = self.browser.new_page(
user_agent=random.choice(user_agent_list))
global global_page
global_page = self
self.page.on("load", self.on_page_loaded)
self.page.goto(RDV_URL, timeout=90000)
# content = self.page.content()
self.setPhoneCountry()
self.setPhoneNumber(contact.phone)
self.setName(contact.last_name, contact.first_name)
self.setEmail(contact.mail)
self.setIdNumber(contact.passport)
self.checkCgu()
self.page.on("domcontentloaded", self.on_document_loaded)
self.clickOnValidBtn()
event_is_set = e.wait()
logging.debug('event set: %s', event_is_set)
global otp_value
self.send_otp(otp_value)
self.clickOnValidBtn()
self.page.on("load", self.on_page_loaded)
time.sleep(4000)
# time.sleep(1000)
def start_page(self, proxy, contact: ContactPojo) -> threading.Event:
# specifying an explicit component name # specifying an explicit component name
async with async_playwright() as pwright: e = threading.Event()
self.browser = await pwright.chromium.launch(headless=False, timeout=30000, proxy=proxy) t = threading.Thread(target=self._run, args=(e, proxy, contact))
self.page = await self.browser.new_page( t.start()
user_agent="Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.87 Mobile Safari/537.36") return e
await self.page.goto(RDV_URL)
content = await self.page.content()
await self.setPhoneCountry()
await self.setPhoneNumber(contact.phone)
await self.setName(contact.last_name, contact.first_name)
await self.setEmail(contact.mail)
await self.setIdNumber(contact.passport)
await self.checkCgu()
await self.clickOnValidBtn()
# page.on('load', self.on_page_loaded)
print(content)
await asyncio.sleep(1000)
def on_page_loaded(self): def on_page_loaded(self):
print("page loaded") print("page loaded")
print("content is " + self.page.content())
print("url is " + self.page.url)
self.getErrors()
async def setPhoneCountry(self): def on_response(self):
await self.page.evaluate("""()=>document.getElementById("phone_country").value = \"FR\" """) print("on_response called")
async def setPhoneNumber(self, phoneNumber): def on_document_loaded(self):
await self.page.evaluate("""(phoneNumber)=>document.getElementById("phone_number").value =phoneNumber""", print("on_document_loaded called")
phoneNumber)
async def setName(self, lastName, firstName): def setPhoneCountry(self):
await self.page.evaluate("""(name)=> { self.page.evaluate("""()=>document.getElementById("phone_country").value = \"FR\" """)
def setPhoneNumber(self, phoneNumber):
self.page.evaluate("""(phoneNumber)=>document.getElementById("phone_number").value =phoneNumber""",
phoneNumber)
def setName(self, lastName, firstName):
self.page.evaluate("""(name)=> {
document.getElementById("surname").value = name.lastName; document.getElementById("surname").value = name.lastName;
document.getElementById("name").value = name.firstName}""", {'lastName': lastName, 'firstName': firstName}) document.getElementById("name").value = name.firstName}""", {'lastName': lastName, 'firstName': firstName})
async def setEmail(self, email): def getErrors(self):
await self.page.evaluate("""(email)=>document.getElementById("email").value = email""", email) items = self.page.query_selector(".alert")
if items:
print(items.inner_html())
async def setIdNumber(self, id): def setEmail(self, email):
await self.page.evaluate(""" (id) => document.getElementById("passport_id").value = id""", id) self.page.evaluate("""(email)=>document.getElementById("email").value = email""", email)
async def checkCgu(self): def setIdNumber(self, id):
await self.page.evaluate("""document.getElementById("cgu").checked = true; self.page.evaluate(""" (id) => document.getElementById("passport_id").value = id""", id)
def checkCgu(self):
self.page.evaluate("""document.getElementById("cgu").checked = true;
document.getElementById("processing").checked = true""") document.getElementById("processing").checked = true""")
async def clickOnValidBtn(self): def clickOnValidBtn(self):
await self.page.evaluate("""document.getElementsByClassName("btn")[0].click();""") self.page.evaluate("""document.getElementsByClassName("btn")[0].click();""")
def clear_app_data(self): def clear_app_data(self):
pass pass
def send_otp(self, otp: str): def send_otp(self, otp: str):
self.page.evaluate(""" (otp)=> document.getElementById("sms_code").value = otp""", otp) self.page.evaluate("""(otp)=> document.getElementById("sms_code").value = otp""", otp)
def reset_air_plan_mode(self): def reset_air_plan_mode(self):
subprocess.call( pass
"/Users/panlei/Library/Android/sdk/platform-tools/adb shell settings put global airplane_mode_on 1",
shell=True)
time.sleep(1)
subprocess.call(
"/Users/panlei/Library/Android/sdk/platform-tools/adb shell am broadcast -a android.intent.action.AIRPLANE_MODE",
shell=True)
time.sleep(1)
subprocess.call(
"/Users/panlei/Library/Android/sdk/platform-tools/adb shell settings put global airplane_mode_on 0",
shell=True)
time.sleep(1)
subprocess.call(
"/Users/panlei/Library/Android/sdk/platform-tools/adb shell am broadcast -a android.intent.action.AIRPLANE_MODE",
shell=True)
time.sleep(10)
async def main(): def main():
contact = ContactPojo(phone_number="755667750", passport_number="5123456789", last_name="PAN", first_name="Lei", executor = ProcessPoolExecutor(2)
mail="panleicim@gmail.com", ccid="", position=0) loop = asyncio.get_event_loop()
loop.run_in_executor(executor, wait_for_otp)
loop.run_in_executor(executor, launch_page)
def launch_page() -> threading.Event:
proxy = {
"server": "http://gw.ntnt.io:5959",
"username": "panleicim-cc-fr-sid-179112",
"password": "M3PZAXgW5V27"
}
contact = ContactPojo(phone_number="+33751177505", passport_number="9933251119", last_name="Chen", first_name="Fan",
mail="lei_chena09@outlook.com", ccid="", position=0)
page = CommandorPage() page = CommandorPage()
await page.start_page(None, contact) return page.start_page(proxy, contact)
# task = asyncio.create_task(wait_for_otp())
# await task
async def wait_for_otp(): def wait_for_otp():
sec = input("Press Enter to continue...") sec = input("Press Enter otp to continue...")
time.sleep(20) asyncio.sleep(1)
print("input otp is: " + sec) print("input otp is: " + sec)
if __name__ == '__main__': if __name__ == '__main__':
proxy = { event = launch_page()
"server": "http://gw.ntnt.io:5959", sec = input("Press Enter otp to continue...")
"username": "panleicim-cc-any-sid-1112", print("input otp is: " + sec)
"password": "M3PZAXgW5V27" otp_value = sec
} event.set()
asyncio.gather(main(), wait_for_otp())
time.sleep(1000)
Binary file not shown.
+14 -2
View File
@@ -2,6 +2,7 @@ import json
import pandas as pandas import pandas as pandas
import definitions
from pojo.contact_pojo import ContactPojo from pojo.contact_pojo import ContactPojo
@@ -24,7 +25,17 @@ class ExcelHelper:
self.write_to_exel("ccid_list.xlsx", ccids.split(",")) self.write_to_exel("ccid_list.xlsx", ccids.split(","))
print(lines) print(lines)
# read the contact list from the exel file def read_user_agent_list(self):
# read the contact list from the exel file
contact_list_in_json = pandas.read_excel(definitions.ROOT_DIR + "/docs/mobile_user_agent_list.xlsx").to_json(
orient='records')
contact_dict_list = json.loads(contact_list_in_json)
user_agents = []
for contact_dict in contact_dict_list:
user_agent = contact_dict['user_agent']
user_agents.append(user_agent)
return user_agents
def read_contacts(self) -> list: def read_contacts(self) -> list:
contact_list_in_json = pandas.read_excel(r'./contact.xlsx').to_json(orient='records') contact_list_in_json = pandas.read_excel(r'./contact.xlsx').to_json(orient='records')
contact_dict_list = json.loads(contact_list_in_json) contact_dict_list = json.loads(contact_list_in_json)
@@ -46,4 +57,5 @@ class ExcelHelper:
if __name__ == '__main__': if __name__ == '__main__':
helper = ExcelHelper() helper = ExcelHelper()
helper.generate_exel_from_txt() # helper.generate_exel_from_txt()
print(helper.read_user_agent_list())