-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some improvements to send_tweet example
- [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
Showing
1 changed file
with
49 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,28 +2,60 @@ | |
# -*- coding: utf-8 -*- | ||
|
||
# Libturpial usage example | ||
# | ||
# | ||
# Author: Carlos Guerrero <[email protected]> | ||
# 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) |