-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfix_alaska_site_migration.py
56 lines (48 loc) · 1.89 KB
/
fix_alaska_site_migration.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from utils import init_django
from urllib.parse import urlparse, parse_qs
def convert_url(url):
parsed = urlparse(url)
if parsed.netloc == "www.legis.state.ak.us":
args = parse_qs(parsed.query)
session = args["session"][0]
if parsed.path == "/basis/get_bill.asp":
bill_id = args["bill"][0]
return f'https://www.akleg.gov/basis/Bill/Detail/{session}?Root={bill_id}'
elif parsed.path == "/basis/get_bill_text.asp":
hsid = args["hsid"][0]
return f"https://www.akleg.gov/basis/Bill/Text/{session}?hsid={hsid}"
elif parsed.path == "/basis/get_documents.asp":
if 'docid' in args:
docid = args["docid"][0]
return f'http://www.akleg.gov/basis/get_documents.asp?session={session}&docid={docid}'
else:
bill_id = args["bill"][0]
return f'https://www.akleg.gov/basis/Bill/Detail/{session}?Root={bill_id}#tab5_4'
elif parsed.path == "/basis/get_fulltext.asp":
bill_id = args["bill"][0]
return f'https://www.akleg.gov/basis/Bill/Detail/{session}?Root={bill_id}#tab1_4'
else:
raise Exception(parsed.path)
def update_url(obj):
new = convert_url(obj.url)
if new:
print(f"{obj.url} => {new}")
obj.url = new
obj.save()
else:
raise Exception(obj.url)
def main():
init_django()
from opencivicdata.legislative.models import Bill
bills = Bill.objects.filter(from_organization__jurisdiction__name="Alaska")
print(f"{len(bills)} alaska bills")
for b in bills:
for s in b.sources.all():
update_url(s)
for doc in b.documents.all():
for link in doc.links.all():
update_url(link)
for doc in b.versions.all():
for link in doc.links.all():
update_url(link)
main()