Skip to content
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

Bump deps, fix reddit galleries #22

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions bot/integrations/reddit/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import datetime
import glob
import io
Expand Down Expand Up @@ -107,13 +108,7 @@ async def _hydrate_post(self, post: domain.Post) -> bool:
if not self.client:
return self._hydrate_post_no_login(post)

try:
submission: asyncpraw.models.Submission = await self.client.submission(url=post.url)
except praw_exceptions.InvalidURL:
# Hack for new reddit urls generated in mobile app
# Does another request, which redirects to the correct url
post.url = requests.get(post.url, timeout=base.DEFAULT_TIMEOUT).url.split('?')[0]
submission = await self.client.submission(url=post.url)
submission: asyncpraw.models.Submission = await self.client.submission(url=post.url)

content = ''
if submission.selftext:
Expand All @@ -135,11 +130,21 @@ async def _hydrate_post(self, post: domain.Post) -> bool:
post.buffer = io.BytesIO(f.read())
os.remove(f'/tmp/{submission.id}.mp4')
elif submission.url.startswith('https://www.reddit.com/gallery/'):
post.buffer = self._download_and_merge_gallery(url=submission.url)
image_urls = []
for media_id in [item['media_id'] for item in submission.gallery_data['items']]:
image_urls.append(
submission.media_metadata[media_id]['p'][0]['u'].split('?')[0].replace('preview', 'i')
)
post.buffer = utils.combine_images(
await asyncio.gather(*[self._download(url=image_url) for image_url in image_urls[:3]])
)

return True

def _hydrate_post_no_login(self, post: domain.Post) -> bool:
if self._is_mobile_url(url=post.url):
post.url = requests.get(post.url, timeout=base.DEFAULT_TIMEOUT).url.split('?')[0]

try:
media = self.downloader.Download(url=post.url, quality=720, destination='/tmp/', output=str(uuid.uuid4()))
except Exception as e:
Expand Down Expand Up @@ -202,3 +207,7 @@ def _is_nsfw(url: str) -> bool:
def _calculate_votes(upvotes: int, ratio: float) -> typing.Tuple[int, int]:
downvotes = (upvotes / ratio) - upvotes
return (upvotes, int(downvotes))

@staticmethod
def _is_mobile_url(url: str) -> bool:
return bool(re.match(NEW_REDDIT_URL_PATTERN, url))
48 changes: 24 additions & 24 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ redvid = "^2.0.5"
RedDownloader = "^4.3.0"
ffmpeg-python = "^0.2.0"
opencv-python = "^4.10.0.84"
asyncpraw = "^7.7.1"
twscrape = "^0.13"
lxml-html-clean = "^0.1.1"
Django = "^5.1.1"
asyncpraw = "^7.8.0"
twscrape = "^0.14"
lxml-html-clean = "^0.3.1"
Django = "^5.1.2"
djangorestframework = "^3.15.2"
djangorestframework-simplejwt = "^5.3.1"
django-structlog = "^8.1.0"
Expand All @@ -34,11 +34,11 @@ aiograpi = "^0.0.3"
pytubefix = "^8.1.1"

[tool.poetry.dev-dependencies]
black = "^24.8.0"
black = "^24.10.0"
flake8 = "^7.1.1"
flake8-import-order = "^0.18.2"
pylint = "^3.3.1"
pylint-django = "^2.5.5"
pylint-django = "^2.6.1"
isort = "^5.13.2"

[tool.black]
Expand Down
Loading