mirror of https://github.com/ooguz/pfxposter
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 94 additions and 70 deletions
-
4README.md
-
2data/config.toml
-
8pfxposter/__init__.py
-
5pfxposter/__main__.py
-
55pfxposter/pfxposter.py
-
80pfxposter/poster.py
-
1requirements.txt
-
9setup.py
@ -1,7 +1,3 @@ |
|||
#!/usr/bin/env python3 |
|||
|
|||
from .pfxposter import * |
|||
|
|||
__version__ = '0.1.2' |
|||
|
|||
from .poster import * |
|||
|
|||
__version__ = '0.1.6' |
@ -0,0 +1,5 @@ |
|||
#!/usr/bin/env python3 |
|||
from . import poster |
|||
|
|||
if __name__ == "__main__": |
|||
poster.main() |
@ -1,55 +0,0 @@ |
|||
#!/usr/bin/env python3 |
|||
|
|||
from mastodon import Mastodon |
|||
import requests |
|||
import atoma |
|||
import re |
|||
import tempfile |
|||
import toml |
|||
import os |
|||
|
|||
homedir = os.getenv("HOME") |
|||
config_file = "{}/.pfxposter".format(homedir) |
|||
|
|||
if os.path.exists(config_file) == False: |
|||
conf_includes = 'updated = 2020-08-24 00:00:00+00:00\n\n[mastodon]\nmastodon_url = "https://oyd.social"\naccess_token = "your_access_token_here"\n\n[pixelfed]\npixelfed_url = "https://pixelfed.social"\nusername = "username"' |
|||
with open(config_file, "w") as f: |
|||
f.write(conf_includes) |
|||
print("Configuration file created please edit") |
|||
os._exit(0) |
|||
|
|||
config = toml.load(config_file) |
|||
|
|||
username = config['pixelfed']['username'] |
|||
last_updated = config['updated'] |
|||
|
|||
mastodon = Mastodon( |
|||
access_token = config['mastodon']['access_token'], |
|||
api_base_url = config['mastodon']['mastodon_url'] |
|||
) |
|||
|
|||
def main(): |
|||
|
|||
pixelfeed_get = requests.get('https://pixelfed.social/users/{}.atom'.format(username)) |
|||
pixelfeed = atoma.parse_atom_bytes(pixelfeed_get.content) |
|||
|
|||
latest_post = pixelfeed.entries[0] |
|||
last_updated_atom = latest_post.updated |
|||
if last_updated == last_updated_atom: |
|||
print("Up-to-date") |
|||
os._exit(0) |
|||
config['updated'] = last_updated_atom |
|||
with open(config_file, "w") as f: |
|||
toml.dump(config, f) |
|||
print("Config file updated") |
|||
image_name = latest_post.title.value |
|||
image_url = re.search("(?P<url>https?://[^\s]+)", latest_post.summary.value).group("url").rstrip('">').replace("_thumb", "") |
|||
tmp = tempfile.NamedTemporaryFile(suffix=".jpg") |
|||
get_image = requests.get(image_url) |
|||
tmp.write(get_image.content) |
|||
mastodon_media = mastodon.media_post(tmp.name) |
|||
mastodon.status_post(image_name, media_ids=mastodon_media['id']) |
|||
print("Status posted: ", image_name) |
|||
tmp.close() |
|||
if __name__ == "__main__": |
|||
main() |
@ -0,0 +1,80 @@ |
|||
import os |
|||
import re |
|||
import sys |
|||
import tempfile |
|||
import textwrap |
|||
|
|||
import atoma |
|||
import requests |
|||
import toml |
|||
from mastodon import Mastodon |
|||
|
|||
homedir = os.getenv("HOME") |
|||
config_file = "{}/.pfxposter".format(homedir) |
|||
|
|||
def create_config(config_file): |
|||
conf_includes = textwrap.dedent(""" |
|||
updated = 2020-08-24 00:00:00+00:00 |
|||
|
|||
[mastodon] |
|||
mastodon_url = "https://oyd.social" |
|||
access_token = "your_access_token_here" |
|||
|
|||
[pixelfed] |
|||
pixelfed_url = "https://pixelfed.social" |
|||
username = "username" |
|||
""") |
|||
with open(config_file, "w") as f: |
|||
f.write(conf_includes) |
|||
print("Configuration file created please edit") |
|||
sys.exit(0) |
|||
|
|||
|
|||
def syncronize(config_file, config, username, last_updated, mastodon): |
|||
pixelfeed_get = requests.get('https://pixelfed.social/users/{}.atom'.format(username)) |
|||
pixelfeed = atoma.parse_atom_bytes(pixelfeed_get.content) |
|||
|
|||
latest_post = pixelfeed.entries[0] |
|||
last_updated_atom = latest_post.updated |
|||
|
|||
if last_updated == last_updated_atom: |
|||
print("Up-to-date") |
|||
sys.exit(0) |
|||
|
|||
config['updated'] = last_updated_atom |
|||
|
|||
with open(config_file, "w") as f: |
|||
toml.dump(config, f) |
|||
print("Config file updated") |
|||
|
|||
image_name = latest_post.title.value |
|||
image_url = re.search(r"(?P<url>https?://[^\s]+)", latest_post.summary.value) \ |
|||
.group("url") \ |
|||
.rstrip('">') \ |
|||
.replace("_thumb", "") |
|||
tmp = tempfile.NamedTemporaryFile(suffix=".jpg") |
|||
get_image = requests.get(image_url) |
|||
tmp.write(get_image.content) |
|||
mastodon_media = mastodon.media_post(tmp.name) |
|||
mastodon.status_post(image_name, media_ids=mastodon_media['id']) |
|||
print("Status posted: ", image_name) |
|||
tmp.close() |
|||
|
|||
|
|||
def main(): |
|||
if not os.path.exists(config_file): |
|||
create_config(config_file) |
|||
|
|||
config = toml.load(config_file) |
|||
|
|||
username = config['pixelfed']['username'] |
|||
last_updated = config['updated'] |
|||
|
|||
mastodon = Mastodon( |
|||
access_token=config['mastodon']['access_token'], |
|||
api_base_url=config['mastodon']['mastodon_url'] |
|||
) |
|||
syncronize(config_file, config, username, last_updated, mastodon) |
|||
|
|||
|
|||
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue