When I use tweepy, it returns 404

43 Views Asked by At

The first code:

import settings
import tweepy
import dataset
from textblob import TextBlob
from sqlalchemy.exc import ProgrammingError
import json

db = dataset.connect(settings.CONNECTION_STRING)

class StreamListener(tweepy.StreamListener):

    def on_status(self, status):
        if status.retweeted:
            return

        description = status.user.description
        loc = status.user.location
        text = status.text
        coords = status.coordinates
        geo = status.geo
        name = status.user.screen_name
        user_created = status.user.created_at
        followers = status.user.followers_count
        id_str = status.id_str
        created = status.created_at
        retweets = status.retweet_count
        bg_color = status.user.profile_background_color
        blob = TextBlob(text)
        sent = blob.sentiment

        if geo is not None:
            geo = json.dumps(geo)

        if coords is not None:
            coords = json.dumps(coords)

        table = db[settings.TABLE_NAME]
        try:
            table.insert(dict(
                user_description=description,
                user_location=loc,
                coordinates=coords,
                text=text,
                geo=geo,
                user_name=name,
                user_created=user_created,
                user_followers=followers,
                id_str=id_str,
                created=created,
                retweet_count=retweets,
                user_bg_color=bg_color,
                polarity=sent.polarity,
                subjectivity=sent.subjectivity,
            ))
        except ProgrammingError as err:
            print(err)

    def on_error(self, status_code):
        if status_code == 420:
            #returning False in on_data disconnects the stream
            return False

auth = tweepy.OAuthHandler(settings.TWITTER_APP_KEY, settings.TWITTER_APP_SECRET)
auth.set_access_token(settings.TWITTER_KEY, settings.TWITTER_SECRET)
api = tweepy.API(auth)

stream_listener = StreamListener()
stream = tweepy.Stream(auth=api.auth, listener=stream_listener)
stream.filter(track=settings.TRACK_TERMS)

The second code:

import os
import tweepy
import settings
import tweepy
import dataset
from textblob import TextBlob
from sqlalchemy.exc import ProgrammingError
import json

TWITTER_APP_KEY = '******'
TWITTER_APP_SECRET = '******'
#TWITTER_APP_KEY = ""
#TWITTER_APP_SECRET = ""
TWITTER_KEY = '******'
TWITTER_SECRET = '******'

class IDPrinter(tweepy.StreamListener):

    def on_status(self, status):
        print(status.id)
    def on_error(self, status_code):
        if status_code == 420:
            #returning False in on_data disconnects the stream
            return False

auth = tweepy.OAuthHandler(TWITTER_APP_KEY, TWITTER_APP_SECRET)
auth.set_access_token(TWITTER_KEY,TWITTER_SECRET)
api = tweepy.API(auth)
printer_listener = IDPrinter()
printer = tweepy.Stream(auth=api.auth, listener=printer_listener)
printer.sample()

In the two sections of code of mine, the first section is the code I need, and the second section is the test code written after the first section made an error.The error is that I cannot connect to the Twitter API.Taking the second section of code as an example:

auth = tweepy.OAuthHandler(TWITTER_APP_KEY, TWITTER_APP_SECRET)
auth.set_access_token(TWITTER_KEY,TWITTER_SECRET)
api = tweepy.API(auth)
printer_listener = IDPrinter()
printer = tweepy.Stream(auth=api.auth, listener=printer_listener)

can run smoothly. However,when I step into printer.sample(),there is something wrong and status_code becomes 404.I have changed my API and Access Toke, but there hasn't been any change. The version I use is the old one tweepy-3.7.0. It suits v1.1. The latest one tweepy-4.14.0 suits v2. Is this the reason that caused it? If so, could anyone help me change the two codes so that they can suit tweepy-4.14.0?

0

There are 0 best solutions below