Login | Register 
FEATURES
APPLICATIONS
DOWNLOADS
EDUCATION
BLOG
WIKI
FORUM
STORE

Threaded Web COMP

Share your components here. First one to 100 wins!

Threaded Web COMP

Postby snaut » Fri Jul 29, 2016 2:24 pm

Here we have a component completely based of Elburz's Twitter Component (Thank you very much for the inspiration) which was posted here: viewtopic.php?f=22&t=8167

In creating this component I tried catching as many cases as I can for communicating with different resources on the web.

So this let's you control the Request Type (choose from GET, POST, PUT, DELETE, HEAD and OPTIONS), Stream the incoming data, control the Chunk Size for the Stream.
You also have the option to Authenticate the connection with Basic, Digest, oAuth1 or oAuth2 Authentication. The received data is being passed to a "callback DAT" in which you can deal with it any way you like.

Inputs to the COMP are a header Table DAT which should contain rows with Header name and value pairs. The second input is for the data you would like to send. Especially when sending a request or trying to post data you can also utilize the

Code: Select all
op('webComp').Send(data={'myFile':b'somedata'})


function. This makes it possible to post anything from form fields to full images to a web resource.

When using oAuth1 for example to get the Stream from Twitter, you will have to obtain an App Key, App Secret and an oAuth Token as well as a oAuth Secret which ca be obtained from https://apps.twitter.com/ after creating and registering a new application.

Similar if you like to use oAuth2 with for example Facebook, you will have to supply an Client ID, Client Secret and a Token. To obtain the Token you will have to implement your own authentication steps or obtain a Client Token from the Apps Advanced Setting page at https://developers.facebook.com

I hope this can be a useful tool for the interim. It is probably missing features and I would love to hear about them or see your implementation!

Cheers
Markus
Attachments
webCOMP.zip
TouchDesigner088 62070
(4.2 MiB) Downloaded 593 times
User avatar
snaut
Staff
 
Posts: 904
Joined: Mon Nov 12, 2007 6:21 pm
Location: Toronto

Re: Threaded Web COMP

Postby bLackburst » Mon Aug 01, 2016 1:46 am

Thanks for this.
I know it's difficult to give working examples without sharing auth credentials but it would be great if it came with a quick rundown of settings or examples for other common operations on common platforms. For example getting instagram pics, facebook users, twitter locations etc. There is of course an undelying amount of API programming left to the user but any examples would be appreciated.
bLackburst
 
Posts: 334
Joined: Thu Sep 01, 2011 3:56 am
Location: Melbourne, Australia

Re: Threaded Web COMP

Postby elburz » Tue Aug 02, 2016 3:19 pm

Wow :shock: So clean and tidy. I'd rather use yours then mine :lol:
--
Elburz Sorkhabi
Creative + Technology
nVoid Art-Tech Limited
http://nvoid.com
elburz
 
Posts: 2017
Joined: Fri Jun 01, 2012 6:55 pm
Location: Toronto, Canada

Re: Threaded Web COMP

Postby snaut » Wed Aug 03, 2016 4:12 pm

Small update posted above. After long head scratching I learned that threads should not spin... So in this version I added a threading event which keeps the thread from eating up CPU cycles until a request is send from the outside.

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

Re: Threaded Web COMP

Postby snaut » Thu Aug 04, 2016 2:18 pm

Accessing Twitter
  • go to apps.twitter.com and click the Create New App Button
    Step1.png
    Step1.png (46.41 KiB) Viewed 12160 times
  • fill out the application details, the website can be a dummy address
    Step2.png
    Step2.png (79.52 KiB) Viewed 12160 times
  • go to "Keys and Access Tokens" and click "Create my access token"
    Step3.png
    Step3.png (85.12 KiB) Viewed 12160 times
  • get App Key, App Secret, User OAuth Token and User OAuth Secret from Consumer Key, Consumer Secret, Access Token and Access Token Secret
    Step4.png
    Step4.png (88.35 KiB) Viewed 12160 times
  • set Authentication Type of the web Component to OAuth1 and fill in the parameters
Step5.png
Step5.png (38.31 KiB) Viewed 12160 times

connecting to Streaming API of Twitter:
For an overview of the available parameters refer to Twitter Documentation here
  • on the Web Parameter Page of the COMP set Request type to "Post", the URL to "https://stream.twitter.com/1.1/statuses/filter.json" and turn on the "Stream" parameter.
    Step6.png
    Step6.png (13.14 KiB) Viewed 12160 times
  • add a Table DAT to the second Input of the COMP and specify any of the request parameters listed in the twitter api documentation
    Step7.png
    Step7.png (9.94 KiB) Viewed 12160 times
  • toggle the "Activate" parameter and pulse the "Go" parameter. In the textport you should see messages coming in.
    Step8.png
    Step8.png (26.29 KiB) Viewed 12160 times
  • the incoming data is send to the docked DAT called web1_callback. Here it literally just prints our a dictionary with the keys
    • msg
    • status
    • code
    • encoding
  • while the stream is active there will only be the msg key and it's value containing whatever comes down the stream
  • to decode the twitter message enter this in the web1_callback DAT:
    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():
             print('key:',k,'value:',decoded[k])
       return

    Step9.png
    Step9.png (29.05 KiB) Viewed 12160 times
  • to stop the stream, pulse the "Stop" parameter.
  • changing the request parameters only takes effect after stopping and restarting the stream.

