0

I am working on a Twitter data mining application in Python, using the modules twitter and redis, based upon the book "Mining the Social Web" by Matthew A.Russell. Whenever I try to get the information of friends and followers using the redis module, it gives me the following error:

raise ConnectionError(self._error_message(e))
ConnectionError: Error 10061 connecting localhost:6379. No connection could be made because the target machine actively refused it.

When I don't use Redis, I can get the ids of all friends and followers. But I require Redis to perform certain analysis stuff on the data. My source code is as follows:

import json
import locale
import redis
from prettytable import PrettyTable

# Pretty printing numbers
from twitter__util import pp 

# These functions create consistent keys from 
# screen names and user id values
from twitter__util import getRedisIdByScreenName 
from twitter__util import getRedisIdByUserId

SCREEN_NAME = "timoreilly"

locale.setlocale(locale.LC_ALL, '')

def calculate():
    r = redis.Redis()  # Default connection settings on localhost

    follower_ids = list(r.smembers(getRedisIdByScreenName(SCREEN_NAME,
                        'follower_ids')))

    followers = r.mget([getRedisIdByUserId(follower_id, 'info.json')
                       for follower_id in follower_ids])
    followers = [json.loads(f) for f in followers if f is not None]

    freqs = {}
    for f in followers:
        cnt = f['followers_count']
        if not freqs.has_key(cnt):
            freqs[cnt] = []

        freqs[cnt].append({'screen_name': f['screen_name'], 'user_id': f['id']})

    # It could take a few minutes to calculate freqs, so store a snapshot for later use

    r.set(getRedisIdByScreenName(SCREEN_NAME, 'follower_freqs'),
          json.dumps(freqs))

    keys = freqs.keys()
    keys.sort()

    print 'The top 10 followers from the sample:'

    fields = ['Date', 'Count']
    pt = PrettyTable(fields=fields)
    [pt.set_field_align(f, 'l') for f in fields]

    for (user, freq) in reversed([(user['screen_name'], k) for k in keys[-10:]
                                    for user in freqs[k]]):
        pt.add_row([user, pp(freq)])

    pt.printt()

    all_freqs = [k for k in keys for user in freqs[k]]
    avg = reduce(lambda x, y: x + y, all_freqs) / len(all_freqs)

    print "\nThe average number of followers for %s's followers: %s" \
        % (SCREEN_NAME, pp(avg))

# psyco can only compile functions, so wrap code in a function

try:
    import psyco
    psyco.bind(calculate)
except ImportError, e:
    pass  # psyco not installed

calculate()

Any and all help would be really appreciated. Thanks!

Nathan822
  • 198
  • 4
  • 11

1 Answers1

1

That's the port that Redis runs on. If I were you, I'd double-check that the Redis server is running on the correct port and interface. To test that, ensure that you've a telnet client installed and do the following:

$ telnet localhost 6379

If that fails, then the Redis server isn't actually running where the Redis client library expects it to be by default, in which case, you'll have to provide the client with the correct details in the constructor arguments.

Keith Gaughan
  • 17,275
  • 3
  • 29
  • 26
  • What operating system are you using? If it's Ubuntu or some other Debian derivative, you'd run the following: `sudo apt-get install telnet` – Keith Gaughan Dec 03 '12 at 16:34
  • Windows 7. I have the telnet client installed and ran the command telnet localhost 6379. If failed. The output is: Connecting to localhost...Could not open communication to the host, on port 6379: Connect failed. – Nathan822 Dec 03 '12 at 16:39
  • 1
    Then the source of your problem is that you need to get a Redis service set up on your machine or, if you've already have one running, it's misconfigured. If it's running, you should see `redis.exe` or `redis-server.exe` listed in the Windows Task Manager. Windows isn't exactly the ideal to be running Redis on, but it is possible to get a Redis service up and running. Check out the answers to this question for more information on doing just that: http://stackoverflow.com/questions/6476945/how-do-i-run-redis-on-windows – Keith Gaughan Dec 03 '12 at 17:02