Skip to content

Instantly share code, notes, and snippets.

Avatar

Evgeny Lukin DxDiagDx

View GitHub Profile
@DxDiagDx
DxDiagDx / environs
Created Jan 13, 2023
environs - Переменные окружения Python
View environs
from dataclasses import dataclass
from environs import Env
@dataclass
class Bots:
bot_token: str
admin_id: str
@DxDiagDx
DxDiagDx / db.py
Last active Nov 24, 2022
Скрипт для работы с базой данных SQLite
View db.py
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 Nov 7, 2022
Python - services.py - Вспомогательные функции для парсинга сайтов
View 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 Oct 26, 2022
WooCommerce - MySQL - Python: получить информацию о товарах из базы данных
View get_data_with_mysql.py
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 Oct 10, 2022
Flashscore — парсим статистику с помощью Python
View flashscore.py
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
Created Oct 7, 2022
Python узнать свой IP-адрес
View get_my_ip.py
import requests
response = requests.get('http://jsonip.com', proxies=proxies)
ip = response.json()['ip']
print('Your public IP is:', ip)
@DxDiagDx
DxDiagDx / get_response.py
Created Sep 29, 2022
Python Requests — запрос в несколько попыток
View get_response.py
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 Sep 19, 2022
Woo API — экспорт дерева категорий. WooCommerce Get Categories Tree
View woo_get_tree_categories.py
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 Jun 28, 2022
Woo API — присваиваем категориям изображения
View woo_set_category_images.py
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 Jun 28, 2022
Woo API — get_response: отправляем повторный запрос при неудачной попытке
View get_response.py
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)