analitics

Pages

Monday, December 12, 2016

Use the tweepy to deal with twitter api - part 001.

I will show you how to install the python module named tweepy  and how to make authentication into twitter webpage.
This will install the tweepy python module.
C:\>cd Python27
C:\Python27>cd Scripts
C:\Python27\Scripts>pip install tweepy
Collecting tweepy
Downloading tweepy-3.5.0-py2.py3-none-any.whl
Collecting requests>=2.4.3 (from tweepy)
Downloading requests-2.12.3-py2.py3-none-any.whl (575kB)
100% |################################| 583kB 556kB/s
Collecting requests-oauthlib>=0.4.1 (from tweepy)
Downloading requests_oauthlib-0.7.0-py2.py3-none-any.whl
Requirement already satisfied: six>=1.7.3 in c:\python27\lib\site-packages (from
tweepy)
Collecting oauthlib>=0.6.2 (from requests-oauthlib>=0.4.1->tweepy)
Downloading oauthlib-2.0.1.tar.gz (122kB)
100% |################################| 133kB 506kB/s
Installing collected packages: requests, oauthlib, requests-oauthlib, tweepy
Running setup.py install for oauthlib ... done
Successfully installed oauthlib-2.0.1 requests-2.12.3 requests-oauthlib-0.7.0 tweepy-3.5.0

To deal with twitter api then you need to create a new application into this webpage.
This webpage will give for authentication your date to connect to twitter:
Application Settings
consumer_key=""
consumer_secret=""

Your Access Token
access_token=""
access_token_secret=""

Let's start with a simple example, by using your application settings and access token:
import tweepy
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

consumer_key=""
consumer_secret=""

access_token=""
access_token_secret=""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

print(api.me().name)

class StdOutListener(StreamListener):
""" A listener handles tweets that are received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
def on_data(self, data):
print(data)
return True

def on_error(self, status):
print(status)

if __name__ == '__main__':
lista = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

stream = Stream(auth, lista)
stream.filter(track=['internet'])

Using python shell to run this script will show all about 'internet'.
The output will come into raw format.
Let's try another example to update your status with this message:
I using OAuth authentication via Tweepy!
Just add this into my code before class definition:
api.update_status(status='I using OAuth authentication via Tweepy!')
You can rad more about streaming by reading this docs.
For example, I used track from here.



Sunday, December 11, 2016

The morse python module with pip installation versus precompiled archive wheel.

Today I try to deal with morse python module and it took me a while to install python modules because of their dependencies.
I started with the pip install of morse module but I got dependency errors in python modules
Because I try to fix this issue the result of this tutorial is more about how to install some python module: matplotlib, scipy, numpy, mkl and morse.
The all installation process will help you to understand how can be fix some pip installation versus precompiled archive wheel.
C:\Python27\Scripts>pip install matplotlib
Collecting matplotlib
  Downloading matplotlib-1.5.3-cp27-cp27m-win32.whl (6.0MB)
    100% |################################| 6.0MB 98kB/s
Requirement already satisfied: numpy>=1.6 in c:\python27\lib\site-packages (from
 matplotlib)
Collecting python-dateutil (from matplotlib)
  Downloading python_dateutil-2.6.0-py2.py3-none-any.whl (194kB)
    100% |################################| 194kB 1.4MB/s
Collecting cycler (from matplotlib)
  Downloading cycler-0.10.0-py2.py3-none-any.whl
Collecting pyparsing!=2.0.4,!=2.1.2,>=1.5.6 (from matplotlib)
  Downloading pyparsing-2.1.10-py2.py3-none-any.whl (56kB)
    100% |################################| 61kB 2.0MB/s
Collecting pytz (from matplotlib)
  Downloading pytz-2016.10-py2.py3-none-any.whl (483kB)
    100% |################################| 491kB 656kB/s
Collecting six>=1.5 (from python-dateutil->matplotlib)
  Downloading six-1.10.0-py2.py3-none-any.whl
Installing collected packages: six, python-dateutil, cycler, pyparsing, pytz, ma
tplotlib
Successfully installed cycler-0.10.0 matplotlib-1.5.3 pyparsing-2.1.10 python-da
teutil-2.6.0 pytz-2016.10 six-1.10.0

Download SciPy wheel file from here.
Install this file with:
C:\Python27\Scripts>pip install scipy-0.18.1-cp27-cp27m-win32.whl
Processing c:\python27\scripts\scipy-0.18.1-cp27-cp27m-win32.whl
Installing collected packages: scipy
Successfully installed scipy-0.18.1

Now you can install morse pyhon module.
pip install morse
If you installed the numpy by pip, but the scipy was installed by precompiled archive then expects numpy+mkl.
So to fix that issue, I download the numpy-1.12.0b1+mkl-cp27-cp27m-win32.whl file and install this with pip:
C:\Python27\Scripts>pip install "numpy-1.12.0b1+mkl-cp27-cp27m-win32.whl"
Processing c:\python27\scripts\numpy-1.12.0b1+mkl-cp27-cp27m-win32.whl
Installing collected packages: numpy
Found existing installation: numpy 1.11.2
Uninstalling numpy-1.11.2:
Successfully uninstalled numpy-1.11.2
Successfully installed numpy-1.12.0b1+mkl

Let's test the morse python module.
C:\Python27>python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (
Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import morse
>>> dir(morse)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'lookup', 'st
ring_to_morse']
>>> dir(morse.lookup)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__'
, '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__',
'__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '_
_new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__'
, '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get
', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'po
pitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
>>> print morse.lookup.keys()
['"', '$', '&', '(', ',', '.', '0', '2', '4', '6', '8', ':', '@', 'B', 'D', 'F',
'H', 'J', 'L', 'N', 'P', 'R', 'T', 'V', 'X', 'Z', '!', "'", ')', '+', '-', '/',
'1', '3', '5', '7', '9', ';', '=', '?', 'A', 'C', 'E', 'G', 'I', 'K', 'M', 'O',
'Q', 'S', 'U', 'W', 'Y', '_']
>>> print morse.lookup.items()
[('"', '.-..-.'), ('$', '...-..-'), ('&', '.-...'), ('(', '-.--.'), (',', '--..-
-'), ('.', '.-.-.-'), ('0', '-----'), ('2', '..---'), ('4', '....-'), ('6', '-..
..'), ('8', '---..'), (':', '---...'), ('@', '.--.-.'), ('B', '-...'), ('D', '-.
.'), ('F', '..-.'), ('H', '....'), ('J', '.---'), ('L', '.-..'), ('N', '-.'), ('
P', '.--.'), ('R', '.-.'), ('T', '-'), ('V', '...-'), ('X', '-..-'), ('Z', '--..
'), ('!', '-.-.--'), ("'", '.----.'), (')', '-.--.-'), ('+', '.-.-.'), ('-', '-.
...-'), ('/', '-..-.'), ('1', '.----'), ('3', '...--'), ('5', '.....'), ('7', '-
-...'), ('9', '----.'), (';', '-.-.-.'), ('=', '-...-'), ('?', '..--..'), ('A',
'.-'), ('C', '-.-.'), ('E', '.'), ('G', '--.'), ('I', '..'), ('K', '-.-'), ('M',
'--'), ('O', '---'), ('Q', '--.-'), ('S', '...'), ('U', '..-'), ('W', '.--'), (
'Y', '-.--'), ('_', '..--.-')]

You can see the python morse module is working well.