connecting to REST API of Twitter:
For an overview of the available connection points refer to Twitter Documentation here
  • To see the last tweets of a twitter account, choose the connection point GET statuses/user_timeline by entering the url https://api.twitter.com/1.1/statuses/user_timeline.json into the "URL" parameter of the COMP.
  • Choose the GET type in the "Request Type" parameter.
  • Turn of the "Stream" parameter
  • add a Table DAT to the second Input of the COMP and specify any of the request parameters listed in the twitter api documentation (for example "screen_name")
  • Pulse the "Go" parameter
  • To decode this response enter this code into the web1_callbacks DAT:
    Code: Select all
    import json
    def receive(data):
       msg = data.get('msg',None)
       if msg:
          
          msg = msg.decode()
          decoded = json.loads(msg)
          for i in decoded:
             print('-------------------------')
             for k in i.keys():
                print('key:',k,'value:',i[k])
          return
User avatar
snaut
Staff
 
Posts: 904
Joined: Mon Nov 12, 2007 6:21 pm
Location: Toronto

Re: Threaded Web COMP

Postby snaut » Thu Aug 04, 2016 3:46 pm

Accessing Facebooks Graph API
  • go to https://developers.facebook.com/ and click the "Add a new App" from the "My Apps" dropdown.
    step1.png
    step1.png (120.83 KiB) Viewed 12155 times
  • fill out the application details and click "Create App ID"
    step2.png
    step2.png (36.25 KiB) Viewed 12155 times
  • go to "Settings>Basic" and note the "App ID" as well as "App Secret"
    step3.png
    step3.png (24.67 KiB) Viewed 12155 times
  • click on "Tools & Support" and then click on the "Graph API Explorer"
  • choose your app from the Application drop down
    step4.png
    step4.png (26.8 KiB) Viewed 12155 times
  • copy the "Access Token"
    step5.png
    step5.png (29.93 KiB) Viewed 12155 times
  • on the Web COMP select OAuth2 on the "Authentication Type" parameter and fill in the previously collected App Id, App Secret and Token
    step6.png
    step6.png (14.26 KiB) Viewed 12155 times
  • refer to the Graph API on how to retrieve information from the Facebook Graph
get your own feed
  • set the Web COMP "Request Type" parameter to "Get"
  • enter https://graph.facebook.com/v2.7/me/feed into the "URL" parameter
  • pulse the "Go" parameter
  • replacing "me" with a user id will return that users feed
User avatar
snaut
Staff
 
Posts: 904
Joined: Mon Nov 12, 2007 6:21 pm
Location: Toronto

Re: Threaded Web COMP

Postby bLackburst » Thu Aug 04, 2016 9:23 pm

Just wonderful Markus, can't thank you enough. This is really making TD much more valuable for commercial projects that need custom social media displays.
bLackburst
 
Posts: 334
Joined: Thu Sep 01, 2011 3:56 am
Location: Melbourne, Australia

Re: Threaded Web COMP

Postby bLackburst » Tue Sep 06, 2016 4:16 am

It seems to have trouble with the json decoded callbacks provided. Won't start thread, intermittent.
bLackburst
 
Posts: 334
Joined: Thu Sep 01, 2011 3:56 am
Location: Melbourne, Australia

Re: Threaded Web COMP

Postby snaut » Thu Oct 06, 2016 4:20 pm

Can you give me instructions on how to duplicate?
User avatar
snaut
Staff
 
Posts: 904
Joined: Mon Nov 12, 2007 6:21 pm
Location: Toronto

Re: Threaded Web COMP

Postby bLackburst » Tue Oct 11, 2016 7:34 pm

When I enter your code into the callbacks dat :
import json
def receive(data):
msg = data.get('msg',None)
if msg:
decoded = json.loads(msg.decode())
for k in decoded.keys():
print('key:',k,'value:',decoded[k])
return

I get very intermittent functionality. I'll try to dig up a toe when I get the time
bLackburst
 
Posts: 334
Joined: Thu Sep 01, 2011 3:56 am
Location: Melbourne, Australia

Next

Return to Shared .tox Components

Who is online

Users browsing this forum: No registered users and 4 guests