adibaewa / pylast

Automatically exported from code.google.com/p/pylast
Apache License 2.0
0 stars 0 forks source link

_collect_nodes gives up too easily #67

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Please type example code that produces the issue:
    me = pylast.User(username, network)
    mytracks = me.get_recent_tracks(limit=None)

What is the expected output? What do you see instead?
Expected: all of my scrobbles. 
Got: Malformed response error. Something about an EOF where it didn't belong.

What versions of Pylast and Python are you using?
latest, 2.6

Please provide any additional information below.
I was able to fix this by rewriting _collect_nodes as follows:

def _collect_nodes(limit, sender, method_name, cacheable, params=None):
    """
        Returns a sequqnce of dom.Node objects about as close to
        limit as possible
    """

    if not params:
        params = sender._get_params()

    nodes = []
    page = 1
    end_of_pages = False

    while not end_of_pages and (not limit or (limit and len(nodes) < limit)):
        params["page"] = str(page)
        max_fails = 10
        fails = 0
        try:
            doc = sender._request(method_name, cacheable, params)

            main = doc.documentElement.childNodes[1]

            if main.hasAttribute("totalPages"):
                total_pages = _number(main.getAttribute("totalPages"))
            elif main.hasAttribute("totalpages"):
                total_pages = _number(main.getAttribute("totalpages"))
            else:
                raise Exception("No total pages attribute")

            for node in main.childNodes:
                if not node.nodeType == xml.dom.Node.TEXT_NODE and (not limit or (limit and len(nodes) < limit)):
                    nodes.append(node)

                if page >= total_pages:
                    end_of_pages = True

            page += 1
        except Exception as e:            
            print "effin' weird ", str(page)
            fails += 1
            if(fails >= max_fails):
                page += 1

    return nodes

This retrieved my 32000 scrobbles, requiring (for some reason) 2000+ pages to 
do so. In those pages, it had to "retry" a page three different times, but 
never had to retry the same page more than once. 

Original issue reported on code.google.com by johnson....@gmail.com on 10 Apr 2011 at 7:25