Skip to content

Commit

Permalink
feat(🛠): first working version
Browse files Browse the repository at this point in the history
  • Loading branch information
Baelfire18 committed Aug 14, 2022
0 parents commit f9e7850
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
env/
36 changes: 36 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# UC Sports Attendance Marker

<a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>

Ante la inminente lata de rellenar tremendo form para marcar asitencia de mi deportivo todas las clases <https://docs.google.com/forms/d/e/1FAIpQLScaPxGIKPDwFNlVJcKmRRy7JHDUKQExt1fbrsIMvhwxJWt3kA/viewform>, cree un bot que lo hace por mí, prerellenando datos del usuario y solo pregutando por semana y día para marcar.

## Setear el ambiente

```bash
python3 -m venv env
```

### Activar ambiente en Windows

```bash
env\Scripts\activate
```

### Activar ambiente en Linux o MAC

```bash
source env/bin/activate
```

## Instalar dependencias

```bash
pip install -r requirements.txt
```

## Ejecutar


```bash
python3 main.py
```
Binary file added chromedriver.exe
Binary file not shown.
11 changes: 11 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Correo UC
EMAIL=

# Primer nombre y 2 apellidos (utilice MAYÚSCULA)
FULL_NAME=

# RUT
RUT=

# NRC (solo números)
NRC=
135 changes: 135 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import os
import platform
import time
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options


class FormBot(webdriver.Chrome):
def __init__(self, FORM_URL):
chrome_options = Options()
chrome_options.add_argument("log-level=3")

if platform.system() == "Darwin":
driver_location = os.path.join(os.getcwd(), "chromedriver")
super().__init__(executable_path=driver_location, options=chrome_options)
else:
super().__init__(options=chrome_options)

self.get(FORM_URL)

def fill_email(self, email):
email_input = self.find_element(
By.XPATH,
'//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div[1]/div[2]/div[1]/div/div[1]/input',
)
email_input.send_keys(email)

def fill_week(self, week):
n = (week - 1) * 3 + 9
self.find_element(By.XPATH, f'//*[@id="i{n}"]/div[3]/div').click()

def fill_name(self, full_name):
name_input = self.find_element(
By.XPATH,
'//*[@id="mG61Hd"]/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div[1]/div/div[1]/input',
)
name_input.send_keys(full_name)

def fill_rut(self, rut):
rut_input = self.find_element(
By.XPATH,
'//*[@id="mG61Hd"]/div[2]/div/div[2]/div[4]/div/div/div[2]/div/div[1]/div/div[1]/input',
)
rut_input.send_keys(rut)

def fill_nrc(self, nrc):
nrc_input = self.find_element(
By.XPATH,
'//*[@id="mG61Hd"]/div[2]/div/div[2]/div[5]/div/div/div[2]/div/div[1]/div/div[1]/input',
)
nrc_input.send_keys(nrc)

def fill_day(self, day):
n = (day - 1) * 3 + 73
self.find_element(By.XPATH, f'//*[@id="i{n}"]/div[3]/div').click()

def submit(self):
self.find_element(
By.XPATH, '//*[@id="mG61Hd"]/div[2]/div/div[3]/div[1]/div[1]/div/span'
).click()


def check_valid_number(field, max_num):
num = 0
while num not in range(1, max_num + 1):
try:
num = int(input(f"Ingresa {field} que quieres llenar (1-{max_num}): "))
except ValueError:
print("ERROR: Ingresa un número")
continue
if num not in range(1, max_num + 1):
print(f"ERROR: Ingresa un número entre 1 y {max_num}")
return num


if ".env" not in os.listdir(os.getcwd()):
print(
"""No hay un archivo .env con tus credenciales.
Crearemos uno para ti, rellena tus datos"""
)
time.sleep(2)

email = input("Email: ")
full_name = input("Primer nombre y 2 apellidos (utilice MAYÚSCULA): ")
rut = input("RUT: ")
nrc = input("NRC (solo números): ")

with open(".env", "w") as f:
f.write(f"EMAIL={email}\n")
f.write(f"FULL_NAME={full_name}\n")
f.write(f"RUT={rut}\n")
f.write(f"NRC={nrc}\n")

load_dotenv()

# Credencials
FORM_URL = "https://docs.google.com/forms/d/e/1FAIpQLScaPxGIKPDwFNlVJcKmRRy7JHDUKQExt1fbrsIMvhwxJWt3kA/viewform"
EMAIL = os.environ.get("EMAIL")
FULL_NAME = os.environ.get("FULL_NAME")
RUT = os.environ.get("RUT")
NRC = os.environ.get("NRC")

print(
f"""Las credenciales de tu ".env" son:
Email: {EMAIL}
Nombre: {FULL_NAME}
RUT: {RUT}
NRC (sólo números): {NRC}
De haber algún error en ellas, abre el archivo ".env",
corrigelas y vuelve a correr el programa.
"""
)

WEEK = check_valid_number("semana", 16)
DAY = check_valid_number("día", 3)

try:
bot = FormBot(FORM_URL)
bot.fill_email(EMAIL)
bot.fill_week(WEEK)
bot.fill_name(FULL_NAME)
bot.fill_rut(RUT)
bot.fill_nrc(NRC)
bot.fill_day(DAY)
# bot.submit()
print("Asitencia marcada con éxito!")

except Exception as e:
print(e)

finally:
time.sleep(5)
bot.close()
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
selenium===4.4.0
python-dotenv===0.20.0
pip===22.2.2

0 comments on commit f9e7850

Please sign in to comment.