This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2010-12-01 11:33:41

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Edited scratch to post to Facebook?

Aaaaaages ago, I found an amazing project by Chalkmarrow that uses Scratch's external sensor connections to connect to python and allow you to post messages to a twitter account! (here's the python code)

Code:

#
# SCRATCHTWEET: A Python middleware program that lets you send Twitter updates via MIT's Scratch software
# Chalkmarrow, 2009. This code is released in the public domain, with no warranty or indemnification.
# Uses the twitter-python API by Dewitt Clinton, http://code.google.com/p/python-twitter/
#

from array import array
import time
import re
from re import search
import math
import socket
import ConfigParser
import getopt
import os
import sys
import twitter

HOST = '127.0.0.1'
PORT = 42001
dummyString = "&thereisnocowlevel"

def sendScratchCommand(cmd):
    n = len(cmd)
    a = array('c')
    a.append(chr((n >> 24) & 0xFF))
    a.append(chr((n >> 16) & 0xFF))
    a.append(chr((n >>  8) & 0xFF))
    a.append(chr(n & 0xFF))
    scratchSock.send(a.tostring()+cmd)

def sendScratchBroadcast(broadcast):
    sendScratchCommand('broadcast \"'+broadcast+'\"')
    
def sendScratchSensor(variable, value):
    sendScratchCommand('sensor-update \"'+variable+'\"'+' \"'+value+'\" ')
    
def tweet(msg):
    api = twitter.Api(username=username, password=password, input_encoding=None)
    api.SetXTwitterHeaders("ScratchTweet", "http://scratch.mit.edu", "0.1")
    status = api.PostUpdate(msg)
    print "%s just posted: %s" % (status.user.name, status.text)
    sendScratchBroadcast("TweetSent")

# Connect to port 42001 of Scratch
print("Trying to connect to Scratch socket 42001")
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect((HOST, PORT))
print("Connected!")
sendScratchBroadcast("PyConnected")

while 1:
    
    # Wait for something from Scratch
    scratchdata=scratchSock.recv(1024)
    if not scratchdata: break
    
    # Received something
    print 'Received from Scratch:'+scratchdata # Echo what was received
    
    # Look for tweet broadcast
    pattern = 'broadcast \"tweet\"'
    result = search(pattern, scratchdata)
    if result:
        print 'tweet broadcast received'
        tweet(message)
        

    # Look for change in username variable    
    pattern = '\"Scratch-username\" \"(.*?)\"'
    result = search(pattern, scratchdata)
    if result and (result.group(1) != dummyString):
        username = result.group(1)
        print 'username = '+ username

    # Look for change in password variable    
    pattern = '\"Scratch-password\" \"(.*?)\"'
    result = search(pattern, scratchdata)
    if result and (result.group(1) != dummyString):
        password = result.group(1)
        print 'password = '+ password

    # Look for change in message variable    
    pattern = '\"Scratch-message\" \"(.*?)\"'
    result = search(pattern, scratchdata)
    if result and (result.group(1) != dummyString):
        message = result.group(1)
        print 'message = ' + message
    
               
scratchSock.close()

and here's a link to the original page that holds an explaination and a scratch project to work with the code. http://scratchconnections.wik.is/User:C … ratchtweet

What I'm wondering, as I'm not a twitter user, is if it would be possible to edit this python code to create a way for scratch to post a message on your facebook page? I thought bringing it here would allow us all to work on this (if anyone wants to  tongue  )

My attempt at getting it to work for facebook was just to change any mention or link to a twitter page to a link on the facebook site, but as I don't know much python, it was a bit of a shot in the dark...

I found this link which seems to be a good place to start as it has some python code for posting on facebook! http://www.elfsternberg.com/2009/08/31/ … nd-django/

Last edited by sparks (2010-12-01 11:38:03)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#2 2010-12-01 13:24:04

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: Edited scratch to post to Facebook?

That is a good Idea! But I don't know/understand python!

Offline

 

#3 2010-12-01 17:17:38

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Edited scratch to post to Facebook?

