How to tweet from RM

homero_merinohomero_merino Member Posts: 5 Contributor I
edited December 2018 in Help

Hi community,

 

I want to know if there's a way to post a tweet from a RM process. I want to automate a "posting" process, and want to know if it is possible to reuse the same connection used to access Twitter.

 

If it's not possible, what do you recommend? I was thinking as an alternative option to use Twython inside a Python script. However, I would prefer to use my current's Twitter connection instead of hardcoding in the script the OAUTH_TOKEN or OAUTH_TOKEN_SECRET.

 

Any PRO suggestion for this? Thanks in advance!

 

Kind regards,

 

Answers

  • JEdwardJEdward RapidMiner Certified Analyst, RapidMiner Certified Expert, Member Posts: 578 Unicorn

    Here's a link to a video with the Twython setup you propose by the amazing Thomas Ott. 

     

    https://www.neuralmarkettrends.com/using-python-with-rapidminer/

     

    Edit: or it would be if the video was still up.  @Thomas_Ott can you help? 

  • Thomas_OttThomas_Ott RapidMiner Certified Analyst, RapidMiner Certified Expert, Member Posts: 1,761 Unicorn

    As far as the video, I did that when I was part of RapidMiner so they uploaded it. I would check with @sgenzer to see if if it's still up there.

     

    I used the Execute Program operator to do this because it was before the Execute Python operator was created. See below for the XML. I can't find the Python code anymore but if I remember correctly it used the sample Twython script to post a Tweet, except I dropped in the macro as part of the Tweet body. 

     

    <?xml version="1.0" encoding="UTF-8"?><process version="7.6.003">
    <context>
    <input/>
    <output/>
    <macros/>
    </context>
    <operator activated="true" class="process" compatibility="7.6.003" expanded="true" name="Process">
    <parameter key="encoding" value="SYSTEM"/>
    <process expanded="true">
    <operator activated="true" class="retrieve" compatibility="7.6.003" expanded="true" height="68" name="Retrieve WordList (2)" width="90" x="45" y="30">
    <parameter key="repository_entry" value="../data/WordList"/>
    </operator>
    <operator activated="true" breakpoints="after" class="sort" compatibility="7.6.003" expanded="true" height="82" name="Sort" width="90" x="179" y="30">
    <parameter key="attribute_name" value="total"/>
    <parameter key="sorting_direction" value="decreasing"/>
    </operator>
    <operator activated="true" breakpoints="after" class="filter_examples" compatibility="6.4.000" expanded="true" height="103" name="Filter Examples" width="90" x="313" y="30">
    <list key="filters_list">
    <parameter key="filters_entry_key" value="word.equals.science"/>
    </list>
    </operator>
    <operator activated="true" class="extract_macro" compatibility="7.6.003" expanded="true" height="68" name="Extract Macro" width="90" x="447" y="30">
    <parameter key="macro" value="word_search"/>
    <parameter key="macro_type" value="data_value"/>
    <parameter key="attribute_name" value="word"/>
    <parameter key="example_index" value="1"/>
    <list key="additional_macros"/>
    </operator>
    <operator activated="true" class="text:create_document" compatibility="7.5.000" expanded="true" height="68" name="Create Document" width="90" x="45" y="165">
    <parameter key="text" value="#!/usr/bin/env python&#10;&#10;from twython import Twython, TwythonError&#10;&#10;app_key = &quot;BJBCxn0eXsA5T22om4EXoMee1&quot;&#10;app_secret = &quot;whSiH8aKRqqfHFId8RsOilHqd1kMSQkznKJGHwbIPCmt6JXxpr&quot;&#10;oauth_token = &quot;9224982-IAF4VH9VMbghZtnGf61wFy0jqv5LKq9XNu8gHtU9Hj&quot;&#10;oauth_token_secret = &quot;xFrMSHZ4a1ZvyyRmhzspJzVzV9c2sJLEcZdtqgGFhZkbH&quot;&#10;&#10;api = Twython(app_key, app_secret, oauth_token, oauth_token_secret)&#10;&#10;#Setting Twitter's search results as a variable&#10;search_results = api.search(q=&quot;%{word_search}&quot;, count=2)&#10;try:&#10; for tweet in search_results[&quot;statuses&quot;]:&#10; api.retweet(id = tweet[&quot;id_str&quot;])&#10;except TwythonError as e:&#10; print e&#10;&#10;"/>
    </operator>
    <operator activated="true" class="text:write_document" compatibility="7.5.000" expanded="true" height="82" name="Write Document" width="90" x="179" y="165">
    <parameter key="encoding" value="SYSTEM"/>
    </operator>
    <operator activated="true" class="write_file" compatibility="7.6.003" expanded="true" height="68" name="Write File" width="90" x="313" y="165">
    <parameter key="filename" value="C:\Users\tott_000\Dropbox\PythonScripts\%{word_search}.py"/>
    </operator>
    <operator activated="true" class="productivity:execute_program" compatibility="7.6.003" expanded="true" height="103" name="Execute Program" width="90" x="447" y="165">
    <parameter key="command" value="python %{word_search}.py"/>
    <parameter key="working_directory" value="C:\Users\tott_000\Dropbox\PythonScripts"/>
    <list key="env_variables"/>
    </operator>
    <operator activated="false" class="text:create_document" compatibility="7.5.000" expanded="true" height="68" name="Create Document (2)" width="90" x="45" y="255">
    <parameter key="text" value="#!/usr/bin/env python&#10;&#10;from twython import Twython, TwythonError&#10;&#10;app_key = &quot;xxxxxxxxxxxxxxxxxx&quot;&#10;app_secret = &quot;xxxxxxxxxxxxxxxxxx&quot;&#10;oauth_token = &quot;xxxxxxxxxxxxxxxxxx&quot;&#10;oauth_token_secret = &quot;xxxxxxxxxxxxxxxxxx&quot;&#10;&#10;api = Twython(app_key, app_secret, oauth_token, oauth_token_secret)&#10;&#10;#Setting Twitter's search results as a variable&#10;search_results = api.search(q=&quot;%{word_search}&quot;, count=2)&#10;try:&#10; for tweet in search_results[&quot;statuses&quot;]:&#10; api.retweet(id = tweet[&quot;id_str&quot;])&#10;except TwythonError as e:&#10; print e&#10;"/>
    </operator>
    <connect from_op="Retrieve WordList (2)" from_port="output" to_op="Sort" to_port="example set input"/>
    <connect from_op="Sort" from_port="example set output" to_op="Filter Examples" to_port="example set input"/>
    <connect from_op="Filter Examples" from_port="example set output" to_op="Extract Macro" to_port="example set"/>
    <connect from_op="Create Document" from_port="output" to_op="Write Document" to_port="document"/>
    <connect from_op="Write Document" from_port="file" to_op="Write File" to_port="file"/>
    <connect from_op="Write File" from_port="file" to_op="Execute Program" to_port="in"/>
    <connect from_op="Execute Program" from_port="out" to_port="result 1"/>
    <portSpacing port="source_input 1" spacing="0"/>
    <portSpacing port="sink_result 1" spacing="0"/>
    <portSpacing port="sink_result 2" spacing="0"/>
    </process>
    </operator>
    </process>

     

     

  • sgenzersgenzer Administrator, Moderator, Employee, RapidMiner Certified Analyst, Community Manager, Member, University Professor, PM Moderator Posts: 2,959 Community Manager

    hi @homero_merino - you're right. It should be a fifth Twitter operator ("Send Tweet" or something like that). They're all just calls to the Twitter API anyway. If you want I can probably play around and figure out how to do this with Enrich Data via Webservice, or if OAuth2 gives me a hard time, shell scripting using Execute Program (cURL).  I've done both before with other APIs.

     

    Scott

     

  • homero_merinohomero_merino Member Posts: 5 Contributor I

    Hi Thomas,

     

    Thank you for your reply.. actually your Python code looks like the one I was wondering to implement; thank you for it.

     

    I need to tell you that you left the app_keyapp_secretoauth_tokenoauth_token_secret params set on your process. I think you should edit your post to avoid unauthorized accesses. That's the reason why I don't like a lot the script solution... 'cause these access params go hardcoded and it's easy for anyone to see them but, well I think it is the only way right now.

     

    Thanks a lot! Kind regards,

     

  • Thomas_OttThomas_Ott RapidMiner Certified Analyst, RapidMiner Certified Expert, Member Posts: 1,761 Unicorn

    Yeah that OAuth stuff is old, it doesn;t work anymore. I hope. :)

  • JEdwardJEdward RapidMiner Certified Analyst, RapidMiner Certified Expert, Member Posts: 578 Unicorn

    To avoid hardcoding the auth tokens in your script you can pass them in as macros using the Execute Python operator. 

     

    The values of the macros can be stored in a database table (encrypted) which you provide access to only to the authorized users. 

    (Store the data in the database encrypted for added security). 

     

    This has the added advantage of making the process portable as you can store multiple auth tokens in the same table (or set of tables) and then only use the one.  It still means that the tokens can be extracted if the person puts in the right amount of effort, but is much more difficult.  You can also use RapidMiner Server to authorize access to the table with the credentials in and even serve certain tables to certain login users.  However, the easiest way is for a future Send Tweet operator to come in. 

     

     

  • sgenzersgenzer Administrator, Moderator, Employee, RapidMiner Certified Analyst, Community Manager, Member, University Professor, PM Moderator Posts: 2,959 Community Manager

    oh huh that's pretty clever, @JEdward. Do you have a sample you can share here?

     

    Scott

     

  • Thomas_OttThomas_Ott RapidMiner Certified Analyst, RapidMiner Certified Expert, Member Posts: 1,761 Unicorn

    My suggestion would've been to macro the OAuth keys but @JEdward beat me to it. 

Sign In or Register to comment.