0

I am quite the novice when it comes to coding. How do I modify this sample code to download tweets using Python?

def get_tweets(api, input_query):
    for tweet in tweepy.Cursor(api.search, q=input_query, lang="en").items():
    yield tweet

if __name__ == "__version__":
    input_query = sys.argv[1]

    access_token = "REPLACE_YOUR_KEY_HERE"
    access_token_secret = "REPLACE_YOUR_KEY_HERE"
    consumer_key = "REPLACE_YOUR_KEY_HERE"
    consumer_secret = "REPLACE_YOUR_KEY_HERE"
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    tweets = get_tweets(api, input_query)
    for tweet in tweets:
        print(tweet.text)

I entered my keys.

I should be able to download tweets using a command like this print_tweets.py "Enter subject here" Where do I enter this command? In Python? In the command prompt?

I am getting this error:

NameError Traceback (most recent call last) in () ----> 1 print_tweets.py

NameError: name 'print_tweets' is not defined

Please help!

Neil
  • 14,063
  • 3
  • 30
  • 51
Aoitori
  • 67
  • 2
  • 8

1 Answers1

0

The NameError is saying the python script isn't receiving command line arguments, sys.argv[1] being the "subject". Replace "Enter subject here" with the subject you wish to search.

In this example, springbreak is sys.argv[1]:

C:\> python print_tweets.py springbreak

should return and print out tweet texts containing your "subject".

You may also need to change:

if __name__ == "__version__":

to

if __name__ == "__main__":

as __main__ is the entry-point to the script.

The entire script:

#!/usr/bin/env python

import sys
import tweepy

def get_tweets(api, input_query):
    for tweet in tweepy.Cursor(api.search, q=input_query, lang="en").items():
        yield tweet

if __name__ == "__main__":
    input_query = sys.argv[1]

    access_token = "REPLACE_YOUR_KEY_HERE"
    access_token_secret = "REPLACE_YOUR_KEY_HERE"
    consumer_key = "REPLACE_YOUR_KEY_HERE"
    consumer_secret = "REPLACE_YOUR_KEY_HERE"
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    tweets = get_tweets(api, input_query)
    for tweet in tweets:
        print(tweet.text)
  • Awesome. Thank you both! So, when I try to run this in the command prompt I get this error message C:\Users\X>print_tweets.py "subject" 'print_tweets.py' is not recognized as an internal or external command, operable program or batch file. What would cause this? – Aoitori Apr 05 '17 at 00:23
  • Try adding `python` before `print_tweets.py`, so the full command should have 3 parts: `python print_tweets.py subject` – chickity china chinese chicken Apr 05 '17 at 00:29
  • Thank you. Finally, should print_tweets.py work based on the script above? How do I store the script as "print_tweets.py" so the command works? – Aoitori Apr 05 '17 at 00:41
  • The script above should be contained in a plain-text file with the `.py` extension. You can give the file any name you like, I gave the script the name `print_tweets.py` as in your question. Then the command `python print_tweets.py subject` should work. Here's a better explanation: [How do I run Python script using arguments in windows command line](http://stackoverflow.com/questions/17544307/how-do-i-run-python-script-using-arguments-in-windows-command-line) – chickity china chinese chicken Apr 05 '17 at 00:45
  • You are a lifesaver. Thank you so much! – Aoitori Apr 05 '17 at 00:54
  • Does it all make sense? If it worked, and my answer helped, please upvote it and click on the check mark beside the answer to toggle it green [like this picture](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235). If so, I'm very glad to help you, good luck! Cheers! :) – chickity china chinese chicken Apr 05 '17 at 00:56