python

Jupyter Notebook SerpApi websearch

import requests
import os

API_KEY = os.environ["SERP_API"]

def search_and_save(query):
    url = "https://serpapi.com/search.json"

    params = {
        "q": query,
        "api_key": API_KEY
    }

    response = requests.get(url, params=params)
    data = response.json()

    results = data.get("organic_results", [])

    # Create folder if it doesn't exist
    folder = "./search_results"
    os.makedirs(folder, exist_ok=True)

    # Create filename (replace spaces with -)
    safe_query = query.replace(" ", "-").replace(".","")
    file_path = os.path.join(folder, f"{safe_query}.txt")

    # Write results to file
    with open(file_path, "w", encoding="utf-8") as f:
        for r in results:
            title = r.get("title", "")
            link = r.get("link", "")
            snippet = r.get("snippet", "")

            f.write(f"{title}\n")
            f.write(f"{link}\n")
            f.write(f"{snippet}\n")
            f.write("-" * 50 + "\n")

    print(f"Saved results to {file_path}")

# Example usage
search_and_save("Jupyter notebook tutorials.")

Login ssh automation

import sys
import pyperclip
import subprocess

if len(sys.argv) == 1:
    print('login func needs client to login...')
elif sys.argv[1] == 'do':
    print('login at do, use paste')
    cb = pyperclip.copy('Password')
    bashCommand = "echo apt update"
    subprocess.call(["ssh", "user@ip.address.0.0"])
else:
    print('No login available for '+ sys.argv[1])

sys.exit(1)

adjust zshrc in

~/.zshrc add location python3 and filelocation

alias li="/Users/hjhubeek/Documents/development/python/li/bin/python /Users/hjhubeek/Documents/development/python/li/li.py"

json call rest

import urllib, json
>>> url = "http://maps.googleapis.com/maps/api/geocode/json?address=googleplex&sens..."
>>> url = "http://myexternalip.com/json"
>>> response = urllib.urlopen(url);
>>> data = json.loads(response.read())
>>> print data
{u'ip': u'80.57.121.251'}

Tags: 
Subscribe to RSS - python