-
Notifications
You must be signed in to change notification settings - Fork 2
Update xpost.py #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
handlechages
wants to merge
2
commits into
casouri:master
Choose a base branch
from
handlechages:patch-3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,13 @@ | |
| logger = logging.getLogger('xpost') | ||
|
|
||
| ### Mastodon | ||
| def is_valid_url(url): | ||
| """Check if a URL is valid.""" | ||
| try: | ||
| result = requests.utils.urlparse(url) | ||
| return all([result.scheme, result.netloc]) | ||
| except ValueError: | ||
| return False | ||
|
|
||
| def access_token(): | ||
| """Return the access token for mastodon app.""" | ||
|
|
@@ -85,7 +92,7 @@ def collect_media_url(post, recursive=False): | |
| return url_list | ||
|
|
||
|
|
||
| def upload_media(url_list, max_attatchment): | ||
| def upload_media(url_list, max_attatchment, mast): | ||
| """Upload media in URL_LIST. | ||
| URL_LIST should be a list of MEDIA_URL. Return (TOOT_LIST, TOO_LARGE, | ||
| TOO_MANY). TOOT_LIST is a list of TOOT_DICT. | ||
|
|
@@ -98,6 +105,9 @@ def upload_media(url_list, max_attatchment): | |
| media_too_many = True | ||
| for media_url in url_list: | ||
| url = media_url['url'] | ||
| if not is_valid_url(url): | ||
| logger.warning(f'Invalid URL: {url}') | ||
| continue | ||
| resp = requests.get(url) | ||
| mime = resp.headers['content-type'].split(';')[0].strip() | ||
| # https://mastodonpy.readthedocs.io/en/stable/#media-post | ||
|
|
@@ -110,13 +120,12 @@ def upload_media(url_list, max_attatchment): | |
| logger.warning(f'Problem uploading media, type: {mime}, url: {url}, error: {err}') | ||
| return (media_list, media_too_large, media_too_many) | ||
|
|
||
|
|
||
| def cross_post(post, mast_dict, config, db, fallback_mast=None): | ||
| """Cross-post POST to mastodon. | ||
| MAST_DICT is a hash map from weibo author ids (string) to Mastodon | ||
| instances. Return a list of POST_RECORD. FALLBACK_MAST is used when we | ||
| cannot find a Mastodon instance from dict for the weibo author. | ||
| """ | ||
| MAST_DICT is a hash map from weibo author ids (string) to Mastodon | ||
| instances. Return a list of POST_RECORD. FALLBACK_MAST is used when we | ||
| cannot find a Mastodon instance from dict for the weibo author. | ||
| """ | ||
| if not should_cross_post(post, config, db): | ||
| return [] | ||
|
|
||
|
|
@@ -133,22 +142,21 @@ def cross_post(post, mast_dict, config, db, fallback_mast=None): | |
| post_record_list = [] | ||
| orig_toot_id = None | ||
|
|
||
| # Maybe upload media. | ||
| # Maybe upload media. instance to toot with') | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A typo, it seems? |
||
| url_list = collect_media_url(post, not standalone_repost) | ||
| media_list = None | ||
| media_too_large = False | ||
| media_too_many = False | ||
| if not external_media: | ||
| media_list, media_too_large, media_too_many = \ | ||
| upload_media(url_list, max_attatchment) | ||
|
|
||
| # Come up with a Mastodon instance for tooting. | ||
| mast = mast_dict.get(user_id) | ||
| if mast == None: | ||
| if fallback_mast != None: | ||
| if mast is None: | ||
| if fallback_mast is not None: | ||
| mast = fallback_mast | ||
| else: | ||
| raise KeyError('Couldn\'t find a Mastodon instance to toot with') | ||
| if not external_media: | ||
| media_list, media_too_large, media_too_many = \ | ||
| upload_media(url_list, max_attatchment, mast) | ||
|
|
||
| # Compose toot. | ||
| # 1. Compose body text. | ||
|
|
@@ -221,6 +229,7 @@ def cross_post(post, mast_dict, config, db, fallback_mast=None): | |
| media_ids=media_list) | ||
| post_record_list.append(make_post_record(post, toot)) | ||
| return post_record_list | ||
|
|
||
|
|
||
|
|
||
| def delete_all_toots(mast): | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this intended?