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

35 lines
1.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2018-03-11 21:30:46 +01:00
import html
2018-03-19 22:03:32 +01:00
from urllib.parse import urlparse
2018-03-11 21:30:46 +01:00
import requests
2018-03-19 22:03:32 +01:00
2018-03-11 21:30:46 +01:00
from models import *
2018-03-25 23:01:32 +02:00
mdls = [Answer, Question, Title, User, Alias, Site]
for i in mdls:
i.drop_table()
for i in reversed(mdls):
print(i)
i.create_table()
2018-03-11 21:30:46 +01:00
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)