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

Add User-Agent header to image requests #203

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 10 additions & 6 deletions lib/plugins/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
def compress_image_to_limit(image_url):
if not isinstance(image_url, str) or not image_url.startswith(("http", "https")):
return None
response = requests.get(image_url)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
response = requests.get(image_url, headers=headers)
if response.status_code != 200 and not response.headers.get(
"Content-Type", ""
).startswith("image/"):
Expand Down Expand Up @@ -265,12 +268,13 @@ def create_post(self, content, **kwargs) -> Tuple[bool, Optional[str]]:
if img_data
else None
)
embed_images.append(
atproto.models.AppBskyEmbedImages.Image(
alt=image["alt_text"] if "alt_text" in image else "",
image=upload,
if upload:
embed_images.append(
atproto.models.AppBskyEmbedImages.Image(
alt=image["alt_text"] if "alt_text" in image else "",
image=upload,
)
)
)
embed = (
atproto.models.AppBskyEmbedImages.Main(images=embed_images)
if embed_images
Expand Down
7 changes: 4 additions & 3 deletions lib/plugins/linkedin.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ def linkedin_post(self, content, images):
}
if images:
data["content"] = self.linkedin_upload_images(images)
if not data["content"]:
return None
headers = self.headers
headers["Content-Type"] = "application/json"
response = requests.post(
Expand All @@ -149,7 +147,10 @@ def linkedin_upload_images(self, images):
value = response.json().get("value")
upload_url = value["uploadUrl"]
filename = os.path.basename(image["url"])
with requests.get(image["url"], stream=True) as r:
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
with requests.get(image["url"], stream=True, headers=headers) as r:
r.raise_for_status()
with open(filename, "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
Expand Down
5 changes: 4 additions & 1 deletion lib/plugins/mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def format_content(self, content, mentions, hashtags, images, **kwargs):
def create_post(self, content, **kwargs):
media_ids = []
for image in content["images"]:
response = requests.get(image["url"])
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
response = requests.get(image["url"], headers=headers)
if response.status_code == 200 and response.headers.get(
"Content-Type", ""
).startswith("image/"):
Expand Down
5 changes: 4 additions & 1 deletion lib/plugins/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ async def async_create_post(self, content):
posts = []
for msg in content:
if msg["msgtype"] == "m.image":
response = requests.get(msg["url"])
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
response = requests.get(msg["url"], headers=headers)
if response.status_code != 200:
continue
temp = tempfile.NamedTemporaryFile()
Expand Down
6 changes: 4 additions & 2 deletions lib/plugins/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ def upload_images(self, images):
uploaded_files = []
for image in images:
filename = image["url"].split("/")[-1]

with requests.get(image["url"]) as response:
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
with requests.get(image["url"], headers=headers) as response:
if response.status_code != 200 or not response.headers.get(
"Content-Type", ""
).startswith("image/"):
Expand Down