Well I think it's a case of cutting the bits relevant only to Twitter out of the code and putting the code for facebook that I linked to in it's place... just need to get it right without cutting out any of the necessary Python scripts...

the part:

def tweet(msg):
    api = twitter.Api(username=username, password=password, input_encoding=None)
    api.SetXTwitterHeaders("ScratchTweet", "http://scratch.mit.edu", "0.1")
    status = api.PostUpdate(msg)
    print "%s just posted: %s" % (status.user.name, status.text)
    sendScratchBroadcast("TweetSent")

looks to me to be the part responsible for actually posting the tweet... I reckon that's probably the part that needs to be swapped for the facebook one...

Last edited by sparks (2010-12-01 17:20:33)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#4 2010-12-01 17:36:42

johnnydean1
Scratcher
Registered: 2010-02-12
Posts: 1000+

Re: Edited scratch to post to Facebook?

sparks wrote:

def tweet(msg):
    api = twitter.Api(username=username, password=password, input_encoding=None)
    api.SetXTwitterHeaders("ScratchTweet", "http://scratch.mit.edu", "0.1")
    status = api.PostUpdate(msg)
    print "%s just posted: %s" % (status.user.name, status.text)
    sendScratchBroadcast("TweetSent")

Let me have a go at deciphering:

I dont use twitter so I dont know what this stuff is:
{
def tweet(msg):
    api = twitter.Api(username=username, password=password, input_encoding=None)
    api.SetXTwitterHeaders("ScratchTweet", "http://scratch.mit.edu", "0.1")
    status = api.PostUpdate(msg)
}

    display %s (username) just posted %s (tweet)
    send broadcast to Scratch "TweetSent"


You can now reach me on Twitter @johnnydean1_

Offline

 

#5 2010-12-02 18:15:03

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Edited scratch to post to Facebook?

Do note that it uses a Twitter Module ( import twitter ) which probably is doing most of the work. But there are ways to fill in forms with other modules. if you search for "python fill in web forms" it should give you some answers. This may be of some user: http://ubuntuforums.org/showthread.php?t=824350

Hope that helps!  smile

Offline

 

#6 2010-12-02 19:40:17

midnightleopard
Scratcher
Registered: 2007-09-13
Posts: 1000+

Re: Edited scratch to post to Facebook?

i am fluent in python. I should check this out later.


http://pwp.wizards.com/5103673563/Scorecards/Landscape.png

Offline

 

#7 2010-12-03 06:17:00

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Edited scratch to post to Facebook?

thanks magnie and midnightleopard! I'll have a look after school today!

midnightleopard, if you're fluent then please do check this out later, and I laughed pretty hard at your sig, very neat!


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#8 2010-12-03 17:13:14

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Edited scratch to post to Facebook?

Right, so this is my not-understanding-python-much evaluation of the existing code...

can be kept
from array import array
import time
import re
from re import search
import math
import socket
import ConfigParser
import getopt
import os
import sys
import twitter <can't be kept, refers to twitter, what replaces it?

HOST = '127.0.0.1' connects to scratch
PORT = 42001 scratch remote connections port is 42001
dummyString = "&thereisnocowlevel"

def sendScratchCommand(cmd): if this section is just naming... objects?...
    n = len(cmd)
    a = array('c')
    a.append(chr((n >> 24) & 0xFF))
    a.append(chr((n >> 16) & 0xFF))
    a.append(chr((n >>  8) & 0xFF))
    a.append(chr(n & 0xFF))
    scratchSock.send(a.tostring()+cmd)

def sendScratchBroadcast(broadcast):
    sendScratchCommand('broadcast \"'+broadcast+'\"')
   
def sendScratchSensor(variable, value):
    sendScratchCommand('sensor-update \"'+variable+'\"'+' \"'+value+'\" ')
   
