question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bulk Statuses Route /statuses/lookup.json

See original GitHub issue

Hello @bear,

While using this library today (thanks for the convenience!), I found that a particular route was missing, so I subclassed twitter.Api and added a method for my use-case (I was able to use the dicts decoded from json). However, when integrating my changes with the existing object structure, it seems that there’s a mismatch between the statuses at this route, and the status object returned at /statuses/show.json. I’ve included my basic patch, and I’ll try to integrate it more fully once I get back to work tomorrow. The issue comes up at line 546 of twitter/status.py. As I said, I’ll dig into this more deeply tomorrow, clean up the code, and include tests.

AttributeError

    546                 media = [m for m in data['extended_entities']['media']]
    547
--> 548         return Status(created_at=data.get('created_at', None),
    549                       favorited=data.get('favorited', None),
    550                       favorite_count=data.get('favorite_count', None),

The status objects returned by the statuses/lookup.json route do not contain “extended_entities”.

Here is the patch (work in progress).

From 3fdfcc2ad35af327a65ec2a448a86a32e3ce5b65 Mon Sep 17 00:00:00 2001
From: Arthur Maciejewicz <arthur@signafire.com>
Date: Tue, 29 Sep 2015 19:57:54 -0400
Subject: [PATCH] Added bulk statuses

---
 twitter/api.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/twitter/api.py b/twitter/api.py
index d38e1aa..5e4128e 100644
--- a/twitter/api.py
+++ b/twitter/api.py
@@ -639,6 +639,69 @@ class Api(object):

         return [Status.NewFromJsonDict(x) for x in data]

+    def StatusesLookup(self,
+                       ids,
+                       trim_user=False,
+                       include_entities=True,
+                       map=True):
+        """Returns a list of status messages, specified by comma delimited ids
+
+        The twitter.Api instance must be authenticated.
+        You are limited to 100 ids at a time.
+
+        Args:
+          ids:
+            An iterable of numeric ID(s) of the status(es) you are trying to retrieve.
+          trim_user:
+            When set to True, each tweet returned in a timeline will include
+            a user object including only the status authors numerical ID.
+            Omit this parameter to receive the complete user object. [Optional]
+          include_entities:
+            If False, the entities node will be disincluded.
+            This node offers a variety of metadata about the tweet in a
+            discreet structure, including: user_mentions, urls, and
+            hashtags. [Optional]
+          map:
+            tweets that do not exist or cannot be viewed by the current
+            user will have their key represented but with an explicitly
+            null value paired with it. [Optional]
+
+
+        Returns:
+          A list of twitter.Status instances representing the status messages
+        """
+
+        url = '%s/statuses/lookup.json' % (self.base_url)
+
+        for id in ids:
+            try:
+                foo = long(id)
+            except ValueError:
+                raise TwitterError({'message': "'ids' must all be integers."})
+
+        parameters = {}
+
+        parameters['id'] = ",".join(str(i) for i in ids)
+
+        if trim_user:
+            parameters['trim_user'] = 1
+
+        if not include_entities:
+            parameters['include_entities'] = 'false'
+
+        if map:
+            parameters['map'] = 'true'
+
+
+        json_data = self._RequestUrl(url, 'GET', data=parameters)
+        data = self._ParseAndCheckTwitter(json_data.content)
+        print data
+
+        statuses = [Status.NewFromJsonDict(status) for status in data]
+
+        return statuses
+
+
     def GetStatus(self,
                   id,
                   trim_user=False,
-- 
2.4.5

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
komePcommented, Mar 26, 2017

I had a need for this functionality, and implemented the following based on the patch from @Zintinio and the current python-twitter code. I did not observe any differences between the data returned with this and the data returned by GetStatus.

However, I did encounter a bug in python-twitter’s POST support: the setting of tweet_mode is not honoured. Adding data['tweet_mode'] = self.tweet_mode before the relevant requests.post call in Api._RequestUrlfixed this for me. Perhaps someone more knowledgeable than me could advise if this needs to be done for all POST requests.

def StatusesLookup(self,
                   status_ids,
                   trim_user=False,
                   include_entities=True,
                   map_all=True):
    """Fetch information for the specified status IDs.

    The twitter.Api instance must be authenticated.  You are limited to
    100 ids at a time.

    Args:
      status_ids:
        An iterable of numeric ID(s) of the status(es) you are trying to
        retrieve.
      trim_user:
        When set to True, each tweet returned in a timeline will include
        a user object including only the status authors numerical ID.
        Omit this parameter to receive the complete user object. [Optional]
      include_entities:
        If False, the entities node will be disincluded.
        This node offers a variety of metadata about the tweet in a
        discreet structure, including: user_mentions, urls, and
        hashtags. [Optional]
      map_all:
        Tweets that do not exist or cannot be viewed by the current user
        will have their key represented but with an explicitly null value
        paired with it. [Optional]

    Returns:
      A list of twitter.Status instances representing the status messages
    """

    url = '%s/statuses/lookup.json' % (self.base_url)

    idstring = ','.join(str(enf_type('status_id', int, status_id))
                        for status_id in status_ids)
    parameters = {
        'id': idstring,
        'trim_user': enf_type('trim_user', bool, trim_user),
        'include_entities': enf_type('include_entities', bool,
                                     include_entities),
        'map': enf_type('map_all', bool, map_all)
    }

    resp = self._RequestUrl(url, 'POST', data=parameters)
    data = self._ParseAndCheckTwitter(resp.content.decode('utf-8'))

    return [(twitter.Status.NewFromJsonDict(data['id'][status_id])
             if data['id'][status_id]
             else None)
            for status_id in (str(s) for s in status_ids)]

If you would like this in a pull request, I can do so. I was lazy so I implemented this in my own code and assigned it to twitter.Api.GetStatuses.

0reactions
jeremylowcommented, Mar 26, 2017

That’s a good call. We should add the tweet_mode param to all the _RequestUrl* methods.

Read more comments on GitHub >

github_iconTop Results From Across the Web

GET statuses/lookup | Docs | Twitter Developer Platform
GET statuses/lookup. Returns fully-hydrated Tweet objects for up to 100 Tweets per request, as specified by comma-separated values passed to the id ...
Read more >
Bulk API 2.0 and Bulk API Developer Guide
Bulk API 2.0 allows for: • Less client-side code writing. • Easy-to-monitor job status. • Automatic retry of failed records.
Read more >
Neptune Loader Get-Status Responses - AWS Documentation
When an error occurs, a JSON errors object is returned in the BODY of the response, with the following fields: startIndex – The...
Read more >
Lookups - Apache Druid
In Druid SQL, lookups can be queried using the LOOKUP function, ... Lookups can be updated in bulk by posting a JSON object...
Read more >
JSON Schema | The home of JSON Schema
We monitor the jsonschema tag on StackOverflow. Project Status #. 2022-06-10: A patch release of Draft 2020-12 has been published with no functional...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found