Skip to content

Instantly share code, notes, and snippets.

View DxDiagDx's full-sized avatar
:octocat:

Evgeny Lukin DxDiagDx

:octocat:
View GitHub Profile
@DxDiagDx
DxDiagDx / environs
Created January 13, 2023 08:56
environs - Переменные окружения Python
from dataclasses import dataclass
from environs import Env
@dataclass
class Bots:
bot_token: str
admin_id: str
@DxDiagDx
DxDiagDx / db.py
Last active November 24, 2022 05:38
Скрипт для работы с базой данных SQLite
import sqlite3
from typing import Dict, List, Tuple
def insert(database: str, table: str, column_values: Dict):
with sqlite3.connect(database) as conn:
cursor = conn.cursor()
columns = ", ".join(column_values.keys())
values = [tuple(column_values.values())]
placeholders = ", ".join("?" * len(column_values.keys()))
@DxDiagDx
DxDiagDx / services.py
Created November 7, 2022 07:46
Python - services.py - Вспомогательные функции для парсинга сайтов
import os
import csv
import time
import json
import requests
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
}
@DxDiagDx
DxDiagDx / get_data_with_mysql.py
Created October 26, 2022 12:31
WooCommerce - MySQL - Python: получить информацию о товарах из базы данных
from sshtunnel import SSHTunnelForwarder
import pymysql
host = 'test.beget.tech'
login = 'test'
password = 'test'
user_db = 'test_db'
password_db = 'test'
@DxDiagDx
DxDiagDx / flashscore.py
Created October 10, 2022 05:49
Flashscore — парсим статистику с помощью Python
import json
import requests
from datetime import datetime
headers = {"x-fsign": "SW9D1eZo"}
def main():
feed = 'f_1_-1_3_ru_5'
@DxDiagDx
DxDiagDx / get_my_ip.py
Last active July 20, 2023 18:17
Python узнать свой IP-адрес
import requests
response = requests.get('http://jsonip.com')
ip = response.json()['ip']
print('Your public IP is:', ip)
@DxDiagDx
DxDiagDx / get_response.py
Created September 29, 2022 12:26
Python Requests — запрос в несколько попыток
import time
import requests
def get_response(metod, attempts=3, pause=3, **kwargs):
response = None
for attempt in range(1, attempts + 1):
try:
if 'get' in metod:
@DxDiagDx
DxDiagDx / woo_get_tree_categories.py
Created September 19, 2022 10:44
Woo API — экспорт дерева категорий. WooCommerce Get Categories Tree
import csv
from woocommerce import API
from config import consumer_key, consumer_secret, site
wcapi = API(
url=site,
consumer_key=consumer_key,
consumer_secret=consumer_secret,
@DxDiagDx
DxDiagDx / woo_set_category_images.py
Last active June 28, 2022 10:23
Woo API — присваиваем категориям изображения
from config import wcapi
def update_category_image(category):
category_id = category['category_id']
data = {
"image": {
"src": category['image']
}
}
@DxDiagDx
DxDiagDx / get_response.py
Created June 28, 2022 10:01
Woo API — get_response: отправляем повторный запрос при неудачной попытке
def get_response(metod, endpoint=None, params=None, data=None):
response = None
for attemp in range(1, 6):
try:
if 'get' == metod.lower():
response = wcapi.get(endpoint=endpoint, params=params)
if 'post' == metod.lower():
response = wcapi.post(endpoint=endpoint, params=params, data=data)
except Exception as error:
print('Ошибка:', error)