def tweet(msg): Then this probably needs replacing with the facebook equivalent
    api = twitter.Api(username=username, password=password, input_encoding=None)
    api.SetXTwitterHeaders("ScratchTweet", "http://scratch.mit.edu", "0.1")
    status = api.PostUpdate(msg)
    print "%s just posted: %s" % (status.user.name, status.text)
    sendScratchBroadcast("TweetSent") < broadcast name can be easily renamed

# Connect to port 42001 of Scratch
print("Trying to connect to Scratch socket 42001")
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect((HOST, PORT))
print("Connected!")
sendScratchBroadcast("PyConnected")< same again

while 1:
   
    # Wait for something from Scratch
    scratchdata=scratchSock.recv(1024)
    if not scratchdata: break
   
    # Received something
    print 'Received from Scratch:'+scratchdata # Echo what was received
   
    # Look for tweet broadcast Nooooo idea.
    pattern = 'broadcast \"tweet\"'
    result = search(pattern, scratchdata)
    if result:
        print 'tweet broadcast received'
        tweet(message)
       

    # Look for change in username variable    might need changing but I don't think so...
    pattern = '\"Scratch-username\" \"(.*?)\"'
    result = search(pattern, scratchdata)
    if result and (result.group(1) != dummyString):
        username = result.group(1)
        print 'username = '+ username

    # Look for change in password variable   
    pattern = '\"Scratch-password\" \"(.*?)\"'
    result = search(pattern, scratchdata)
    if result and (result.group(1) != dummyString):
        password = result.group(1)
        print 'password = '+ password

    # Look for change in message variable   
    pattern = '\"Scratch-message\" \"(.*?)\"'
    result = search(pattern, scratchdata)
    if result and (result.group(1) != dummyString):
        message = result.group(1)
        print 'message = ' + message
   
               
scratchSock.close()

Last edited by sparks (2010-12-03 17:13:25)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#9 2010-12-03 17:27:46

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Edited scratch to post to Facebook?

Of course the neatest thing would be to get smalltalk to post to the facebook page so that you don't have to run python all the time... I'm not sure but I don't think squeak can do that...


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#10 2010-12-03 18:37:04

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Edited scratch to post to Facebook?

sparks wrote:

Right, so this is my not-understanding-python-much evaluation of the existing code...

can be kept
from array import array
import time
import re
from re import search
import math
import socket
import ConfigParser
import getopt
import os
import sys
import twitter <can't be kept, refers to twitter, what replaces it? <- import twitter should be completely removed.

HOST = '127.0.0.1' connects to scratch <- this defines the IP address to connect to 127.0.0.1 is the local IP of every computer, you could connect to a  Scratch program on another computer by change 127.0.0.1 to the computer IP address.
PORT = 42001 scratch remote connections port is 42001 <- Yes, it is.
dummyString = "&thereisnocowlevel"

def sendScratchCommand(cmd): if this section is just naming... objects?... <- This is preparing the command to send to Scratch, it adds a four byte thing to the front of the string, for what purpose, idk...
    n = len(cmd)
    a = array('c')
    a.append(chr((n > > 24) & 0xFF) )
    a.append(chr((n > > 16) & 0xFF) )
    a.append(chr((n > >  8) & 0xFF) )
    a.append(chr(n & 0xFF) )
    scratchSock.send(a.tostring()+cmd)

def sendScratchBroadcast(broadcast): <- As you can see this uses the "def sendScratchCommand(cmd)" function. Sending a broadcast to Scratch.
    sendScratchCommand('broadcast \"'+broadcast+'\"')
   
def sendScratchSensor(variable, value): <- This sets a "network" variable, different from the normal variables.
    sendScratchCommand('sensor-update \"'+variable+'\"'+' \"'+value+'\" ')
   
def tweet(msg): Then this probably needs replacing with the facebook equivalent <- Since this is connecting to Facebook, yes, but it won't work by replacing all twitter with facebook  tongue
    api = twitter.Api(username=username, password=password, input_encoding=None)
    api.SetXTwitterHeaders("ScratchTweet", "http://scratch.mit.edu", "0.1")
    status = api.PostUpdate(msg)
    print "%s just posted: %s" % (status.user.name, status.text)
    sendScratchBroadcast("TweetSent") < broadcast name can be easily renamed <- Yes, but it won't make any difference, you could rename it to FacebookSent, but it will still send the tweet.

