Skip to content

Instantly share code, notes, and snippets.

@rochacbruno
Last active May 3, 2016 06:47
Show Gist options
  • Save rochacbruno/4375818 to your computer and use it in GitHub Desktop.
Save rochacbruno/4375818 to your computer and use it in GitHub Desktop.
fb client
#!/usr/bin/python
# coding: utf-8
##############################################################################
# DRAFT 0.3
#
# REQUIREMENTS
# $ pip install fbconsole
#
# USAGE
# - direct post
# $ python fb.py "Posting to facebook"
#
# - post to friends wall
# $ python fb.py /username "posting on username wall"
#
# - Post a picture
# $ python fb.py photo /tmp/file.png "this is a picture"
#
# - Retrieving latest 5 posts from your news fedd
# $ python fb.py news 5
# - 3 latest posts by you
# $ python fb.py posts 3
# - Reading 8 latests posts on your wall
# $ python fb.py wall 8
# - Listing latest 30 things you liked
# $ python fb.py likes 30
#
# Onptionally you can do:
# sudo -i
# ln -s /path/fb.py /bin/fb
# then you have a command "fb" to call directly
# $ fb "updating my facebook status"
##############################################################################
import fbconsole
import sys
fbconsole.AUTH_SCOPE ='publish_stream,publish_checkins,read_mailbox,read_requests,read_stream,manage_notifications,'.split(',')
fbconsole.authenticate()
def post(args):
to = args[0] + "/feed" if args[0].startswith("/") else "/me/feed"
data = dict(message=args[0] if len(args) == 1 else args[1])
print fbconsole.post(to, data)[u"id"]
def postphoto(args):
to = "/me/photos"
data = dict(source=open(args[0]))
if len(args) > 1:
data["message"] = args[1]
print fbconsole.post(to, data)[u"id"]
def getstuff(where, args):
limit=5 if not args else int(args[0])
print limit
data = dict(limit=limit)
total = 1
for post in fbconsole.iter_pages(fbconsole.get(where, data)):
total += 1
print post
print post.get('message')
print "#"*80
if total > limit:
break
functions = dict(
post=post,
photo=postphoto,
posts=lambda args: getstuff("/me/posts", args),
wall=lambda args: getstuff("/me/feed", args),
news=lambda args: getstuff("/me/home", args),
likes=lambda args: getstuff("/me/likes", args)
)
if __name__ == "__main__":
args = sys.argv[1:]
functions.get(args[0], lambda x: post(args))(args[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment