Skip to content

Instantly share code, notes, and snippets.

@ivgomezarnedo
ivgomezarnedo / deploy-lambda.yml
Created September 22, 2023 07:34
Automate the deployment of a Lambda every time that a new commit is added to the "main" branch.
name: Deploy Python Lambda
on:
push:
branches:
- main
paths:
- '**.py' # Change to the file extension of the language that you are using.
jobs:
@ivgomezarnedo
ivgomezarnedo / recursive_column_names.py
Created December 23, 2022 09:32
This function will return all column names of a Dataframe, including all the nested fields. The output will be a list of fields with a format like: <root_name>.<root_column_name>[.<sub_column_name>….]
def extract_names(json_obj, name = "root"):
names = []
if isinstance(json_obj, dict):
if "name" in json_obj:
name = json_obj["name"]
names.append(json_obj["name"])
for key, value in json_obj.items():
names.extend(extract_names(value, name=name))
elif isinstance(json_obj, list):
for item in json_obj:
@ivgomezarnedo
ivgomezarnedo / thumbnail_generator_lambda.py
Last active March 23, 2023 22:41
Lambda function that generates a thumbnail for any image file uploaded to S3.
import os
import json
import boto3
from PIL import Image
import pathlib
import urllib
# Read from environmental variables
ID = os.environ['AWS_ID']
KEY = os.environ['AWS_KEY']
@ivgomezarnedo
ivgomezarnedo / sheets_compressed_to_table.py
Last active March 23, 2023 22:41
Download a zip file from the internet, extract it and, for each Excel file downloaded, transform its sheets to CSV removing all rows until the header is found.
import zipfile
import glob
from pathlib import Path
import urllib
import pandas as pd
zip_path = "/home/user/files/zip"
xlsx_path = "/home/user/files/xlsx"
csv_path = "/home/user/files/csv"
file_name = "downloaded.zip"
@ivgomezarnedo
ivgomezarnedo / predict_using_local_model.py
Last active March 23, 2023 22:41
Using feature inputs, Load model/scaler from local file and predict the target
import pickle
from datetime import datetime
def predict_recaudacion(feature_1_date: str, feature_2: float, feature_3: float, feature_4: float):
week_day, month = transform_date_inputs(feature_1_date)
# Load model
model_file = 'model.sav'
loaded_model = pickle.load(open(model_file, 'rb'))
# Load scaler
scaler_file = 'scaler.pkl'
@ivgomezarnedo
ivgomezarnedo / url_combinations.py
Last active March 23, 2023 22:42
Given a URL and a Date, generate possible URLs using combinations and check if one of them is the correct one.
import itertools
from datetime import datetime, timedelta
import urllib.request
url_base = "https://www.web.es/f/files/Press%20notes/PRESS_NOTE_DATE_{0}.pdf" #EXAMPLE (Change it)
def check_if_url_exists(url):
try:
request = urllib.request.Request(url)
@ivgomezarnedo
ivgomezarnedo / pulse_secure_part_2.py
Last active March 23, 2023 22:43
Code with the process to interact with the 2-factor widget, copy the key and paste it into the Pulse Secure dialog. Part 2 of the full process
import pywinauto
import pyperclip
def get_two_factor_widget_app(path_to_widget_app):
try:
app2 = pywinauto.Application().connect(path=path_to_widget_app)
except pywinauto.application.ProcessNotFoundError as ex:
print("2-factor process not found. Launching it...")
pywinauto.Application().start(path_to_widget_app)
app2 = pywinauto.Application().connect(path=path_to_widget_app)
@ivgomezarnedo
ivgomezarnedo / pulse_secure_part_1.py
Last active March 23, 2023 22:42
Code with the process to interact with the Pulse Secure Cancel/Connect buttons. Part 1 of the full process
import pywinauto
def cancel_and_connect_to_VPN(app):
try:
app['Pulse Secure']['Cancel'].click()
time.sleep(5)
print("Application canceled, clicking in Connect...")
app['Pulse Secure']['Connect'].click()
except pywinauto.MatchError as me:
print("Cancel button doesn't exist. Clicking in Connect...")
app['Pulse Secure']['Connect'].click()
@ivgomezarnedo
ivgomezarnedo / Full_process_pulse_secure_vpn_reconnect.py
Last active March 23, 2023 22:43
GIST with Pywinauto code to automate connection to a 2-Factor Pulse Secure VPN. Full process
import pywinauto
import time
import pyperclip
import subprocess
from pywinauto import Desktop
from datetime import datetime
import warnings
warnings.simplefilter('ignore', category=UserWarning) # Ignore pywinauto warnings
# https://github.com/pywinauto/pywinauto/issues/530
@ivgomezarnedo
ivgomezarnedo / telegram_utils.py
Last active March 23, 2023 22:42
Telegram methods used to send messages or exceptions to a Telegram group
import telegram
import time
import traceback
bot = telegram.Bot(token=TELEGRAM_TOKEN)
def send_to_telegram(message, group_id=TELEGRAM_METRICS_GROUP_ID):
"""