Skip to content

Commit

Permalink
Some improvements to send_tweet example
Browse files Browse the repository at this point in the history
- [x] Handling the exception when libturpial is not in the sys.path
- [x] Now it's possible to parse arguments from the command line
- [] Support for identi.ca protocol
  • Loading branch information
milmazz committed Jul 8, 2013
1 parent 5b0f444 commit 45feaf2
Showing 1 changed file with 49 additions and 17 deletions.
66 changes: 49 additions & 17 deletions examples/send_tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,60 @@
# -*- coding: utf-8 -*-

# Libturpial usage example
#
#
# Author: Carlos Guerrero <guerrerocarlos@gmail.com>
# 24 June 2013

import os
import sys
import argparse

from libturpial.api.models.account import Account
from libturpial.api.core import Core
try:
from libturpial.api.models.account import Account
from libturpial.api.core import Core
except ImportError:
path = \
os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')
sys.path.append(path)

account = "username-twitter" #Replace <username> with the user you want to send tweet with
message = "Tweet sent using Libturpial"
from libturpial.api.models.account import Account
from libturpial.api.core import Core

c = Core()
accounts = c.list_accounts()
core = Core()

if account in accounts:
c.update_status(account,message)
# Parsing the command line
description = "Send a message from libturpial"
parser = argparse.ArgumentParser(description=description)
parser.add_argument("-u", "--username", dest="username",
help="Username", required=True)
parser.add_argument("-p", "--protocol", dest="protocol",
choices=core.list_protocols(), help="Protocol",
required=True)
parser.add_argument("-m", "--message", dest="message", help="Message",
required=True)
args = parser.parse_args()


username = args.username
protocol = args.protocol
account_pattern = "-".join([username, protocol])
message = args.message

print "Trying to send message '%s'\nas user: %s, protocol: %s" % (message,
username,
protocol)

if account_pattern in core.list_accounts():
core.update_status(account_pattern, message)
else:
#If account is not already registered in libturpial, acces must be granted:
a = Account.new('twitter') #you can also create identi.ca accounts
url = a.request_oauth_access()
print "Please go to the following URL, log-in and allow access for Libturpial. Then write the PIN in here."
print url
cod = raw_input("PIN:")
a.authorize_oauth_access(cod)
c.register_account(a)
# If account is not already registered in libturpial,
# access must be granted:
account = Account.new(protocol)
url = account.request_oauth_access()
instructions = "Please go to the following URL, log-in and allow access" \
" for libturpial. Then write the PIN in here."
print "%s\n%s" % (instructions, url)
cod = raw_input("PIN: ")
account.authorize_oauth_access(cod)
account_id = core.register_account(account)
core.update_status(account_id, message)

0 comments on commit 45feaf2

Please sign in to comment.