1
0
Fork 0
mirror of https://github.com/Findus23/se-simulator.git synced 2024-09-19 15:53:45 +02:00
se-simulator/create.py
Lukas Winkler 18fc22de4d answers!
2018-03-25 23:01:32 +02:00

33 lines
1.1 KiB
Python

import html
from urllib.parse import urlparse
import requests
from models import *
mdls = [Answer, Question, Title, User, Alias, Site]
for i in mdls:
i.drop_table()
for i in reversed(mdls):
print(i)
i.create_table()
r = requests.get("https://api.stackexchange.com/2.2/sites?pagesize=500")
for site in r.json()["items"]:
if site["site_type"] == "meta_site":
continue
element, created = Site.get_or_create(shortname=site["api_site_parameter"])
print(created)
element.name = html.unescape(site["name"])
element.shortname = site["api_site_parameter"]
element.url = urlparse(site["site_url"]).hostname
element.icon_url = site["high_resolution_icon_url"] if "high_resolution_icon_url" in site else site["icon_url"]
styling = site["styling"]
element.tag_background_color = styling["tag_background_color"]
element.tag_foreground_color = styling["tag_foreground_color"]
element.link_color = styling["link_color"]
element.save()
if "aliases" in site:
for al in site["aliases"]:
domain = urlparse(al).hostname
alias, created = Alias.get_or_create(url=domain, site=element)