# Connect to port 42001 of Scratch
print("Trying to connect to Scratch socket 42001")
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) <- Defines what type of connection, here it is TCP
scratchSock.connect((HOST, PORT)) <- This is where Python really connects to Scratch
print("Connected!")
sendScratchBroadcast("PyConnected")< same again <- Eh, that is just telling a receiver block that Python is connected.

while 1:
   
    # Wait for something from Scratch
    scratchdata=scratchSock.recv(1024)
    if not scratchdata: break
   
    # Received something
    print 'Received from Scratch:'+scratchdata # Echo what was received
   
    # Look for tweet broadcast Nooooo idea. <- this part detects if Scratch is sending a broadcast, if it equals "tweet" then do tweet(message).
    pattern = 'broadcast \"tweet\"'
    result = search(pattern, scratchdata)
    if result:
        print 'tweet broadcast received'
        tweet(message)
       

    # Look for change in username variable    might need changing but I don't think so... <- This doesn't need changing, it just finds what was typed in Scratch for the username.
    pattern = '\"Scratch-username\" \"(.*?)\"'
    result = search(pattern, scratchdata)
    if result and (result.group(1) != dummyString):
        username = result.group(1)
        print 'username = '+ username

    # Look for change in password variable    <- This gets the password
    pattern = '\"Scratch-password\" \"(.*?)\"'
    result = search(pattern, scratchdata)
    if result and (result.group(1) != dummyString):
        password = result.group(1)
        print 'password = '+ password

    # Look for change in message variable    <- This gets the message
    pattern = '\"Scratch-message\" \"(.*?)\"'
    result = search(pattern, scratchdata)
    if result and (result.group(1) != dummyString):
        message = result.group(1)
        print 'message = ' + message
   
               
scratchSock.close() <- this closes the connection with Scratch.

I've added some comments to the code, all start with a "<-" but do remember, the code won't work with these if copy and pasted.  wink

sparks wrote:

Of course the neatest thing would be to get smalltalk to post to the facebook page so that you don't have to run python all the time... I'm not sure but I don't think squeak can do that...

Untrue, you could probably make a block that sends a message to Facebook.

Last edited by Magnie (2010-12-03 18:40:17)

Offline

 

#11 2010-12-03 18:40:58

kinker
Scratcher
Registered: 2010-08-01
Posts: 100+

Re: Edited scratch to post to Facebook?

Magnie wrote:

sparks wrote:

Right, so this is my not-understanding-python-much evaluation of the existing code...

can be kept
from array import array
import time
import re
from re import search
import math
import socket
import ConfigParser
import getopt
import os
import sys
import twitter <can't be kept, refers to twitter, what replaces it? <- import twitter should be completely removed.

HOST = '127.0.0.1' connects to scratch <- this defines the IP address to connect to 127.0.0.1 is the local IP of every computer, you could connect to a  Scratch program on another computer by change 127.0.0.1 to the computer IP address.
PORT = 42001 scratch remote connections port is 42001 <- Yes, it is.
dummyString = "&thereisnocowlevel"

def sendScratchCommand(cmd): if this section is just naming... objects?... <- This is preparing the command to send to Scratch, it adds a four byte thing to the front of the string, for what purpose, idk...
   [blocks] n = len(cmd)
    a = array('c')
    a.append(chr((n >> 24) & 0xFF) )
    a.append(chr((n >> 16) & 0xFF) )
    a.append(chr((n >>  8) & 0xFF) )
    a.append(chr(n & 0xFF) )
    scratchSock.send(a.tostring()+cmd)[/blocks]

def sendScratchBroadcast(broadcast): <- As you can see this uses the "def sendScratchCommand(cmd)" function. Sending a broadcast to Scratch.
    sendScratchCommand('broadcast \"'+broadcast+'\"')
   
