from django.db.models import Q
from collections import defaultdict
def catalog(request):
categories = {
1: "wallets",
2: "bags",
3: "belts",
...
}
products = Products.objects.filter(gender="men").filter(categories__in=categories.keys()). select_related("gender", "categories").only("id", "title", "price", "image")
categorized_products = defaultdict(list)
for product in products:
category = categories[product.categories_id]
categorized_products[category].append(product)
context = categorized_products
return render(request, 'catalog.html', context)
module = importlib.import_module(f'actions.{module_name}')
class PostSerializer(serializers.ModelSerializer):
categories = serializers.StringRelatedField(many=True)
comments_count = serializers.SerializerMethodField()
class Meta:
model = Post
fields = ('id', 'title', 'body', 'categories', 'comments_count')
def get_comments_count(self, obj):
return obj.comments.count()
class category(models.Model):
category_name = models.CharField(max_length=70, verbose_name='Категория')
class Meta:
verbose_name = "Категории товаров"
def __str__(self):
return self.category_name
def get_related_items(self):
related_items = []
related_items.extend(list(self.sup_set.all()))
related_items.extend(list(self.paddle_set.all()))
related_items.extend(list(self.boot_set.all()))
return related_items
{% for category in categories %}
<h2>{{ category.category_name }}</h2>
<ul>
{% for item in category.get_related_items %}
<li>{{ item.name }}</li>
{% endfor %}
</ul>
{% endfor %}
from fastapi import FastAPI
import re
import netifaces
from wsdiscovery.discovery import ThreadedWSDiscovery as WSDiscovery
app = FastAPI()
class Finder:
def __init__(self):
self.ips = list()
for iface in netifaces.interfaces():
if netifaces.AF_INET in netifaces.ifaddresses(iface):
self.ips.append(
netifaces.ifaddresses(iface)[netifaces.AF_INET][0]["addr"]
)
self.scope = [".".join(ip.split(".")[:2]) for ip in self.ips]
self.wsd = WSDiscovery()
def start(self):
self.wsd.start()
self.ret = self.wsd.searchServices()
self.wsd.stop()
self.onvif_services = [
s for s in self.ret if str(s.getTypes()).find("onvif") >= 0
]
self.urls = [ip for s in self.onvif_services for ip in s.getXAddrs()]
self.ips = [
ip for url in self.urls for ip in re.findall(r"\d+\.\d+\.\d+\.\d+", url)
]
self.lst = [
ip for ip in self.ips if any(ip.startswith(sp) for sp in self.scope)
]
return {"results": self.lst}
finder = Finder()
@app.get("/cameras")
def find_cameras():
return {"cameras": finder.start()}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app)
import requests
from bs4 import BeautifulSoup
def get_first_result_title(movie_title):
search_term = movie_title + "кинопоиск"
search_term = search_term.replace(" ", "+")
url = f"https://www.google.com/search?q={search_term}"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0"
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
first_result_title = soup.find("div", class_="r").a.text
return first_result_title
movie_title = input("Enter movie title: ")
title = get_first_result_title(movie_title)
print(f"Title of first result: {title}")
import json
import re
import requests
req = requests.get('https://steamcommunity.com/market/listings/730/Glove%20Case')
search = re.search('var line1=(.+);', req.text)
data_str = search.group(1)
data = json.loads(data_str)
result = '\n'.join(map(str, data))
f_res = '[Date, Price, Amount]\n' + result
print(f_res)