Compare commits
No commits in common. "40a9e5d0108097e2a87511b70966e958a7b3ee6c" and "d7e74db70371ac693a361ee3491dd69b3787fb08" have entirely different histories.
40a9e5d010
...
d7e74db703
2 changed files with 10 additions and 55 deletions
62
main.py
62
main.py
|
@ -1,9 +1,8 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from mastodon import Mastodon
|
from mastodon import Mastodon
|
||||||
import twitter
|
import twitter
|
||||||
import requests
|
|
||||||
|
|
||||||
import sqlite3, json, re, random
|
import sqlite3, json, re
|
||||||
|
|
||||||
cfg = {
|
cfg = {
|
||||||
"cw":None,
|
"cw":None,
|
||||||
|
@ -17,7 +16,7 @@ try:
|
||||||
except:
|
except:
|
||||||
print("No config.json, using default configuration")
|
print("No config.json, using default configuration")
|
||||||
|
|
||||||
scopes = ["read:accounts", "write:statuses", "write:media"]
|
scopes = ["read:accounts", "write:statuses"]
|
||||||
|
|
||||||
if "client" not in cfg:
|
if "client" not in cfg:
|
||||||
print("No application info -- registering application with {}".format(cfg['site']))
|
print("No application info -- registering application with {}".format(cfg['site']))
|
||||||
|
@ -63,26 +62,18 @@ if "accounts" not in cfg:
|
||||||
accounts = []
|
accounts = []
|
||||||
while i != "":
|
while i != "":
|
||||||
i = input("User: ")
|
i = input("User: ")
|
||||||
if not re.match("@(\w){1,16}$", i):
|
if not re.match("@(\w){1,15}$", i):
|
||||||
if i == "":
|
|
||||||
print("Accounts to learn from: {}".format(", ".join(accounts)))
|
|
||||||
break
|
|
||||||
print("Invalid username")
|
print("Invalid username")
|
||||||
continue
|
continue
|
||||||
if i == "" and len(accounts) == 0:
|
if i == "" and len(accounts) == 0:
|
||||||
print("You must enter at least one account")
|
print("You must enter at least one account")
|
||||||
continue
|
continue
|
||||||
print("Checking account...")
|
print("Checking account...")
|
||||||
try:
|
print(api.GetUser(screen_name=i))
|
||||||
user = api.GetUser(screen_name=i)
|
|
||||||
accounts.append(i)
|
|
||||||
except:
|
|
||||||
print("Invalid username.")
|
|
||||||
|
|
||||||
print("Saving account list. To choose a different set of accounts, delete the 'accounts' entry in config.json.")
|
|
||||||
cfg['accounts'] = accounts
|
|
||||||
|
|
||||||
json.dump(cfg, open("config.json", "w+"))
|
|
||||||
|
|
||||||
# connect to database
|
# connect to database
|
||||||
|
|
||||||
|
@ -90,33 +81,11 @@ db = sqlite3.connect("tip.db")
|
||||||
db.text_factory=str
|
db.text_factory=str
|
||||||
c = db.cursor()
|
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_urls VARCHAR, count INT DEFAULT 0) WITHOUT ROWID")
|
c.execute("CREATE TABLE IF NOT EXISTS `images` (post_id INT NOT NULL UNIQUE PRIMARY KEY, user_id INT NOT NULL, image_url VARCHAR) WITHOUT ROWID")
|
||||||
db.commit()
|
db.commit()
|
||||||
|
last_tweet = c.execute("SELECT post_id FROM `images` WHERE user_id LIKE ? ORDER BY post_id DESC LIMIT 1", (f.id,)).fetchone()
|
||||||
|
|
||||||
for acct in cfg['accounts']:
|
print("Downloading tweets from {}".format())
|
||||||
last_tweet = c.execute("SELECT post_id FROM `images` WHERE screen_name 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))
|
|
||||||
|
|
||||||
while True:
|
|
||||||
|
|
||||||
tl = api.GetUserTimeline(screen_name = acct, since_id = last_tweet, exclude_replies = True, include_rts = False, count = 200, trim_user = True)
|
|
||||||
if len(tl) == 0:
|
|
||||||
# we've reached the end of this user's timeline
|
|
||||||
break
|
|
||||||
for tweet in tl:
|
|
||||||
media_urls = []
|
|
||||||
if tweet.media != None:
|
|
||||||
for media in tweet.media:
|
|
||||||
media_urls.append(media.media_url)
|
|
||||||
c.execute("INSERT INTO `images` (screen_name, post_id, image_urls) VALUES (?, ?, ?)", (acct, tweet.id_str, ",".join(media_urls)))
|
|
||||||
|
|
||||||
last_tweet = c.execute("SELECT post_id FROM `images` WHERE screen_name LIKE ? ORDER BY post_id DESC LIMIT 1", (acct,)).fetchone()[0]
|
|
||||||
print(last_tweet)
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
client = Mastodon(
|
client = Mastodon(
|
||||||
client_id=cfg['client']['id'],
|
client_id=cfg['client']['id'],
|
||||||
|
@ -124,17 +93,4 @@ client = Mastodon(
|
||||||
access_token=cfg['secret'],
|
access_token=cfg['secret'],
|
||||||
api_base_url=cfg['site'])
|
api_base_url=cfg['site'])
|
||||||
|
|
||||||
base_count = c.execute("SELECT MIN(count) FROM images").fetchone()[0]
|
|
||||||
# base_count gets the lowest post count from the table.
|
|
||||||
# for example, if image A and B have already been posted once, and image C has never been posted, it'll return 0.
|
|
||||||
# this lets us filter for images that haven't been posted yet in this "generation".
|
|
||||||
|
|
||||||
chosen_tweet = c.execute("SELECT post_id, image_urls FROM images WHERE count = ? AND image_urls NOT LIKE '' ORDER BY RANDOM() LIMIT 1", (base_count,)).fetchone()
|
|
||||||
image = random.choice(chosen_tweet[1].split(","))
|
|
||||||
filename = re.match(r".*\/(.*)", image).group(1)
|
|
||||||
# save image to disk
|
|
||||||
open(filename, 'wb').write(requests.get(image).content)
|
|
||||||
# post!
|
|
||||||
media = client.media_post(filename, description="Image downloaded from Twitter account {}".format(acct))
|
|
||||||
client.status_post("Source: https://twitter.com/{}/status/{}".format(acct, chosen_tweet[0]), media_ids=media)
|
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,2 @@
|
||||||
Mastodon.py==1.4.0
|
Mastodon.py==1.3.1
|
||||||
python-twitter==3.5
|
python-twitter==3.5
|
||||||
requests==2.21.0
|
|
||||||
|
|
Reference in a new issue