Login | Register 
FEATURES
APPLICATIONS
DOWNLOADS
EDUCATION
BLOG
WIKI
FORUM
STORE

Threaded Web COMP

Share your components here. First one to 100 wins!

Re: Threaded Web COMP

Postby gionniz » Wed Nov 09, 2016 11:14 am

I am getting this error, and don't really understand why...
any idea?

{'status': 'Thread Started'}
python >>>
{'status': '##start post request'}
python >>>
{'code': 401, 'encoding': 'ISO-8859-1'}
python >>>
{'msg': b'<html>\\n<head>\\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>\\n<title>Error 401 Unauthorized</title>'}
python >>>
{'msg': b'</head>'}
python >>>
{'msg': b'<body>'}
python >>>
{'msg': b'<h2>HTTP ERROR: 401</h2>'}
python >>>
{'msg': b"<p>Problem accessing '/1.1/statuses/filter.json'. Reason:"}
python >>>
{'msg': b'<pre> Unauthorized</pre>'}
python >>>
{'msg': b'</body>'}
python >>>
{'msg': b'</html>'}
python >>>
{'status': '##end of message'}
python >>>
gionniz
 
Posts: 40
Joined: Sat Nov 30, 2013 10:32 am

Re: Threaded Web COMP

Postby snaut » Wed Nov 09, 2016 4:56 pm

Hi gionniz.

what is it that you are trying to do?
You get a default "Unauthorized" response from the server indicating that your credentials are not correct//

Cheers
Markus
User avatar
snaut
Staff
 
Posts: 896
Joined: Mon Nov 12, 2007 6:21 pm
Location: Toronto

Re: Threaded Web COMP

Postby snaut » Fri Dec 09, 2016 2:32 pm

The question came up how to post images to Twitter.
I changed the Send method a bit to allow for specifying the url for each Send you are doing - this is especially useful when having to respond to events received from a server. Please download the latest WebComp zip from the first post.
First you will need to read in the file, then upload the file and finally post a status update with the id which twitter returns after successfully having received your image.

Here is a little guide:
1. Follow the Steps here: to create your Twitter Application and get all necessary OAuth info.

2.The Imagedata needs to be base64 encoded before it can be send to Twitter:
Code: Select all
import base64

# save out a TOP
file = op('noise1').save('noise1.jpg')

# open the saved out file in binary format and read it into a variable
imgFile = open(file, 'r+b')
imgData = imgFile.read()

# encode as base64 and convert into a string
b64Data = base64.b64encode(imgData)
dataString = b64Data.decode()

# create a data dictionary you want to send to twitter
dataDict = {}
dataDict['media_data'] = dataString
dataDict['Name'] = 'An Image from TouchDesigner'

# send the data to Twitter
op('web1').Send(data=dataDict, url='https://upload.twitter.com/1.1/media/upload.json')


3. Now we wait for an response from Twitter and post an status update with the img id.
For this we need to create a script in the web1_callbacks DAT:

Code: Select all
import json
def receive(data):
   # see if the data contains a message part
   msg = data.get('msg',None)
   if msg:
      # the returned message will be binary so lets decode this and convert the json into a dict
      msg = msg.decode()
      answerDict = json.loads(msg)

      # see if we can retrieve a media_id
      image = answerDict.get('media_id',None)
      if image:
         # create a data dictionary which contains the status update text and the uploaded media id
         dataDict = {}
         dataDict['media_ids'] = image
         dataDict['status'] = 'Testing Image Upload from TouchDesigner099'
         # send it to twitter
         op('web1').Send(data=dataDict, url='https://api.twitter.com/1.1/statuses/update.json')
      else:
         print(answerDict)
   else:
      print(data)
   return


Be advised that there is a limit on how much you can upload. So don;t run these scripts every frame. Also keep your Twitter applications OAuth keys and secret save!!!!
User avatar
snaut
Staff
 
Posts: 896
Joined: Mon Nov 12, 2007 6:21 pm
Location: Toronto

Re: Threaded Web COMP

Postby tomlimbic » Mon Jan 09, 2017 12:55 pm

Sorry for the newb question, anyone able give me a pointer on how to get the twitter stream python data that is successfully going into textport to a text DAT?

Something I can use to then select just the message and eventually send it to a text TOP?

Bit stuck on this one.

Thanks in advance
tomlimbic
 
Posts: 1
Joined: Tue Nov 08, 2016 7:03 am

Re: Threaded Web COMP

Postby prochoy » Tue Jan 10, 2017 11:42 am

The supplied callbacks are creating a python dictionary of all keys/values and printing them to the console. You simply need to find which keys you would like to use and assign their value to whatever operators you would like. I would suggest reading through how to parse python dictionaries/lists.

Below is a simple callback eample that assigns the user's screen name and tweet to Text TOPs...
Code: Select all
import json
def receive(data):
  msg = data.get('msg',None)
  if msg:
     decoded = json.loads(msg.decode())
     for k in decoded.keys():
      if k == 'user':
           print(decoded[k]['screen_name'])
           op('text1').par.text = '@' + decoded[k]['screen_name']
      if k == 'text':
           print(decoded[k])
           op('text2').par.text = decoded[k]
           print('-------------------------')
  return
prochoy
 
Posts: 67
Joined: Wed Dec 31, 2014 2:20 pm

