2016-02-08 00:55:12 +00:00
#!/usr/bin/env python3
2016-05-30 21:55:59 +00:00
import configparser , os . path , tweepy , dbtools , getpass
2016-02-08 00:55:12 +00:00
if os . path . isfile ( " config.cfg " ) :
print ( " config.cfg already exists. Please remove it before running this script. " )
exit ( 1 )
config = configparser . RawConfigParser ( )
config . add_section ( ' Database ' )
2016-05-30 21:55:59 +00:00
print ( ''' Twitools will use a database for certain tasks. You can use a file or a MySQL database for this purpose.
If you wish to use a MySQL database , you will need the credentials for it . If you don ' t know what any of that means, stick with the default value and just press Enter.
2016-02-08 00:55:12 +00:00
''' )
2016-05-30 21:55:59 +00:00
dbtype = input ( " Database type: %i (file), %i (MySQL) [ %i ]: " % ( dbtools . SQLITE , dbtools . MYSQL , dbtools . SQLITE ) )
2016-02-08 00:55:12 +00:00
print ( )
2016-05-30 21:55:59 +00:00
try :
dbtype = int ( dbtype )
except :
2016-02-08 00:55:12 +00:00
pass
2016-05-30 21:55:59 +00:00
if dbtype == dbtools . MYSQL :
dbhost = input ( " MySQL host [localhost]: " ) or " localhost "
dbuser = input ( " MySQL username [twitools]: " ) or " twitools "
dbpass = getpass . getpass ( " MySQL password (not echoed!): " )
dbname = input ( " MySQL database name [twitools]: " ) or " twitools "
print ( )
2016-02-08 00:55:12 +00:00
2016-05-30 21:55:59 +00:00
config . set ( ' Database ' , ' type ' , dbtype )
config . set ( ' Database ' , ' host ' , dbhost )
config . set ( ' Database ' , ' user ' , dbuser )
config . set ( ' Database ' , ' pass ' , dbpass )
config . set ( ' Database ' , ' name ' , dbname )
2016-02-08 00:55:12 +00:00
2016-05-30 21:55:59 +00:00
else :
dbtype = dbtools . SQLITE
dbpath = input ( " Name of the database file [Database.db]: " ) or " Database.db "
print ( )
config . set ( ' Database ' , ' type ' , dbtype )
config . set ( ' Database ' , ' path ' , dbpath )
if dbtype == dbtools . MYSQL :
db = dbtools . dbObject ( dbtype = dbtype , host = dbhost , user = dbuser , pwd = dbpass , db = dbname )
else :
db = dbtools . dbObject ( dbtype = dbtype , path = dbpath )
if not db . isInitialized ( ) :
db . executeQuery ( " CREATE TABLE tweets(`tweet_id` INTEGER NOT NULL, `in_reply_to_status_id` TEXT, `in_reply_to_user_id` TEXT, `timestamp` TEXT, `source` TEXT, `text` TEXT, `retweeted_status_id` TEXT, `retweeted_status_user_id` TEXT, `retweeted_status_timestamp` TEXT, `expanded_urls` TEXT, PRIMARY KEY(tweet_id)); " )
2016-06-30 10:33:09 +00:00
db . executeQuery ( " CREATE TABLE messages(`id` INTEGER NOT NULL, `text` TEXT, `sender_id` INTEGER, `recipient_id` INTEGER, `created_at` TEXT, PRIMARY KEY(id)); " )
2016-08-04 16:17:42 +00:00
db . executeQuery ( " CREATE TABLE followers(`id` TEXT NOT NULL, `since` INTEGER NOT NULL, `until` INTEGER, PRIMARY KEY(id, until)); " )
db . executeQuery ( " CREATE TABLE following(`id` TEXT NOT NULL, `since` INTEGER NOT NULL, `until` INTEGER, PRIMARY KEY(id, until)); " )
2017-01-27 21:20:34 +00:00
db . executeQuery ( " CREATE TABLE retweets(id INT PRIMARY KEY, author VARCHAR(30), created_at VARCHAR(30), text TEXT); " )
db . executeQuery ( " CREATE TABLE lyrics(id INTEGER PRIMARY KEY AUTOINCREMENT, text VARCHAR(140) NOT NULL, ref INT NOT NULL default ' 0 ' , tweet_id INT, active BOOLEAN default ' 0 ' ); " )
2016-12-06 21:30:18 +00:00
2016-05-30 21:55:59 +00:00
db . commit ( )
db . closeConnection ( )
2016-02-08 00:55:12 +00:00
config . add_section ( " Twitter " )
cke = " V6ekVFYtavi6IvRFLS0dHifSh "
cse = " U2duSfBtW0Z8UQFoJyARf3jU80gdQ44EEqWqC82ebuGbIPN3t7 "
config . set ( " Twitter " , " cke " , cke )
config . set ( " Twitter " , " cse " , cse )
auth = tweepy . OAuthHandler ( cke , cse )
try :
authurl = auth . get_authorization_url ( )
except tweepy . TweepError :
print ( " Error getting request token. Please try again later... " )
exit ( 1 )
print ( ''' In the next step, we ' ll get you connected to Twitter. Please follow this link,
sign on to Twitter and copy the PIN you will get there . Insert it below , then
press Enter to continue .
''' )
print ( authurl )
print ( )
pin = input ( " PIN: " )
print ( )
try :
auth . get_access_token ( pin )
except tweepy . TweepError :
print ( " Error getting access token. Please try again later... " )
exit ( 1 )
config . set ( " Twitter " , " ato " , auth . access_token )
config . set ( " Twitter " , " ase " , auth . access_token_secret )
print ( " Seems like everything worked out fine. Let ' s write that config file... " )
with open ( ' config.cfg ' , ' wt ' ) as cfg :
config . write ( cfg )
print ( " We ' re all done. You can now use Twitools. Have fun! " )