Posted on Leave a comment

Python Script to Query Elasticsearch

Here is a basic Python script to query Elasticsearch using the elasticsearch library. Very useful for data mining.

Here is a basic Python script to query Elasticsearch using the elasticsearch library:

from elasticsearch import Elasticsearch

# Connect to Elasticsearch cluster
es = Elasticsearch(["http://localhost:9200/"])

# Define query body
query_body = {
    "query": {
        "match_all": {}
    }
}

# Execute search
result = es.search(index="your_index_name", body=query_body)

# Extract search hits
hits = result["hits"]["hits"]

# Print results
for hit in hits:
    print(hit["_source"])

In this script, the elasticsearch library is imported, and a connection to an Elasticsearch cluster is established. Then, a query body is defined as a dictionary, which includes a match_all query. Finally, the search method is called on the es instance to execute the query, and the search hits are extracted from the result and printed.

Posted on Leave a comment

Python Script to Query Active Directory

Here is a basic Python script to query Active Directory using the “ldap3” library. Useful script code for authentication.

import ldap3

server = ldap3.Server("ldap://hostname:port/")
conn = ldap3.Connection(server, "CN=username,DC=domain,DC=com", "password")
conn.bind()

query = "(&(objectCategory=person)(objectClass=user)(cn=*))"
conn.search("DC=domain,DC=com", query, attributes=["cn", "mail", "memberOf"])

result = conn.response

for item in result:
    print(item["attributes"])

conn.unbind()

Replace the placeholders with the appropriate values for your Active Directory:

  • “hostname” and “port” are the address and port of the Active Directory server
  • “username” is the username of an account with permission to search the directory
  • “password” is the password for the account
  • “domain” is the domain name of your Active Directory
  • “cn=*” can be modified to search for specific common names.

The “attributes” list in the conn.search() call can be modified to retrieve other properties of the users in the directory.

Posted on Leave a comment

Python Script to Query POSTGRESQL Database

Here is a basic Python script to query a PostgreSQL database using the “psycopg2” library. Very useful for data mining.

import psycopg2

conn = psycopg2.connect(
    host="hostname",
    database="database_name",
    user="username",
    password="password"
)

cursor = conn.cursor()

query = "SELECT * FROM table_name"
cursor.execute(query)

result = cursor.fetchall()

for row in result:
    print(row)

conn.close()

Replace the placeholders with the appropriate values for your database:

  • “hostname” is the address of the database server
  • “database_name” is the name of the database you want to connect to
  • “username” and “password” are the credentials used to access the database
  • “table_name” is the name of the table you want to query.

You can modify the SELECT statement to retrieve specific columns or filter the data as desired.

Posted on Leave a comment

Python Script to Query SQL Database

Here is a basic Python script to query a SQL database using the “sqlite3” library. Very useful for data mining.

import sqlite3

conn = sqlite3.connect('database.db')
cursor = conn.cursor()

query = "SELECT * FROM table_name"
result = cursor.execute(query).fetchall()

for row in result:
    print(row)

conn.close()

Replace “database.db” with the name of your SQLite database file, and “table_name” with the name of the table you want to query. You can modify the SELECT statement to retrieve specific columns or filter the data as desired.

Posted on Leave a comment

Python Script to Download Youtube Videos

Here is an example of a Python script that uses the “pytube” library to download a YouTube video:

from pytube import YouTube

link = "https://www.youtube.com/watch?v=Nj6PFaDmp6c"
yt = YouTube(link)

#Download the video with the highest resolution
video = yt.get('mp4', '720p')

#Set the filename and download location
video.download("/path/to/save/video")

You can also download the audio-only version of a video by using the yt.streams.filter(only_audio=True) method, and then calling the download() method on the audio stream.

from pytube import YouTube

link = "https://www.youtube.com/watch?v=Nj6PFaDmp6c"
yt = YouTube(link)

# Download audio-only version
audio_stream = yt.streams.filter(only_audio=True).first()
audio_stream.download("/path/to/save/audio")

It is important to note that downloading videos from YouTube without permission from the video owner or without a valid reason is against YouTube’s terms of service and also it may be illegal depending on your country laws.

Posted on Leave a comment

Python Script to Download Images from URL

Here is an example Python script that downloads images from a given URL and saves them to a local directory:

import requests

def download_image(url, save_path):
    # Open the image URL
    response = requests.get(url)
    
    # Open the save path
    with open(save_path, 'wb') as f:
        # Write the image data to the save path
        f.write(response.content)

