import sys
import re
import requests


def article_list_assessment_check(article_title):
    content = get_content(article_title)
    listings = parse_article(content)
    mismatches = []
    print("Looking at " + article_title + ". Checking " + str(len(listings)) + " articles.")
    for l in listings:
        assessments = current_assessment(l["title"])
        if not l["assessment"] in assessments:
            mismatches.append({"title": l["title"], "listed_as": l["assessment"], "current": assessments})
            print("Found a mismatch! " + l["title"] + " listed as " + l["assessment"] + ", currently " + str(assessments))
    print(str(len(mismatches)) + " mismatches found.")
    return mismatches


def get_content(article_title):
    resp = requests.get("https://en.wikipedia.org/w/api.php?action=query&titles=" + article_title + "&prop=revisions&rvprop=content&format=json")
    pages = resp.json()["query"]["pages"]
    for p_key, p_val in pages.items():
        return p_val["revisions"][0]["*"]
    return None


def parse_article(content):
    article_listing_regex = re.compile(r'''
        [\*#]\s*                                        # line starts with a bullet or a number
        (?P<assessment>\{\{[Ii]con\|\w+\}\})            # assessment should always be first
        (?P<history>\s*\{\{[Ii]con\|\w+\}\})*           # option of multiple icons for FFA, or DGA
        \s*
        \'*\[\[(?P<title>[^#<>\[\]\{\}]+)\]\]                # actual title is a wikilink
    ''', re.VERBOSE)
    results = []
    for l in article_listing_regex.finditer(content):
        article = {}
        article["title"] = l.group("title").split("|")[0]
        article["assessment"] = l.group("assessment").split("|")[-1][:-2]
        article["history"] = None
        if l.group("history"):
            article["history"] = l.group("history").split("|")[-1][:-2]
        results.append(article)
    return results


def current_assessment(article_title, most_common=False):
    resp = requests.get("https://en.wikipedia.org/w/api.php?action=query&titles=" + article_title + "&prop=pageassessments&format=json")
    pages = resp.json()["query"]["pages"]
    for p_key, p_val in pages.items():
        assessments = [proj_val["class"] for proj_key, proj_val in p_val["pageassessments"].items()]
        if most_common:
            common_assessment = max(set(assessments), key=assessments.count)
            return common_assessment
        else:
            return assessments
    return None


def main():
    args = sys.argv[1:]
    if len(args):
        article_title = args[0]
    else:
        article_title = "Wikipedia:Vital articles/Level/2"
    article_list_assessment_check(article_title)


if __name__ == "__main__":
    main()
@@ -75,24 +75,38 @@
 rticle_title
+, section=None
 ):%0A    conte
@@ -135,16 +135,25 @@
 le_title
+, section
 )%0A    li
@@ -762,35 +762,39 @@
 itle
-):%0A    resp = requests.get(
+, section=None):%0A    baseurl = 
 %22htt
@@ -828,32 +828,74 @@
 .php
-?
+%22%0A    query_attrs = %7B%0A        %22
 action
-=
+%22: %22
 query
-&
+%22,%0A        %22
 titles
-=%22 +
+%22:
  art
@@ -904,26 +904,35 @@
 le_title
+,%0A      
  
-+
  %22
-&
 prop
-=
+%22: %22
 revision
@@ -936,36 +936,174 @@
 ions
-&
+%22,%0A        %22
 rvprop
-=
+%22: %22
 content
-&
+%22,%0A        %22
 format
-=
+%22: %22
 json%22
+%0A    %7D%0A    if section:%0A        query_attrs%5B%22rvsection%22%5D = section%0A    resp = requests.get(baseurl, query_attrs
 )%0A  
@@ -1668,21 +1668,16 @@
         
-     
 # actual
@@ -2172,54 +2172,25 @@
 itle
-, most_common=False):%0A    resp = requests.get(
+):%0A    baseurl = 
 %22htt
@@ -2224,32 +2224,74 @@
 .php
-?
+%22%0A    query_attrs = %7B%0A        %22
 action
-=
+%22: %22
 query
-&
+%22,%0A        %22
 titles
-=%22 +
+%22:
  art
@@ -2304,18 +2304,27 @@
 itle
+,%0A      
  
-+
  %22
-&
 prop
-=
+%22: %22
 page
@@ -2338,21 +2338,86 @@
 ents
-&
+%22,%0A        %22
 format
-=
+%22: %22
 json%22
+%0A    %7D%0A    resp = requests.get(baseurl, query_attrs
 )%0A  
@@ -2608,355 +2608,260 @@
     
-if most_common:%0A            common_assessment = max(set(assessments), key=assessments.count)%0A            return common_assessment%0A        else:%0A            return assessments%0A    return None%0A%0A%0Adef main():%0A    args = sys.argv%5B1:%5D%0A    if len(args):%0A        article_title = args%5B0%5D%0A    else:%0A        article_title = %22Wikipedia:Vital articles/Level/2%22
+return assessments%0A    return None%0A%0A%0Adef main():%0A    args = sys.argv%5B1:%5D%0A    article_title = %22Wikipedia:Vital articles/Level/1%22%0A    section = None%0A    if len(args):%0A        article_title = args%5B0%5D%0A        if len(args) %3E 1:%0A            section = args%5B1%5D
 %0A   
@@ -2904,16 +2904,25 @@
 le_title
+, section
 )%0A%0A%0Aif _

Refactor request querys, added abilty to get_content from a specific section