<?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>tweepy &#8211; Terence Eden’s Blog</title>
	<atom:link href="https://shkspr.mobi/blog/tag/tweepy/feed/" rel="self" type="application/rss+xml" />
	<link>https://shkspr.mobi/blog</link>
	<description>Regular nonsense about tech and its effects 🙃</description>
	<lastBuildDate>Sat, 24 Aug 2024 18:47:58 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://shkspr.mobi/blog/wp-content/uploads/2023/07/cropped-avatar-32x32.jpeg</url>
	<title>tweepy &#8211; Terence Eden’s Blog</title>
	<link>https://shkspr.mobi/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title><![CDATA[Easy Tutorial For Getting Twitter Friends Using Python & Tweepy]]></title>
		<link>https://shkspr.mobi/blog/2017/06/easy-tutorial-for-getting-twitter-friends-using-python-tweepy/</link>
					<comments>https://shkspr.mobi/blog/2017/06/easy-tutorial-for-getting-twitter-friends-using-python-tweepy/#comments</comments>
				<dc:creator><![CDATA[@edent]]></dc:creator>
		<pubDate>Fri, 09 Jun 2017 11:01:11 +0000</pubDate>
				<category><![CDATA[/etc/]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[tweepy]]></category>
		<category><![CDATA[twitter]]></category>
		<guid isPermaLink="false">https://shkspr.mobi/blog/?p=25344</guid>

					<description><![CDATA[Here&#039;s a very simple introduction to getting started with Tweepy - a Python program which lets you access Twitter. This will work on small computers like the Raspberry Pi.  Everything here takes place in the Terminal on the Command Line.  This should work on Windows and Mac - but I&#039;m using Linux.  Get Python  Open your terminal or command prompt. Type python  You should see something like: …]]></description>
										<content:encoded><![CDATA[<p>Here's a very simple introduction to getting started with Tweepy - a Python program which lets you access Twitter. This will work on small computers like the Raspberry Pi.</p>

<p>Everything here takes place in the Terminal on the Command Line.  This <em>should</em> work on Windows and Mac - but I'm using Linux.</p>

<h2 id="get-python"><a href="https://shkspr.mobi/blog/2017/06/easy-tutorial-for-getting-twitter-friends-using-python-tweepy/#get-python">Get Python</a></h2>

<p>Open your terminal or command prompt. Type <code>python</code></p>

<p>You should see something like:</p>

<pre><code>Python 2.7.9 (default, Dec 28 2016, 18:26:44)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt;
</code></pre>

<p>If you do, you have Python installed! Press <code>[ctrl]</code> and <code>d</code> to exit.</p>

<p>If you get an error, you will need to <a href="https://wiki.python.org/moin/BeginnersGuide/Download">install Python</a></p>

<h2 id="install-tweepy"><a href="https://shkspr.mobi/blog/2017/06/easy-tutorial-for-getting-twitter-friends-using-python-tweepy/#install-tweepy">Install Tweepy</a></h2>

<p><a href="https://github.com/tweepy/tweepy">Tweepy</a> is a library which makes it easy to use the Twitter API.</p>

<p>To install it, type:</p>

<p><code>pip install tweepy</code></p>

<h2 id="get-api-keys"><a href="https://shkspr.mobi/blog/2017/06/easy-tutorial-for-getting-twitter-friends-using-python-tweepy/#get-api-keys">Get API Keys</a></h2>

<p>You will need to visit <a href="https://apps.twitter.com/">https://apps.twitter.com/</a> and follow Twitter's instructions for creating an app.</p>

<p>You will end up with a "Consumer Key" and a "Consumer Secret"</p>

<img src="https://shkspr.mobi/blog/wp-content/uploads/2017/06/Twitter-Keys.png" alt="A screenshot of teh Twitter website showing application keys" width="665" height="276" class="aligncenter size-full wp-image-25346">

<p>These are the secret tokens which belong to your app.  What we need to do next is to sign in to the app and generate your <em>personal</em> tokens.</p>

<h2 id="get-tokens"><a href="https://shkspr.mobi/blog/2017/06/easy-tutorial-for-getting-twitter-friends-using-python-tweepy/#get-tokens">Get Tokens</a></h2>

<ul>
<li>You can save this piece of code as <code>oauth.py</code></li>
<li>You run it as <code>python oauth.py</code></li>
</ul>

<pre><code>import tweepy

consumer_key = "YOUR CONSUMER KEY HERE"
consumer_secret = "YOUR CONSUMER SECRET HERE"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.secure = True
auth_url = auth.get_authorization_url()
print 'Visit this URL and authorise the app to use your Twitter account: ' + auth_url
verifier = raw_input('Type in the generated PIN: ').strip()
auth.get_access_token(verifier)

# Print out the information for the user
print "consumer_key        = '%s'" % consumer_key
print "consumer_secret     = '%s'" % consumer_secret
print "access_token        = '%s'" % auth.access_token
print "access_token_secret = '%s'" % auth.access_token_secret
</code></pre>

<ul>
<li>It will print a URL, open that URL in your browser and it will take you to Twitter.</li>
<li>Log in to Twitter with your account.</li>
<li>Twitter will show you a PIN - 6 numbers long.</li>
<li>Type the PIN into the Command Line.</li>
<li>The program will display <em>all</em> your API Tokens.</li>
</ul>

<p>So, how do we use them?</p>

<h2 id="using-your-twitter-tokens"><a href="https://shkspr.mobi/blog/2017/06/easy-tutorial-for-getting-twitter-friends-using-python-tweepy/#using-your-twitter-tokens">Using your Twitter tokens</a></h2>

<ul>
<li>Add your tokens to this code.</li>
<li>You can save this piece of code as <code>tweet.py</code></li>
<li>You run it as <code>python tweet.py</code></li>
<li>It will Tweet a message on your behalf.</li>
</ul>

<pre><code>import tweepy

# Consumer keys and access tokens, used for OAuth
consumer_key        = 'YOUR CONSUMER KEY HERE'
consumer_secret     = 'YOUR CONSUMER SECRET HERE'
access_token        = 'YOUR ACCESS TOKEN HERE'
access_token_secret = 'YOUR ACCESS TOKEN SECRET HERE'

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

# Creation of the actual interface, using authentication
api = tweepy.API(auth)

# Send the tweet
message = 'This is a test'
api.update_status(message)
</code></pre>

<h2 id="get-a-list-of-all-the-people-you-follow"><a href="https://shkspr.mobi/blog/2017/06/easy-tutorial-for-getting-twitter-friends-using-python-tweepy/#get-a-list-of-all-the-people-you-follow">Get a list of all* the people you follow</a></h2>

<ul>
<li>Add your tokens to this code.</li>
<li>You can save this piece of code as <code>following.py</code></li>
<li>You run it as <code>python following.py</code></li>
<li>It will get the <em>ID numbers</em> of everyone* you follow and print them out.</li>
</ul>

<pre><code>import tweepy

# Consumer keys and access tokens, used for OAuth
consumer_key        = 'YOUR CONSUMER KEY HERE'
consumer_secret     = 'YOUR CONSUMER SECRET HERE'
access_token        = 'YOUR ACCESS TOKEN HERE'
access_token_secret = 'YOUR ACCESS TOKEN SECRET HERE'

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

# Creation of the actual interface, using authentication
api = tweepy.API(auth)

# Get all the people the user follows
friends = api.friends_ids()

# Print out each one
for id in friends:
    print(id)
</code></pre>

<p>* For technical reasons, this will only get the first 5,000 people you follow.</p>

<p>Let's save these to a file.  Note the differences in this code and the previous piece of code.</p>

<pre><code>import tweepy
import os

# Consumer keys and access tokens, used for OAuth
consumer_key        = 'YOUR CONSUMER KEY HERE'
consumer_secret     = 'YOUR CONSUMER SECRET HERE'
access_token        = 'YOUR ACCESS TOKEN HERE'
access_token_secret = 'YOUR ACCESS TOKEN SECRET HERE'

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

# Creation of the actual interface, using authentication
api = tweepy.API(auth)
friends = sorted(api.friends_ids())

friendsFile = open("friends.txt","w")

for id in friends:
    friendsFile.write(str(id) + "\n")

friendsFile.close()
</code></pre>

<p>You will now have a file called "friends.txt" which contains the ID Numbers of everyone you follow.</p>

<p>Handy for backing up your account :-)</p>

<blockquote class="social-embed" id="social-embed-872863823996350464" lang="en" itemscope="" itemtype="https://schema.org/SocialMediaPosting"><blockquote class="social-embed" id="social-embed-872862984950996992" lang="en" itemscope="" itemtype="https://schema.org/SocialMediaPosting"><header class="social-embed-header" itemprop="author" itemscope="" itemtype="https://schema.org/Person"><a href="https://twitter.com/edent" class="social-embed-user" itemprop="url"><img class="social-embed-avatar social-embed-avatar-circle" src="data:image/webp;base64,UklGRkgBAABXRUJQVlA4IDwBAACQCACdASowADAAPrVQn0ynJCKiJyto4BaJaQAIIsx4Au9dhDqVA1i1RoRTO7nbdyy03nM5FhvV62goUj37tuxqpfpPeTBZvrJ78w0qAAD+/hVyFHvYXIrMCjny0z7wqsB9/QE08xls/AQdXJFX0adG9lISsm6kV96J5FINBFXzHwfzMCr4N6r3z5/Aa/wfEoVGX3H976she3jyS8RqJv7Jw7bOxoTSPlu4gNbfXYZ9TnbdQ0MNnMObyaRQLIu556jIj03zfJrVgqRM8GPwRoWb1M9AfzFe6Mtg13uEIqrTHmiuBpH+bTVB5EEQ3uby0C//XOAPJOFv4QV8RZDPQd517Khyba8Jlr97j2kIBJD9K3mbOHSHiQDasj6Y3forATbIg4QZHxWnCeqqMkVYfUAivuL0L/68mMnagAAA" alt="" itemprop="image"><div class="social-embed-user-names"><p class="social-embed-user-names-name" itemprop="name">Terence Eden is on Mastodon</p>@edent</div></a><img class="social-embed-logo" alt="Twitter" src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%0Aaria-label%3D%22Twitter%22%20role%3D%22img%22%0AviewBox%3D%220%200%20512%20512%22%3E%3Cpath%0Ad%3D%22m0%200H512V512H0%22%0Afill%3D%22%23fff%22%2F%3E%3Cpath%20fill%3D%22%231d9bf0%22%20d%3D%22m458%20140q-23%2010-45%2012%2025-15%2034-43-24%2014-50%2019a79%2079%200%2000-135%2072q-101-7-163-83a80%2080%200%200024%20106q-17%200-36-10s-3%2062%2064%2079q-19%205-36%201s15%2053%2074%2055q-50%2040-117%2033a224%20224%200%2000346-200q23-16%2040-41%22%2F%3E%3C%2Fsvg%3E"></header><section class="social-embed-text" itemprop="articleBody"><small class="social-embed-reply"><a href="https://twitter.com/Suw/status/872861640076734465">Replying to @Suw</a></small><a href="https://twitter.com/Suw">@Suw</a> <a href="https://twitter.com/paul_clarke">@paul_clarke</a> Hmmm. I could make a "following backer up thingamajig". Might be of some use. But would anyone use it *before* it happened?</section><hr class="social-embed-hr"><footer class="social-embed-footer"><a href="https://twitter.com/edent/status/872862984950996992"><span aria-label="2 likes" class="social-embed-meta">❤️ 2</span><span aria-label="0 replies" class="social-embed-meta">💬 0</span><span aria-label="0 reposts" class="social-embed-meta">🔁 0</span><time datetime="2017-06-08T17:08:47.000Z" itemprop="datePublished">17:08 - Thu 08 June 2017</time></a></footer></blockquote><header class="social-embed-header" itemprop="author" itemscope="" itemtype="https://schema.org/Person"><a href="https://twitter.com/paul_clarke" class="social-embed-user" itemprop="url"><img class="social-embed-avatar social-embed-avatar-circle" src="data:image/webp;base64,UklGRvQCAABXRUJQVlA4IOgCAACQDACdASowADAAPqlCnEmmI6KhMdmd+MAVCWIAnTNQyxDBO2DJPpEKMZ1Mmpxs5NV891blUBmDkkEdQ5pL/oOOFpfjZrbYRylF6SO1Nv/SW5jtRQjeE6efGfTBPgeHL8Z+BeALfcFRvd3hg/TgAPzzLCzHO8qqwgvjX+n9EGApSYlGxdmBraM0FmvyLTsqyNeNjsXiGGfpM03SW69GqibiW0wLmec1On56m0mXN7u2BEkXHnmq2Q3O8kkR/k8W0eRZoOjorBJRUkTO50yV/lptq/QXz4Dn2tWIrpvPbGTuC6Zo90ZwSSVftIom3He0mBqV11dMsmWFSMxZJId10QYU1A7ZJSu4uFFGLidn0MN6o3GokIcM8W73ZabURwBy/mnvPYKdO5V8z31CHJhv11/phmcmAuclFN5Mz0SgggAqDptSdmQpBhSW/k7ZS8s81MHoc01BfR8nYm6q+V+tOUisVXyN5A9oZvZnj7ZVx5mrZkS6kHvP53tumb6DUOXv1xbzCO9cK5GSgozM92nfm78EKNCVgTHuJdohhR4fQSs2sVoU5hFiQtgxUA7zG3Q+xV8CM87hHdgO07wKJhCQ7KFdP6C/M3j6UZtSfL0YxRETjW7bffYjOs4IgKGS/rdVpG2djNpfwK5n2lZLq/QmL5UG78hV7bpojg95lk6oxUBqMblnNrZTR9+z0pZr7Tg5O8vAo5maiAX8jwM2Jhxt7P0xUHjM4L5bTddIpol01DvwU5omc6MoYjkhnGsXcXJoYd1tftNnzhdZI4lMd8B15X87W6DwBp/i/84bcd86ZFfxX4RL0VGF+aF3+tTvvZy0MvRuzcfoBm6Z0DBN8Iq31xZa2NjX/C8Q3sQnHXd4d2kQc/yfyfBWx7oMfeg7n9/jmRv5fXA+BQtMGu6HG6tPYzvV73/3x04D2s01yDIt0wqmOvF4P/lUvtq7/bb72kVajNRXspjGxsjx5JXkMNvdG2NAeir+zGoGAAA=" alt="" itemprop="image"><div class="social-embed-user-names"><p class="social-embed-user-names-name" itemprop="name">Paul Clarke</p>@paul_clarke</div></a><img class="social-embed-logo" alt="Twitter" src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%0Aaria-label%3D%22Twitter%22%20role%3D%22img%22%0AviewBox%3D%220%200%20512%20512%22%3E%3Cpath%0Ad%3D%22m0%200H512V512H0%22%0Afill%3D%22%23fff%22%2F%3E%3Cpath%20fill%3D%22%231d9bf0%22%20d%3D%22m458%20140q-23%2010-45%2012%2025-15%2034-43-24%2014-50%2019a79%2079%200%2000-135%2072q-101-7-163-83a80%2080%200%200024%20106q-17%200-36-10s-3%2062%2064%2079q-19%205-36%201s15%2053%2074%2055q-50%2040-117%2033a224%20224%200%2000346-200q23-16%2040-41%22%2F%3E%3C%2Fsvg%3E"></header><section class="social-embed-text" itemprop="articleBody"><small class="social-embed-reply"><a href="https://twitter.com/edent/status/872862984950996992">Replying to @edent</a></small><a href="https://twitter.com/edent">@edent</a> <a href="https://twitter.com/Suw">@Suw</a> I would. But I am on the edge.</section><hr class="social-embed-hr"><footer class="social-embed-footer"><a href="https://twitter.com/paul_clarke/status/872863823996350464"><span aria-label="0 likes" class="social-embed-meta">❤️ 0</span><span aria-label="1 replies" class="social-embed-meta">💬 1</span><span aria-label="0 reposts" class="social-embed-meta">🔁 0</span><time datetime="2017-06-08T17:12:07.000Z" itemprop="datePublished">17:12 - Thu 08 June 2017</time></a></footer></blockquote>
<img src="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/info/okgo.php?ID=25344&HTTP_REFERER=RSS" alt="" width="1" height="1" loading="eager">]]></content:encoded>
					
					<wfw:commentRss>https://shkspr.mobi/blog/2017/06/easy-tutorial-for-getting-twitter-friends-using-python-tweepy/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