Re: Threaded Web COMP

Postby JMNix » Tue Feb 21, 2017 3:21 pm

Hi everyone! Thanks for putting together this COMP - this has helped open up a world of possibilities for connecting projects to web sources.

I'm working on an interactive projection activation, and we're trying to build a very simple trigger to start playback on movie files, activated by a Twitter feed. We have a third party app that will write the tweets on a @"testAccount" we've set up with the appropriate developer API's, we have our app key/secret/OAuth token/secret in place in the COMP, and we've entered the Twitter account in table4 (where @nasa appears in the original). We even beat some initial 401 Unauthorized issues. It seems like everything should be working, but...

After entering our @"testAccount" in table4, switching web1 to Active, and pulsing Go, we get our code 200 back, but then the script seems to hang with no feedback. Pulsing Stop or switching Active to Off (on web1) does not break the loop, and requires a full restart of TD to regain control of Textport.

Code: Select all
python >>>
{'status': 'Thread Started'}
python >>>
{'status': '##start post request'}
python >>>
{'code': 200, 'encoding': None}
python >>>


Followed by an indefinite hold (10min+ tested)

This only seems to specifically be a problem with our "fresh" Twitter account we created specifically for this activation. Testing @nasa yields no problems - the following Textport comes from only changing our tracked Twitter handle in table4 from @"testAccount" to @nasa (stop initiated by pulsing Stop in web1).

Code: Select all
python >>>
{'status': 'Thread Started'}
python >>>
{'status': '##start post request'}
python >>>
{'code': 200, 'encoding': None}
python >>>
{'msg': b'{"created_at":"Tue Feb 21 18:58:41 +0000 2017","id":834115142975705095,"id_str":"834115142975705095","text":"Nasa announces major press conference on a \'discovery beyond our solar system\' https:\\/\\/t.co\\/HWG2qPOdXy","source":"\\u003ca href=\\"http:\\/\\/twitter.com\\" rel=\\"nofollow\\"\\u003eTwitter Web Client\\u003c\\/a\\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":3012613095,"id_str":"3012613095","name":"Barfield Educational","screen_name":"BarfieldEd","location":"Batley and surrounding areas","url":"http:\\/\\/www.barfieldeducational.co.uk\\/","description":"Providing STEM sessions in schools and social enterprise for disadvantaged children and senior citizens","protected":false,"verified":false,"followers_count":51,"friends_count":147,"listed_count":16,"favourites_count":25,"statuses_count":1243,"created_at":"Mon Feb 02 17:51:27 +0000 2015","utc_o
python >>>
{'status': 'Execution Interrupted, waiting for new commnad'}
python >>>
{'status': '##end of message'}
python >>>


Some other accounts seem to work just fine - we've tried with @cubs and @Yankees (accounts selected arbitrarily), which both worked with no issues (maybe an extra second or two of latency on @Yankees?).

However, we had a similar-but-different issue when we tried to access @AppleSupport - there is roughly a ~20 second latency between Activating/pulsing Go web1 and receiving tweets. Similarly with sending pulses/Offs, Textport holds for 15-20 seconds before returning the Execution Interrupted. Maybe there's a connection?

We've recreated this issue identically on both a laptop running Windows 10 and a desktop running Windows 7 SP1. We're using 099 Commercial, but also have access to 088.

So. With all this in mind - what could we be possibly doing wrong here? Is our testTwitter account not configured properly? Yet that's where we got our OAuth tokens etc from. Any suggestions or insight?

Thanks in advance!
JMNix
 
Posts: 1
Joined: Wed Jan 25, 2017 2:52 pm

Re: Threaded Web COMP

Postby snaut » Wed Mar 08, 2017 6:04 pm

I just tried getting tweets from one single account and made following changes:

The Request Type parameter was set to "GET"
The url to fetch my accounts tweets was set to "https://userstream.twitter.com/1.1/user.json"

I disconnected the table4 from the input as I don't want to filter for certain messages.

And it works well for me - but this only let's me see my own accounts tweets or tweets mentioning my account.

For the behavior you describe, did you change the URL?

Cheers
Markus
User avatar
snaut
Staff
 
Posts: 896
Joined: Mon Nov 12, 2007 6:21 pm
Location: Toronto

Re: Threaded Web COMP

Postby 9delta9 » Sun Apr 02, 2017 5:34 pm

HELP
Attachments
Screen Shot 2017-04-02 at 4.33.05 PM.png
ERROR
Screen Shot 2017-04-02 at 4.33.05 PM.png (349.97 KiB) Viewed 6122 times
9delta9
 
Posts: 1
Joined: Sat Mar 25, 2017 5:48 pm

Re: Threaded Web COMP

Postby snaut » Sun Apr 02, 2017 8:47 pm

requests seems to not be running on OSX? Will try at the office on a mac...

Cheers
Markus
User avatar
snaut
Staff
 
Posts: 896
Joined: Mon Nov 12, 2007 6:21 pm
Location: Toronto

Re: Threaded Web COMP

Postby alanstones » Sun May 07, 2017 10:06 am

I'm getting similar errors trying to run this on a Mac so would be great to hear of a solution.
alanstones
 
Posts: 1
Joined: Fri Apr 07, 2017 11:13 am

PreviousNext

Return to Shared .tox Components

Who is online

Users browsing this forum: No registered users and 4 guests