From e01b61143d6d2a3be6a72b764b6d63ad8bb63e5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Umut=20Karc=C4=B1?= Date: Mon, 24 Aug 2020 14:10:20 +0300 Subject: [PATCH 1/4] 1- example `updated` timestamp replaced to epoch 2- __init__ or another file shouldn't be used as a main entry point. __main__ is the correct way. python3 -m pfxposter will work this way. 3- renamed pfxposter to poster, easier to import, easier to understand. 4- moved all stuff into functions, this is cleaner, hope for the cleanest code for all. 5- pip3 is not the best way, python3 -m pip is better due to pip3 may be in a different PATH than your virtual environment. 6- requests must be in requirements for development and setup.py for installation. Excluding it to not break the OS is just a bad workaround. 7- textwrap.dedent and triple quotes helps with readability 8- when using regular expressions, you should use raw strings (strings that ignores escape sequences like `\s`) 9- os._exit(0) is an immediate exit, sys.exit(0) will also exit the software but after flushing any open buffers and running any atexit callbacks. 10- breaking long chain calls is more readable --- README.md | 4 +-- data/config.toml | 2 +- pfxposter/__init__.py | 6 +--- pfxposter/__main__.py | 5 +++ pfxposter/pfxposter.py | 55 ---------------------------- pfxposter/poster.py | 81 ++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + setup.py | 4 +-- 8 files changed, 93 insertions(+), 65 deletions(-) create mode 100644 pfxposter/__main__.py delete mode 100644 pfxposter/pfxposter.py create mode 100644 pfxposter/poster.py diff --git a/README.md b/README.md index 83e1b46..ab667f7 100644 --- a/README.md +++ b/README.md @@ -13,10 +13,10 @@ A PixelFed to Mastodon and Twitter crossposter that is not written as intended. In order to post PixelFed entries to Twitter, you will need a Mastodon-Twitter crossposter, you may find them on web. ## Installation - ``` -pip3 install pfxposter +python3 -m pip instal pfxposter ``` +Use of virtual enviroments is strongly recommended, requests package may override your OS vendored version. ## Usage diff --git a/data/config.toml b/data/config.toml index 440dc94..c4463e9 100644 --- a/data/config.toml +++ b/data/config.toml @@ -1,4 +1,4 @@ -updated = 2020-08-24 00:00:00+00:00 +updated = 1970-01-01T00:00:00+00:00 [mastodon] mastodon_url = "https://oyd.social" diff --git a/pfxposter/__init__.py b/pfxposter/__init__.py index a44685a..ea311cc 100644 --- a/pfxposter/__init__.py +++ b/pfxposter/__init__.py @@ -1,7 +1,3 @@ -#!/usr/bin/env python3 - -from .pfxposter import * +from .poster import * __version__ = '0.1.2' - - diff --git a/pfxposter/__main__.py b/pfxposter/__main__.py new file mode 100644 index 0000000..4dc0952 --- /dev/null +++ b/pfxposter/__main__.py @@ -0,0 +1,5 @@ +#!/usr/bin/env python3 +from . import poster + +if __name__ == "__main__": + poster.main() diff --git a/pfxposter/pfxposter.py b/pfxposter/pfxposter.py deleted file mode 100644 index a81dac7..0000000 --- a/pfxposter/pfxposter.py +++ /dev/null @@ -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("(?Phttps?://[^\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() diff --git a/pfxposter/poster.py b/pfxposter/poster.py new file mode 100644 index 0000000..cd6bc76 --- /dev/null +++ b/pfxposter/poster.py @@ -0,0 +1,81 @@ +import os +import re +import sys +import tempfile +import textwrap + +import atoma +import requests +import toml +from mastodon import Mastodon + + +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"(?Phttps?://[^\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(): + homedir = os.getenv("HOME") + config_file = "{}/.pfxposter".format(homedir) + + 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) + + + diff --git a/requirements.txt b/requirements.txt index 6044b60..645ce9f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ Mastodon.py>=1.5.1 atoma>=0.0.17 toml>=0.10.1 python-crontab>=2.5.1 +requests \ No newline at end of file diff --git a/setup.py b/setup.py index 2a8f418..0c9009f 100755 --- a/setup.py +++ b/setup.py @@ -45,10 +45,10 @@ setup( "Programming Language :: Python :: Implementation :: PyPy", "Topic :: Internet", ], - entry_points={"console_scripts": ["pfxposter = pfxposter.pfxposter:main"]}, + entry_points={"console_scripts": ["pfxposter = pfxposter.poster:main"]}, data_files=[('/home/{}/.pfxposter'.format(os.getenv("USER")), ['data/config.toml'])], python_requires=">=3.6", - install_requires=['Mastodon.py>=1.5.1', 'atoma>=0.0.17', 'toml>=0.10.1', 'python-crontab>=2.5.1'] + install_requires=['Mastodon.py>=1.5.1', 'atoma>=0.0.17', 'toml>=0.10.1', 'python-crontab>=2.5.1', 'requests'] ) _post_install() From d9ea75ddc029975e6374372b3f26e5d6b5618230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zcan=20O=C4=9Fuz?= Date: Mon, 24 Aug 2020 14:59:58 +0300 Subject: [PATCH 2/4] Typo fix --- README.md | 4 ++-- pfxposter/__init__.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 84f5d20..6902917 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,9 @@ In order to post PixelFed entries to Twitter, you will need a Mastodon-Twitter c ## Installation ``` -python3 -m pip instal pfxposter +python3 -m pip install pfxposter ``` -Use of virtual enviroments is strongly recommended, requests package may override your OS vendored version. +Using virtual enviroment is strongly recommended, for the reason that the requests library of the distro vendor may be overrided after the installation. ## Usage diff --git a/pfxposter/__init__.py b/pfxposter/__init__.py index ea311cc..c7f62d2 100644 --- a/pfxposter/__init__.py +++ b/pfxposter/__init__.py @@ -1,3 +1,3 @@ from .poster import * -__version__ = '0.1.2' +__version__ = '0.1.4' From 27533359f2c39ddd7fa60968c5afb50f67b6b329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zcan=20O=C4=9Fuz?= Date: Mon, 24 Aug 2020 15:14:00 +0300 Subject: [PATCH 3/4] RC1: everything tested --- pfxposter/poster.py | 5 ++--- setup.py | 5 +---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/pfxposter/poster.py b/pfxposter/poster.py index cd6bc76..7e2f7b7 100644 --- a/pfxposter/poster.py +++ b/pfxposter/poster.py @@ -9,6 +9,8 @@ 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(""" @@ -60,9 +62,6 @@ def syncronize(config_file, config, username, last_updated, mastodon): def main(): - homedir = os.getenv("HOME") - config_file = "{}/.pfxposter".format(homedir) - if not os.path.exists(config_file): create_config(config_file) diff --git a/setup.py b/setup.py index 0c9009f..aa9ef30 100755 --- a/setup.py +++ b/setup.py @@ -10,13 +10,10 @@ with open("README.md", "r") as fh: long_description = fh.read() def _post_install(): - conf_includes = 'updated = 2020-08-24 00:00:00+00:00\n\n[mastodon]\n mastodon_url = "https://oyd.social"\naccess_token = "your_access_token_here"\n\n[pixelfed]\npixelfed_url = "https://pixelfed.social"\nusername = "username"' cron_job = CronTab(user=os.getenv("USER")) autochecker = cron_job.new(command='pfxposter') autochecker.minute.every(1) cron_job.write() - with open("{}/.pfxposter".format(os.getenv("HOME")), "w") as conf: - conf.write(conf_includes) setup( name='pfxposter', @@ -46,7 +43,7 @@ setup( "Topic :: Internet", ], entry_points={"console_scripts": ["pfxposter = pfxposter.poster:main"]}, - data_files=[('/home/{}/.pfxposter'.format(os.getenv("USER")), ['data/config.toml'])], + #data_files=[('/home/{}/.pfxposter'.format(os.getenv("USER")), ['data/config.toml'])], python_requires=">=3.6", install_requires=['Mastodon.py>=1.5.1', 'atoma>=0.0.17', 'toml>=0.10.1', 'python-crontab>=2.5.1', 'requests'] ) From 465c2c7584d27262faacf48e3833785396670ba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zcan=20O=C4=9Fuz?= Date: Mon, 24 Aug 2020 15:15:27 +0300 Subject: [PATCH 4/4] RC2: 0.1.6 --- pfxposter/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pfxposter/__init__.py b/pfxposter/__init__.py index c7f62d2..2319cb5 100644 --- a/pfxposter/__init__.py +++ b/pfxposter/__init__.py @@ -1,3 +1,3 @@ from .poster import * -__version__ = '0.1.4' +__version__ = '0.1.6'