-
Notifications
You must be signed in to change notification settings - Fork 5
/
create.py
executable file
·34 lines (30 loc) · 1.16 KB
/
create.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env 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)