Skip to content

Commit

Permalink
Update to Twitter API v2 (#417)
Browse files Browse the repository at this point in the history
* update tweepy and its dependencies

* upgrade to api v2 endpoints

* update package-lock.json
  • Loading branch information
matthewyu01 authored Aug 23, 2023
1 parent de02a02 commit 1be9911
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 41 deletions.
9 changes: 6 additions & 3 deletions meow/scheduler/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ def get_analytics(smposts):
ACCESS_KEY = section.twitter_access_key
ACCESS_SECRET = section.twitter_access_secret

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
twitter_api = tweepy.Client(
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
access_token=ACCESS_KEY,
access_token_secret=ACCESS_SECRET
)

twitter_api = tweepy.API(auth)

tweet = twitter_api.get_status(tw_id)
tw_retweet = tweet.retweet_count
Expand Down
27 changes: 20 additions & 7 deletions meow/scheduler/management/commands/sendtweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,40 @@ def handle(self, *args, **options):
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)

api = tweepy.API(auth)

api = tweepy.Client(
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
access_token=ACCESS_KEY,
access_token_secret=ACCESS_SECRET
)

# Make the tweet follow DB social media standards
tweet = smpost.post_twitter

if url is not None:
tweet = tweet + " " + url

res = api.update_status(status=tweet)
res = api.create_tweet(text=tweet)

# Add the id for the post to the database
smpost.id_twitter = res.id
tweet_id = res.data['id']
smpost.id_twitter = tweet_id
smpost.save()

tweet_url = f"https://twitter.com/twitter/status/{tweet_id}"
logger.info('Tweet {} has sent successfully. URL: {}'.format(
smpost.slug,
"https://twitter.com/statuses/{}".format(res.id))
tweet_url,
)
)
return "https://twitter.com/statuses/{}".format(res.id)
return tweet_url

except tweepy.TweepError as e:
except tweepy.errors.TweepyException as e:
smpost.log(traceback.format_exc())
smpost.log_error(e, section, True)

logger.error("Send tweet errored\nslug: {} {}".format(smpost.slug, traceback.format_exc()))
except Exception as e:
smpost.log(traceback.format_exc())
smpost.log_error(e, section, True)

Expand Down
22 changes: 12 additions & 10 deletions meow/scheduler/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,16 +780,18 @@ def twitter_connect(request):
section.twitter_access_secret = twitter_auth.access_token_secret
section.twitter_account_handle = None
section.save()
try:
# If this fails it doesn't matter; it's just a screen name
api = tweepy.API(twitter_auth)
screen_name = api.me().screen_name
section.twitter_account_handle = screen_name
section.save()
except:
pass
request.session["message"] = "Twitter account, @" + \
screen_name + ", successfully added."

twitter_api = tweepy.Client(
consumer_key=TWITTER_CONSUMER_KEY,
consumer_secret=TWITTER_CONSUMER_SECRET,
access_token=twitter_auth.access_token,
access_token_secret=twitter_auth.access_token_secret
)
screen_name = twitter_api.get_me().data['name']
section.twitter_account_handle = screen_name
section.save()

request.session["message"] = f"Twitter account, @{screen_name}, successfully added."
request.session.save()
return redirect("/")

Expand Down
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ isort==4.2.15
kombu==4.2.1
lazy-object-proxy==1.3.1
mccabe==0.6.1
oauthlib==2.0.3
oauthlib==3.2.0
parsedatetime==2.4
psycopg2==2.7.5
Pillow==4.2.1
pylint==1.7.2
pyshorteners==0.6.1
pytz==2017.2
redis==2.10.6
requests==2.20.0
requests-oauthlib==0.8.0
requests>=2.9.1
requests-oauthlib==1.3.0
singledispatch==3.4.0.3
six==1.10.0
social-auth-app-django==3.1.0
social-auth-core==3.2.0
tweepy==3.8.0
tweepy==4.6.0
urllib3==1.24.2
vine==1.1.4
whitenoise==3.3.1
Expand Down

0 comments on commit 1be9911

Please sign in to comment.