# Example usage
url = "https://images.unsplash.com/photo-1593642532857-b1ebc8e7c147"
save_path = "image.jpg"
download_image(url, save_path)

This script uses the requests library to make a GET request to the image URL, which returns the image data. It then opens a file at the specified save path, and writes the image data to it in binary mode. You can use a loop and a list of urls to download multiple images or use a function to pass the url and save_path as arguments. You can also customize this script to handle errors, such as if the URL is not valid or the image cannot be downloaded for some reason. You can also add file extensions to the save path depending on the type of image you are trying to download.

Posted on Leave a comment

Python Script to Convert JSON to CSV

Here is an example Python script that converts a JSON file to a CSV file:

import json
import csv

# Open the JSON file
with open('data.json', 'r') as json_file:
    json_data = json.load(json_file)

# Open a CSV file for writing
with open('data.csv', 'w', newline='') as csv_file:
    # Create a CSV writer
    csv_writer = csv.writer(csv_file)
    
    # Write the headers
    csv_writer.writerow(json_data[0].keys())

    # Write the data
    for row in json_data:
        csv_writer.writerow(row.values())

This script assumes that the JSON file is an array of objects, where each object represents a row in the CSV file. It will write the keys of the first object to the headers of the csv file. It will then write the values of the each object to the csv file. It will use json library to load the json data and csv library to write the data to csv file You can customize this script to suit the specific format of your JSON data and the desired structure of your CSV file.

Posted on Leave a comment

Python Script to Replace String in XML File

Here is a Python script that can be used to replace a specific string in an XML file:

import xml.etree.ElementTree as ET

# specify the input and output file paths
xml_file = 'input.xml'
output_file = 'output.xml'

# specify the string to be replaced and its replacement
old_string = 'old_string'
new_string = 'new_string'

# parse the XML file
tree = ET.parse(xml_file)
root = tree.getroot()

# search for the string in the XML elements and replace it
for elem in root.iter():
    if old_string in elem.text:
        elem.text = elem.text.replace(old_string, new_string)
    for at in elem.attrib:
        if old_string in elem.attrib[at]:
            elem.attrib[at] = elem.attrib[at].replace(old_string, new_string)

# write the modified XML to the output file
tree.write(output_file)

Please note that this script will replace all instances of the specified string in the XML file, both in the element text and in the element attributes. If you want to replace only in a specific element you can use the xpath functionality in the ElementTree library.

root.find('.//element_name').text = new_string

This will replace the string only in the element_name element.

Posted on Leave a comment

Python Script to Replace Text in Multiple Files

Here is an example of a Python script that replaces a specific text in multiple files:

import os
import glob

# specify the directory containing the files
directory = '/path/to/directory'

# specify the file extension
file_extension = '*.txt'

# specify the text to be replaced and the replacement text
old_text = 'old_text'
new_text = 'new_text'

# get a list of all the files in the directory with the specified extension
files = glob.glob(os.path.join(directory, file_extension))

# loop through the files
for file_path in files:
    # open the file and read its contents
    with open(file_path, 'r') as file:
        file_contents = file.read()
    
    # replace the text
    new_contents = file_contents.replace(old_text, new_text)
    
    # open the file again in write mode
    with open(file_path, 'w') as file:
        file.write(new_contents)

This script uses the glob library to find all the files with the specified extension in the specified directory, then loops through each file and replaces the old text with the new text. You can change the directory, file extension, old text, and new text to suit your needs. Also, as an alternative, you can use os.scandir() to loop through all the files in the directory, and then use the same logic to replace the text in each file.

Posted on Leave a comment

Python Script to Replace Text in a File

Here is an example of a Python script that replaces a specific text in a file:

import os

# specify the file path
file_path = '/path/to/file.txt'

# specify the text to be replaced and the replacement text
old_text = 'old_text'
new_text = 'new_text'

# open the file and read its contents
with open(file_path, 'r') as file:
    file_contents = file.read()

# replace the text
new_contents = file_contents.replace(old_text, new_text)

# open the file again in write mode
with open(file_path, 'w') as file:
    file.write(new_contents)

This script opens the file at the specified path, reads its contents, replaces the old text with the new text, and writes the new contents back to the file. You can change the file path, old text, and new text to suit your needs. Also, you can use the glob library to find all the files with a specific extension, and then use the above script to replace the text in each file.