def sendScratchSensor(variable, value): <- This sets a "network" variable, different from the normal variables.
    sendScratchCommand('sensor-update \"'+variable+'\"'+' \"'+value+'\" ')
   
def tweet(msg): Then this probably needs replacing with the facebook equivalent <- Since this is connecting to Facebook, yes, but it won't work by replacing all twitter with facebook  tongue
    api = twitter.Api(username=username, password=password, input_encoding=None)
    api.SetXTwitterHeaders("ScratchTweet", "http://scratch.mit.edu", "0.1")
    status = api.PostUpdate(msg)
    print "%s just posted: %s" % (status.user.name, status.text)
    sendScratchBroadcast("TweetSent") < broadcast name can be easily renamed <- Yes, but it won't make any difference, you could rename it to FacebookSent, but it will still send the tweet.

# Connect to port 42001 of Scratch
print("Trying to connect to Scratch socket 42001")
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) <- Defines what type of connection, here it is TCP
scratchSock.connect((HOST, PORT)) <- This is where Python really connects to Scratch
print("Connected!")
sendScratchBroadcast("PyConnected")< same again <- Eh, that is just telling a receiver block that Python is connected.

while 1:
   
    # Wait for something from Scratch
    scratchdata=scratchSock.recv(1024)
    if not scratchdata: break
   
    # Received something
    print 'Received from Scratch:'+scratchdata # Echo what was received
   
    # Look for tweet broadcast Nooooo idea. <- this part detects if Scratch is sending a broadcast, if it equals "tweet" then do tweet(message).
    pattern = 'broadcast \"tweet\"'
    result = search(pattern, scratchdata)
    if result:
        print 'tweet broadcast received'
        tweet(message)
       

    # Look for change in username variable    might need changing but I don't think so... <- This doesn't need changing, it just finds what was typed in Scratch for the username.
    pattern = '\"Scratch-username\" \"(.*?)\"'
    result = search(pattern, scratchdata)
    if result and (result.group(1) != dummyString):
        username = result.group(1)
        print 'username = '+ username

    # Look for change in password variable    <- This gets the password
    pattern = '\"Scratch-password\" \"(.*?)\"'
    result = search(pattern, scratchdata)
    if result and (result.group(1) != dummyString):
        password = result.group(1)
        print 'password = '+ password

    # Look for change in message variable    <- This gets the message
    pattern = '\"Scratch-message\" \"(.*?)\"'
    result = search(pattern, scratchdata)
    if result and (result.group(1) != dummyString):
        message = result.group(1)
        print 'message = ' + message
   
               
scratchSock.close() <- this closes the connection with Scratch.

I've added some comments to the code, all start with a "<-" but do remember, the code won't work with these if copy and pasted.  wink

sparks wrote:

Of course the neatest thing would be to get smalltalk to post to the facebook page so that you don't have to run python all the time... I'm not sure but I don't think squeak can do that...

Untrue, you could probably make a block that sends a message to Facebook.

Could that be real?


Put in the weirdness: http://i54.tinypic.com/zl6fph.pnghttp://img821.imageshack.us/i/gobanim2.gif/kinker style! [url]http://internetometer.com/image/16724.png[/url]♬♫ 92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. ♫♪

Offline

 

#12 2010-12-03 18:47:50

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Edited scratch to post to Facebook?

A block that sends a message to Facebook? Sure, send a forum post? Sure.Most of what I've seen in Scratch Mods are just blocks that don't do very much interaction with web pages. You could probably make a block where you input a URL, Username, Password, Message, Topic #/Post # and it could post a message to that topic on any forum.

Offline

 

#13 2010-12-04 14:51:08

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Edited scratch to post to Facebook?

that sounds to me to be a better alternative to working with python running, as it means a user would not have to install python to use the block... I have even less idea where to start with smalltalk code for posting to facebook. I googled "squeak posting to facebook" and several similar searches without any luck though...  smile


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#14 2010-12-04 17:24:50

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Edited scratch to post to Facebook?

Google something like "Squeak Fill Web Forms".  smile

Offline

 

Board footer