scripts/scraper.py
def sort_articles(self):
        """Puts data from self.fetched_books in the correct order and format,
        and puts them into the self.content['books'] list.
        self.fetched_books is like:
            {
                'theguardian/mainsection': {
                    'meta': {...},
                    'articles': [...],
                },
                'theguardian/sport': {
                    ...
                },
                ...
            }

        So, we need to take the value dicts out of there and put them in
        self.content['books'] in the correct order.
        And sort the articles in each section by page number.
        """

        # The initial known books of each day's newspaper, in order:
        start_orders = {
            "weekday": ["theguardian/mainsection", "theguardian/g2"],
            "saturday": [
                "theguardian/mainsection",
                "theguardian/theguide",
                "theguardian/guardianreview",
                "theguardian/weekend",
                "theguardian/travel",
                "theguardian/cook",
                "theguardian/family",
            ],
            "sunday": [
                "theobserver/news",
                "theobserver/review",
                "theobserver/magazine",
            ],
        }

        # Get today's initial books, and also the sport section.
        if self.paper_name == "observer":
            start_order = start_orders["sunday"]
            sport_id = "theobserver/sport"

        elif self.issue_date.weekday() == 5:
            start_order = start_orders["saturday"]
            sport_id = "theguardian/sport"

        else:
            start_order = start_orders["weekday"]
            sport_id = "theguardian/sport"

        # We'll put the initial books in `start`.
        # And the sport book in `end`.
        # And any others - new books or special one-offs - in `middle`.
        start = []
        middle = []
        end = []

        for book in start_order:
            if book in self.fetched_books:
                start.append(self.fetched_books.pop(book))

        if sport_id in self.fetched_books:
            end.append(self.fetched_books.pop(sport_id))

        for book, bookdata in self.fetched_books.items():
            middle.append(bookdata)

        # Put all the books together in the correct order.
        # self.contents['books'] is now a list of dicts, one dict per book.
        # Each book dict has 'meta' and 'articles' keys.
        self.contents["books"] = start + middle + end

        # Sort the articles within each book:
        for book in self.contents["books"]:
            book["articles"] = sorted(
                book["articles"], key=lambda k: k["fields"]["newspaperPageNumber"]
            )
Puts data from self.fetched_books in the correct order and format,
        and puts them into the self.content['books'] list.
        self.fetched_books is like:
            {
                'theguardian/mainsection': {
                    'meta': {...},
                    'articles': [...],
                },
                'theguardian/sport': {
                    ...
                },
                ...
            }

        So, we need to take the value dicts out of there and put them in
        self.content['books'] in the correct order.
        And sort the articles in each section by page number.
        
Puts data from self.fetched_books in the correct order and format,
and puts them into the self.content['books'] list.

So, we need to take the value dicts out of there and put them in
self.content['books'] in the correct order.
And sort the articles in each section by page number.
Puts data from self . fetched_books in the correct order and format and puts them into the self . content [ ' books ' ] list . So we need to take the value dicts out of there and put them in self . content [ ' books ' ] in the correct order . And sort the articles in each section by page number .
def sort_articles(self):
        start_orders = {
            "weekday": ["theguardian/mainsection", "theguardian/g2"],
            "saturday": [
                "theguardian/mainsection",
                "theguardian/theguide",
                "theguardian/guardianreview",
                "theguardian/weekend",
                "theguardian/travel",
                "theguardian/cook",
                "theguardian/family",
            ],
            "sunday": [
                "theobserver/news",
                "theobserver/review",
                "theobserver/magazine",
            ],
        }
        if self.paper_name == "observer":
            start_order = start_orders["sunday"]
            sport_id = "theobserver/sport"
        elif self.issue_date.weekday() == 5:
            start_order = start_orders["saturday"]
            sport_id = "theguardian/sport"
        else:
            start_order = start_orders["weekday"]
            sport_id = "theguardian/sport"
        start = []
        middle = []
        end = []
        for book in start_order:
            if book in self.fetched_books:
                start.append(self.fetched_books.pop(book))
        if sport_id in self.fetched_books:
            end.append(self.fetched_books.pop(sport_id))
        for book, bookdata in self.fetched_books.items():
            middle.append(bookdata)
        self.contents["books"] = start + middle + end
        for book in self.contents["books"]:
            book["articles"] = sorted(
                book["articles"], key=lambda k: k["fields"]["newspaperPageNumber"]
            )
def sort_articles ( self ) : start_orders = { "weekday" : [ "theguardian/mainsection" , "theguardian/g2" ] , "saturday" : [ "theguardian/mainsection" , "theguardian/theguide" , "theguardian/guardianreview" , "theguardian/weekend" , "theguardian/travel" , "theguardian/cook" , "theguardian/family" , ] , "sunday" : [ "theobserver/news" , "theobserver/review" , "theobserver/magazine" , ] , } if self . paper_name == "observer" : start_order = start_orders [ "sunday" ] sport_id = "theobserver/sport" elif self . issue_date . weekday ( ) == 5 : start_order = start_orders [ "saturday" ] sport_id = "theguardian/sport" else : start_order = start_orders [ "weekday" ] sport_id = "theguardian/sport" start = [ ] middle = [ ] end = [ ] for book in start_order : if book in self . fetched_books : start . append ( self . fetched_books . pop ( book ) ) if sport_id in self . fetched_books : end . append ( self . fetched_books . pop ( sport_id ) ) for book , bookdata in self . fetched_books . items ( ) : middle . append ( bookdata ) self . contents [ "books" ] = start + middle + end for book in self . contents [ "books" ] : book [ "articles" ] = sorted ( book [ "articles" ] , key = lambda k : k [ "fields" ] [ "newspaperPageNumber" ] )
Puts data from self.fetched_books in the correct order and format,
and puts them into the self.content['books'] list.
Puts data from self . fetched_books in the correct order and format and puts them into the self . content [ ' books ' ] list .
"""Puts data from self.fetched_books in the correct order and format,
        and puts them into the self.content['books'] list.
        self.fetched_books is like:
            {
                'theguardian/mainsection': {
                    'meta': {...},
                    'articles': [...],
                },
                'theguardian/sport': {
                    ...
                },
                ...
            }

        So, we need to take the value dicts out of there and put them in
        self.content['books'] in the correct order.
        And sort the articles in each section by page number.
        """ # The initial known books of each day's newspaper, in order: # Get today's initial books, and also the sport section. # We'll put the initial books in `start`. # And the sport book in `end`. # And any others - new books or special one-offs - in `middle`. # Put all the books together in the correct order. # self.contents['books'] is now a list of dicts, one dict per book. # Each book dict has 'meta' and 'articles' keys. # Sort the articles within each book:
self