twitools/setup.py

123 lines
4.8 KiB
Python
Raw Normal View History

2017-02-28 10:11:38 +00:00
#!/usr/bin/env python3
2017-03-07 13:17:21 +00:00
import configparser, os.path, tweepy, dbtools, getpass, shutil
2017-02-28 10:11:38 +00:00
os.makedirs("media", exist_ok=True)
2017-03-20 16:56:15 +00:00
os.makedirs("tmp", exist_ok=True)
2017-02-28 10:11:38 +00:00
2017-03-07 13:17:21 +00:00
if not os.path.isfile("filters/filler.py"):
shutil.copyfile("filters/filler.py.dist", "filters/filler.py")
2017-03-08 01:43:00 +00:00
if not os.path.isfile("filters/markov.py"):
shutil.copyfile("filters/markov.py.dist", "filters/markov.py")
2017-02-28 10:11:38 +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')
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.
''')
dbtype = input("Database type: %i (file), %i (MySQL) [%i]: " % (dbtools.SQLITE, dbtools.MYSQL, dbtools.SQLITE))
print()
try:
dbtype = int(dbtype)
except:
pass
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()
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)
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));")
db.executeQuery("CREATE TABLE messages(`id` INTEGER NOT NULL, `text` TEXT, `sender_id` INTEGER, `recipient_id` INTEGER, `created_at` TEXT, PRIMARY KEY(id));")
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));")
db.executeQuery("CREATE TABLE names(`id` TEXT NOT NULL, `name` TEXT NOT NULL, `since` INTEGER NOT NULL, `until` INTEGER, PRIMARY KEY(id, until));")
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');")
2017-03-23 16:01:05 +00:00
db.executeQuery("CREATE TABLE tokens(`cid` INT PRIMARY KEY, `ato` TEXT, `ase` TEXT, `tweet` BOOLEAN DEFAULT 1, `fish` INT DEFAULT 0, `mentions` BOOLEAN DEFAULT 0);")
db.executeQuery("CREATE TABLE timelines(`cid` INT, `nr` INT, `tid` INT, PRIMARY KEY(cid, nr));")
2017-02-28 10:11:38 +00:00
db.commit()
db.closeConnection()
config.add_section("Twitter")
print('''We are going to need a consumer key and consumer secret for accessing Twitter.
If you don't have this yet, go to apps.twitter.com and create a new application. This
application will need read/write access as well as access to direct messages.''')
2017-02-08 12:17:14 +00:00
cke = input("Your Twitter application's consumer key: ")
cse = input("Your Twitter application's consumer secret: ")
2017-02-28 10:11:38 +00:00
config.set("Twitter", "cke", cke)
config.set("Twitter", "cse", cse)
try:
auth = tweepy.OAuthHandler(cke, cse)
except tweepy.TweepError:
print("Failed to authenticate with Twitter. Please check your consumer key and secret.")
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!")