Error 'dict' object is not callable in requesting data from tweepy API

I was trying to retrieve tweets via tweepy API with the following code but the json dictionary that was retrieved had an error.

The Code:

import tweepy
from tweepy import OAuthHandler
from tweepy import Stream
import socket
import json
consumer_key="****"
consumer_secret="****"
access_token="****"
access_secret="****"


class TweetListener(Stream):
    def __init__(self, *args, csocket):    
        super().__init__(*args)
        self.client_socket = csocket
 
    def on_data(self, data):
        try:
            msg = json.loads(data)
            print(msg('text').encode('utf=8'))
            self.client_socket.send(msg('text').encode('utf=8'))
            return True
        except BaseException as e:
            print('Error %s'%str(e))
        return True
    def on_error(self, status):
        print(status)
        return True
def send_data(c_socket):
    twtr_stream = TweetListener(
        consumer_key, consumer_secret,
        access_token, access_secret,
        csocket=c_socket
    )
    twtr_stream.filter(track=['ETH'])
s = socket.socket()
host = "127.0.0.1"
port = 5000
s.bind((host,port))
print("Active port %s"%str(port))
s.listen(5)
c, addr = s.accept()
print("request from addr "+str(addr))
send_data(c)

send_data(c) caused The Error: Error 'dict' object is not callable which kept on repeating.

I have another file that is associated with it, both these codes are required to be run simultaneously.

Code:

from pyspark import SparkContext
from pyspark.streaming import StreamingContext
sc = SparkContext(appName='StreamingTwitterAnalysis')
sc.setLogLevel("ERROR")
ssc = StreamingContext(sc,10)
socket_stream = ssc.socketTextStream("127.0.0.1",5000)
lines = socket_stream.window(20)
hashtags = lines.flatMap(lambda text: text.split(" ")).filter(lambda word: word.lower().startwith("#")).map(lambda word: (word.lower(),1)).reduceByKey(lambda a,b:a+b)
dstream = hashtags.transform(lambda foo: foo.sortBy(lambda x:x[0].lower()).sortBy(lambda x:x[1].ascending==False))
dstream.pprint()
ssc.start()
ssc.awaitTermination()

Notebook Snippet: enter image description here


Solution 1:

In line no. 17 of the code you uploaded on pastebin, you load a JSON object msg, which is presumably a dict:

msg = json.loads(data)

In line no. 18 you then call that object with a string parameter:

print(msg('text').encode('utf=8'))

Since dicts are not callable, you get the mentioned error. So I presume you wanted to access a key by that name:

print(msg['text'].encode('utf=8'))