109 lines
3.1 KiB
Python
Executable File
109 lines
3.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
from mastodon import Mastodon
|
|
import twitter
|
|
|
|
import sqlite3, json, re
|
|
|
|
cfg = {
|
|
"cw":None,
|
|
"mark_sensitive":True,
|
|
"site":"https://botsin.space"
|
|
}
|
|
try:
|
|
j = json.load(open("config.json"))
|
|
for key, value in j.items():
|
|
cfg[key] = value
|
|
except:
|
|
print("No config.json, using default configuration")
|
|
|
|
scopes = ["read:accounts", "write:statuses"]
|
|
|
|
if "client" not in cfg:
|
|
print("No application info -- registering application with {}".format(cfg['site']))
|
|
client_id, client_secret = Mastodon.create_app("Twitter Image Poster",
|
|
api_base_url=cfg['site'],
|
|
scopes=scopes,
|
|
website="https://git.lynnesbian.space/lynnesbian/Twitter_Image_Poster")
|
|
|
|
cfg['client'] = {
|
|
"id": client_id,
|
|
"secret": client_secret
|
|
}
|
|
|
|
if "secret" not in cfg:
|
|
print("No user credentials -- logging in to {}".format(cfg['site']))
|
|
client = Mastodon(client_id = cfg['client']['id'],
|
|
client_secret = cfg['client']['secret'],
|
|
api_base_url=cfg['site'])
|
|
|
|
print("Open this URL and authenticate to give Twitter Image Poster access to your bot's account: {}".format(client.auth_request_url(scopes=scopes)))
|
|
cfg['secret'] = client.log_in(code=input("Secret: "), scopes=scopes)
|
|
|
|
if "twitter" not in cfg:
|
|
print("No Twitter credentials")
|
|
print("Please create a Twitter app by using this page: https://developer.twitter.com/en/apps/create")
|
|
cfg['twitter'] = {
|
|
"consumer_key": None,
|
|
"consumer_secret": None,
|
|
"access_token_key": None,
|
|
"access_token_secret": None
|
|
}
|
|
for i in cfg['twitter'].keys():
|
|
cfg['twitter'][i] = input("{}: ".format(i))
|
|
|
|
json.dump(cfg, open("config.json", "w+"))
|
|
|
|
# log in to twitter
|
|
api = twitter.Api(**cfg['twitter'])
|
|
|
|
if "accounts" not in cfg:
|
|
print("Please specify the accounts you'd like Twitter Image Poster to learn from. Enter one account at a time formatted as '@user', followed by a blank line.")
|
|
i = None
|
|
accounts = []
|
|
while i != "":
|
|
i = input("User: ")
|
|
if not re.match("@(\w){1,16}$", i):
|
|
if i == "":
|
|
print("Accounts to learn from: {}".format(", ".join(accounts)))
|
|
break
|
|
print("Invalid username")
|
|
continue
|
|
if i == "" and len(accounts) == 0:
|
|
print("You must enter at least one account")
|
|
continue
|
|
print("Checking account...")
|
|
try:
|
|
user = api.GetUser(screen_name=i)
|
|
accounts.append(i)
|
|
except:
|
|
print("Invalid username.")
|
|
|
|
|
|
# connect to database
|
|
|
|
db = sqlite3.connect("tip.db")
|
|
db.text_factory=str
|
|
c = db.cursor()
|
|
|
|
c.execute("CREATE TABLE IF NOT EXISTS `images` (post_id INT NOT NULL UNIQUE PRIMARY KEY, screen_name VARCHAR NOT NULL, image_url VARCHAR) WITHOUT ROWID")
|
|
db.commit()
|
|
|
|
for acct in accounts:
|
|
last_tweet = c.execute("SELECT post_id FROM `images` WHERE user_id LIKE ? ORDER BY post_id DESC LIMIT 1", (acct,)).fetchone()
|
|
if last_tweet != None:
|
|
last_tweet = last_tweet[0]
|
|
else:
|
|
last_tweet = 0 # start from the first one
|
|
print("Downloading tweets from account {}, starting from {}".format(acct, last_tweet))
|
|
tl = api.GetUserTimeline(screen_name = acct, since_id = last_tweet, exclude_replies = True)
|
|
|
|
|
|
|
|
client = Mastodon(
|
|
client_id=cfg['client']['id'],
|
|
client_secret = cfg['client']['secret'],
|
|
access_token=cfg['secret'],
|
|
api_base_url=cfg['site'])
|
|
|
|
|