#!/usr/bin/env python
import urllib
import json


api_url = "http://en.wikipedia.org/w/api.php"

        
def create_book(data):
    for l in data.split('\n'):
        if l.startswith('*[['):
            l=l.replace('[[','')
            l=l.replace(']]','')
            l = l.strip()[1:]
            link, name  = l.split('|') 
            url = api_url +  '?action=query&format=json&prop=revisions&rvprop=content&titles=' + link # FIXME
            res = json.loads(urllib.urlopen(url).read())
            content = res['query']['pages'].values()[0]['revisions'][0]['*']
            for l in content.split('\n'):
                if l.startswith('==='):
                    print ';' + l.replace('===','').encode('utf8')
                elif l.startswith(':'):
                    print l.encode('utf8')
   
if __name__ == '__main__':
    """
    List of Sigppost Books (not articles!) can be found here:
    http://en.wikipedia.org/wiki/Book:Wikipedia_Signpost 
    
    Action: 
    Copy and paste a year (source) below.
    Software: 
      * parses below
      * replaces the listed books with the transcluded articles (adding chapters)
      * outputs the stuff 
    """
      
    create_book("""
;January
*[[Book:Wikipedia Signpost/2009-01-03|3 January 2009]]
*[[Book:Wikipedia Signpost/2009-01-10|10 January 2009]]
*[[Book:Wikipedia Signpost/2009-01-17|17 January 2009]]
*[[Book:Wikipedia Signpost/2009-01-24|24 January 2009]]
*[[Book:Wikipedia Signpost/2009-01-31|31 January 2009]]

;February
*[[Book:Wikipedia Signpost/2009-02-08|8 February 2009]]
*[[Book:Wikipedia Signpost/2009-02-16|16 February 2009]]
*[[Book:Wikipedia Signpost/2009-02-23|23 February 2009]]

;March
*[[Book:Wikipedia Signpost/2009-03-02|2 March 2009]]
*[[Book:Wikipedia Signpost/2009-03-09|9 March 2009]]
*[[Book:Wikipedia Signpost/2009-03-16|16 March 2009]]
*[[Book:Wikipedia Signpost/2009-03-23|23 March 2009]]
*[[Book:Wikipedia Signpost/2009-03-30|30 March 2009]]

;April
*[[Book:Wikipedia Signpost/2009-04-06|6 April 2009]]
*[[Book:Wikipedia Signpost/2009-04-13|13 April 2009]]
*[[Book:Wikipedia Signpost/2009-04-20|20 April 2009]]
*[[Book:Wikipedia Signpost/2009-04-27|27 April 2009]]

;May
*[[Book:Wikipedia Signpost/2009-05-04|4 May 2009]]
*[[Book:Wikipedia Signpost/2009-05-11|11 May 2009]]
*[[Book:Wikipedia Signpost/2009-05-18|18 May 2009]]
*[[Book:Wikipedia Signpost/2009-05-25|25 May 2009]]

;June
*[[Book:Wikipedia Signpost/2009-06-01|1 June 2009]]
*[[Book:Wikipedia Signpost/2009-06-08|8 June 2009]]
*[[Book:Wikipedia Signpost/2009-06-15|15 June 2009]]
*[[Book:Wikipedia Signpost/2009-06-22|22 June 2009]]
*[[Book:Wikipedia Signpost/2009-06-29|29 June 2009]]

;July
*[[Book:Wikipedia Signpost/2009-07-06|6 July 2009]]
*[[Book:Wikipedia Signpost/2009-07-13|13 July 2009]]
*[[Book:Wikipedia Signpost/2009-07-20|20 July 2009]]
*[[Book:Wikipedia Signpost/2009-07-27|27 July 2009]]

;August
*[[Book:Wikipedia Signpost/2009-08-03|3 August 2009]]
*[[Book:Wikipedia Signpost/2009-08-10|10 August 2009]]
*[[Book:Wikipedia Signpost/2009-08-17|17 August 2009]]
*[[Book:Wikipedia Signpost/2009-08-24|24 August 2009]]
*[[Book:Wikipedia Signpost/2009-08-31|31 August 2009]]

;September
*[[Book:Wikipedia Signpost/2009-09-07|7 September 2009]]
*[[Book:Wikipedia Signpost/2009-09-14|14 September 2009]]
*[[Book:Wikipedia Signpost/2009-09-21|21 September 2009]]
*[[Book:Wikipedia Signpost/2009-09-28|28 September 2009]]

;October
*[[Book:Wikipedia Signpost/2009-10-05|5 October 2009]]
*[[Book:Wikipedia Signpost/2009-10-12|12 October 2009]]
*[[Book:Wikipedia Signpost/2009-10-19|19 October 2009]]
*[[Book:Wikipedia Signpost/2009-10-26|26 October 2009]]

;November
*[[Book:Wikipedia Signpost/2009-11-02|2 November 2009]]
*[[Book:Wikipedia Signpost/2009-11-09|9 November 2009]]
*[[Book:Wikipedia Signpost/2009-11-16|16 November 2009]]
*[[Book:Wikipedia Signpost/2009-11-23|23 November 2009]]
*[[Book:Wikipedia Signpost/2009-11-30|30 November 2009]]

;December
*[[Book:Wikipedia Signpost/2009-12-07|7 December 2009]]
*[[Book:Wikipedia Signpost/2009-12-14|14 December 2009]]
*[[Book:Wikipedia Signpost/2009-12-21|21 December 2009]]
*[[Book:Wikipedia Signpost/2009-12-28|28 December 2009]]
""")

Start a discussion about improving the User:He!ko/Signpost page

Start a discussion