########################################################################################################## ########################################################################################################## # Jungherr, Andreas: Using Digital Trace Data in the Social Sciences: Twitter as Example (tutorial) # POLNET+ 2016, 8 - 9 July, Konstanz # For a detailed tutorial on how to collect and work with Twitter data in the social sciences see # Jürgens & Jungherr (2016a). # Script Examples to illustrate the workings of the scripts provided in Jürgens & Jungherr (2016b). ###Further Reading: # Pascal Jürgens and Andreas Jungherr. 2016a. A Tutorial for Using Twitter Data in the Social Sciences: # Data Collection, Preparation, and Analysis. Social Science Research Network (SSRN). # doi: 10.2139/ssrn.2710146 # Pascal Jürgens and Andreas Jungherr. 2016b. twitterresearch [Computer software]. # Available at https://github.com/trifle/twitterresearch ########################################################################################################## ########################################################################################################## # First make sure, you have installed python and relevant libraries as described in # (Jürgens & Jungherr 2016a, pp.15-20) # Start your command line or terminal and enter ipython # Now navigate to the working directory where you saved the scripts provided by Jürgens & Jungherr (2016b). cd /(...)/twitterresearch # import python script "examples.py" from script set import examples ########################################################################################################## ### Examples how to collect data through Twitter's streaming APIs ###Keyword-based searches # Load command defined in "examples.py" examples.track_keywords() # Now you should see tweets containing the predifined keywords "politics" or "election" through your command line # To cancel this process enter crtl c # Here you see the command es defined in "examples.py" def track_keywords(): """ Track two keywords with a tracking stream and print machting tweets and notices. To stop the stream, press ctrl-c or kill the python process. """ keywords = ["politics", "election"] stream = streaming.stream( on_tweet=print_tweet, on_notification=print_notice, track=keywords) # You can easily adjust the keywords used in the predifined command as shown here. Just enter the following command. # You can enter the keywords of interest to you in the field "keywords" and replace the placeholder "trump". def track_keywords(): """ Track two keywords with a tracking stream and print machting tweets and notices. To stop the stream, press ctrl-c or kill the python process. """ keywords = ["trump"] stream = examples.streaming.stream( on_tweet=examples.print_tweet, on_notification=examples.print_notice, track=keywords) # Now, let's test the adjusted command track_keywords() # The command describted above allowed you to see tweets corresponding with your search criteria. The following command # allows you to save them in a .json file in your working directory. examples.save_track_keywords() # Here is the command as defined in "examples.py". You can adjust it to your interests as shown above. def save_track_keywords(): """ Track two keywords with a tracking stream and save machting tweets. To stop the stream, press ctrl-c or kill the python process. """ # Set up file to write to outfile = open("keywords_example.json", "w") def save_tweet(tweet): json.dump(tweet, outfile) # Insert a newline after one tweet outfile.write("\n") keywords = ["politics", "election"] stream = streaming.stream( on_tweet=save_tweet, on_notification=print_notice, track=keywords) ###User-based searches # The following command allows you to track tweets coming in from a selection of users. examples.follow_users() # Here is the command as defined in "examples.py". You can adjust it to your interests as shown above. # In this case you exchange the IDs listed in "users" with IDs of your choosing. def follow_users(): """ Follow several users, printing their tweets (and retweets) as they arrive. To stop the stream, press ctrl-c or kill the python process. """ # user IDs are: nytimes: 807095, washingtonpost: 2467791 # they can be obtained through: # users = ["nytimes", "washingtonpost"] # users_json = rest.fetch_user_list_by_screen_name(screen_names=users) # for u in users_json: # print("{0}: {1}".format(u["screen_name"], u["id"])) users = ["807095", "2467791"] stream = streaming.stream( on_tweet=print_tweet, on_notification=print_notice, follow=users) ########################################################################################################## ### Example how to collect data through Twitter's REST APIs #User-based searches # The following command shows all available tweets from a user's archive. examples.print_user_archive() # The following command downloads all available tweets from a user's archive and saves it to a .json file # in your active workding directors. examples.save_user_archive_to_file() ########################################################################################################## ########################################################################################################## # For more examples and advise how to prepare and analyze Twitter data please see the tutorial: # Pascal Jürgens and Andreas Jungherr. 2016a. A Tutorial for Using Twitter Data in the Social Sciences: # Data Collection, Preparation, and Analysis. Social Science Research Network (SSRN). # doi: 10.2139/ssrn.2710146 # If you run into trouble with or find any bugs in the code provided in Jürgens, Jungherr (2016b) please # report an issue in our GitHub repository https://github.com/trifle/twitterresearch ########################################################################################################## ##########################################################################################################