<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/rss-style.xsl" type="text/xsl"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	     xmlns:dc="http://purl.org/dc/elements/1.1/"
	   xmlns:atom="http://www.w3.org/2005/Atom"
	     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	  xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>Twitter API &#8211; pagination and IDs &#8211; Terence Eden’s Blog</title>
	<atom:link href="https://shkspr.mobi/blog/2010/06/twitter-api-pagination-and-ids/feed/" rel="self" type="application/rss+xml" />
	<link>https://shkspr.mobi/blog</link>
	<description>Regular nonsense about tech and its effects 🙃</description>
	<lastBuildDate>Wed, 04 Oct 2023 10:27:08 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://shkspr.mobi/blog/wp-content/uploads/2023/07/cropped-avatar-32x32.jpeg</url>
	<title>Twitter API &#8211; pagination and IDs &#8211; Terence Eden’s Blog</title>
	<link>https://shkspr.mobi/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title><![CDATA[Twitter API - pagination and IDs]]></title>
		<link>https://shkspr.mobi/blog/2010/06/twitter-api-pagination-and-ids/</link>
					<comments>https://shkspr.mobi/blog/2010/06/twitter-api-pagination-and-ids/#comments</comments>
				<dc:creator><![CDATA[@edent]]></dc:creator>
		<pubDate>Sun, 27 Jun 2010 07:54:36 +0000</pubDate>
				<category><![CDATA[/etc/]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[dabr]]></category>
		<category><![CDATA[twitter]]></category>
		<guid isPermaLink="false">http://shkspr.mobi/blog/?p=2121</guid>

					<description><![CDATA[Looking for some Twitter API help.  Bit of a geeky post, this...  Pagination is the act of splitting data into logical  pages. Suppose I had a list of item, numbered 0 - 99.  If I want 20 items per page, it&#039;s trivial to see that pagination looks like:  p1 = 0-19 p2 = 20-40 p3 = 41-61 p4 = 62-82 p5 = 83-99   If I wanted to start at, say, page 55 - pagination would look like:  p1 = 55-75 p2 = 76-96 …]]></description>
										<content:encoded><![CDATA[<p>Looking for some Twitter API help.&nbsp; Bit of a geeky post, this...</p>

<p>Pagination is the act of splitting data into logical&nbsp; pages. Suppose I had a list of item, numbered 0 - 99.&nbsp; If I want 20 items per page, it's trivial to see that pagination looks like:</p>

<pre>p1 = 0-19
p2 = 20-40
p3 = 41-61
p4 = 62-82
p5 = 83-99
</pre>

<p>If I wanted to start at, say, page 55 - pagination would look like:</p>

<pre>p1 = 55-75
p2 = 76-96
p3 = 97-99</pre>

<p>Easy, right?&nbsp; So why am I telling you this?</p>

<h2 id="twitter-timeline"><a href="https://shkspr.mobi/blog/2010/06/twitter-api-pagination-and-ids/#twitter-timeline">Twitter Timeline</a></h2>

<p>Imagine that those items are Twitter Status ID.&nbsp; Each one represents a tweet in your timeline.</p>

<p>Twitter will allow us to "page" back and forth through our timeline. If we say status ID 80 is the most recent post in our timeline, and we want to see 20 tweets at a time, pagination would look like this.</p>

<pre>p1 = 80-60
p2 = 60-40
... etc.</pre>

<p>Normally, that would be fine.</p>

<p>The only issue is that friends are posting <em>all the time</em>.&nbsp; Imagine we start with tweets 80-60.&nbsp; We go to page 2, but in the meantime, 5 new tweets have been made.</p>

<pre>p1 = 80-60
p2 = 65-45</pre>

<p>The user sees 5 tweets she has already read.&nbsp; Not desirable.</p>

<p>If 20 tweets had been made before clicking on the "next" button, this is what happens.</p>

<pre>p1 = 80-60
p2 = 80-60</pre>

<h2 id="max_id-to-the-rescue-aka-the-easy-bit"><a href="https://shkspr.mobi/blog/2010/06/twitter-api-pagination-and-ids/#max_id-to-the-rescue-aka-the-easy-bit">Max_ID To The Rescue (AKA, the easy bit)</a></h2>

<p>Luckily, Twitter allows us to use a max_id parameter in our API calls.&nbsp; This says "Get the tweets <strong>older</strong> than this number."</p>

<pre>http://api.twitter.com/1/statuses/home_timeline.json?max_id=123465789</pre>

<p>So, using max_id we can ensure that the user never has to read the same tweet twice.&nbsp; Instead of dumbly using pages, we call the specific tweets we want.</p>

<pre><span style="text-decoration: line-through;">p1</span> max_id=80 = 80-60</pre>

<pre><span style="text-decoration: line-through;">p2</span> max_id=60 = 60-40</pre>

<p>Easy!&nbsp; We just use the oldest tweet on the page as the max_id parameter when we call the next page.</p>

<h2 id="looking-to-the-future-aka-where-it-all-goes-horribly-wrong"><a href="https://shkspr.mobi/blog/2010/06/twitter-api-pagination-and-ids/#looking-to-the-future-aka-where-it-all-goes-horribly-wrong">Looking To The Future (AKA, where it all goes horribly wrong)</a></h2>

<p>So far, we've looked at stepping back in time.&nbsp; Seeing older tweets.&nbsp; Suppose we want to see newer tweets?</p>

<p>Twitter provides us with a since_id parameter for API calls.&nbsp; This says "Get the tweets <strong>newer</strong> than this number."</p>

<p>Unfortunately, it doesn't work.&nbsp; Well, it <em>works</em> but not the way I expected it to!</p>

<p>Suppose our user is deep down in her tweets, this is how I would expect since_id to work</p>

<pre>max_id=60&nbsp; = 60-40</pre>

<pre> (So, let's show any more recent tweets)</pre>

<pre>since_id=60 = 80-60</pre>

<p>We see the 20 tweets that occured <em>since</em> the since_id.&nbsp; Right?&nbsp; Wrong!&nbsp; This is what happens?</p>

<pre>max_id=60&nbsp; = 60-40</pre>

<pre>(So, let's show any more recent tweets)</pre>

<pre>since_id=60 = 100-80</pre>

<p><strong>What?</strong></p>

<h2 id="an-explanation"><a href="https://shkspr.mobi/blog/2010/06/twitter-api-pagination-and-ids/#an-explanation">An Explanation</a></h2>

<p>The since_id retrieves tweets <em>starting with the most recent</em>.&nbsp; It stops when it reaches the since_id.</p>

<p>I don't know the max_id that I'm looking for, so I can't call that.</p>

<p>I could call the most recent 200 tweets and look for the 20 I need.&nbsp; That's wasteful in terms of bandwidth and processing - there's also no guarantee that the since_id will be in there.</p>

<p>So, I have a problem.&nbsp; The "Older" link in my Twitter application will work.&nbsp; The "Newer" links won't.</p>

<p>Any suggestions?</p>
<img src="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/info/okgo.php?ID=2121&HTTP_REFERER=RSS" alt="" width="1" height="1" loading="eager">]]></content:encoded>
					
					<wfw:commentRss>https://shkspr.mobi/blog/2010/06/twitter-api-pagination-and-ids/feed/</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
			</item>
	</channel>
</rss>
