Send video from kik bot - python

I am trying to write a simple kik bot to send videos from youtube.
Started with https://github.com/kikinteractive/kik-bot-python-example
Modified it this way:
messages_to_send.append(
VideoMessage(
to=message.from_user,
chat_id=message.chat_id,
video_url="https://www.youtube.com/watch?v=WHATEVER"
))
But when try, i get an error like:
kik.error.KikError: {"message":"Error sending video message:
text/html; charset=utf-8 is not a supported
Content-Type","error":"BadRequest"}
Dont know from where is taking "text/html; charset=utf-8" because i ve defined is a VideoMessage(
Sorry if it is a silly question, i am noob with kik and python
Thanks in advance

I think the video_url parameter expects an URL that points to a video file. In the example from their docs the URL is "http://example.kik.com/video.mp4", meaning (in my opinion) that it should be a video file. In your example, "https://www.youtube.com/watch?v=WHATEVER" would point to an HTML file (i.e. not a video file).
Maybe you'll have to find (a) if YouTube provides a URL that returns a video mimetype (I bet they don't), or (b) use something as youtube-dl to download the MP4 file, uplaod it somewhere else and use this somewhere-else's URL in yourt code snipet. Or… (c) just send a text message with the YouTube URL : )
Does that make sense?

Related

how to download a video from a twitch clip in python [duplicate]

I have been trying to download past broadcasts for a streamer on twitch using python. I found this python code online:
https://gist.github.com/baderj/8340312
However, when I try to call the functions I am getting errors giving me a status 400 message.
Unsure if this is the code I want to download the video (as an mp4) or how to use it properly.
And by video I mean something like this as an example: www(dot)twitch.tv/imaqtpie/v/108909385 //note cant put more than 3 links since I don't have 10 reputation
Any tips on how i should go about doing this?
Here's an example of running it in cmd:
python twitch_past_broadcast_downloader.py 108909385
After running it, it gave me this:
Exception API returned 400
This is where i got the information on running it:
https://www.johannesbader.ch/2014/01/find-video-url-of-twitch-tv-live-streams-or-past-broadcasts/
Huh it's not as easy at it seems ... The code you found on this gist is quite old and Twitch has completely changed its API. Now you will need a Client ID to download videos, in order to limit the amount of video you're downloading.
If you want to correct this gist, here are simple steps you can do :
Register an application : Everything is explained here ! Register you app and keep closely your client id.
Change API route : It is no longer '{base}/api/videos/a{id_}' but {base}/kraken/videos/{id_} (not sure about the last one). You will need to change it inside the python code. The doc is here.
Add the client id to the url : As said in the doc, you need to give a header to the request you make, so add a Client-ID: <client_id> header in the request.
And now I think you will need to start debugging a bit, because it is old code :/
I will try myself to do it and I will edit this answer when I'm finished, but try yourself :)
See ya !
EDIT : Mhhh ... It doesn't seem to be possible anyway to download a video with the API :/ I was thinking only links to API changed, but the chunks section of the response from the video url disappeared and Twitch is no longer giving access to raw videos :/
Really sorry I told you to do that, even with the API I think is no longer possible :/
You can download past broadcasts of Twitch videos with Python library streamlink.
You will need OAuth token which you can generate with the command
streamlink --twitch-oauth-authenticate
Download VODs with:
streamlink --twitch-oauth-token <your-oauth-token> https://www.twitch.tv/videos/<VideoID> best -o <your-output-folder>

how to send image from url using telethon

I want to know how to send image or media from a URL link,
file = BOT.upload_file('/user/home/photo.jpg')
BOT.send_file(chat , file)
I know that using this method we can send image from path, but I want to know if its possible to send it from a URL link. but I am trying to run the code on Heruku so uploading it from the patch will not be possible so if there is a way to send it using a URL link please tell me how to do that.
can anyone help me figure this out please.
you don't have to explicitly upload a file, telethon does it internally, so:
BOT.send_file(chat , '/user/home/photo.jpg')
is enough (unless you're willing to resend something pre-uploaded multiple times)
likewise, you can pass a URL to send_file, Telegram servers will fetch it and send by itself (note there are limits for file size, 5MB for images, 20 MB for documents)
BOT.send_file(chat , url)

Screenshot command Discord.py rewrite

First of all, I'm a new guy here and this is my first question, so I'd like to request y'all to ignore any flaws or unexpected details in this question.
So I'm trying to make a screenshot command for my Discord.py bot and currently I'm struck with the following code fragment:
async def ss(ctx, site):
embed=discord.Embed(colour = discord.Colour.orange(), timestamp=ctx.message.created_at)
embed.set_image(url=(f"https://image.thum.io/get/width/1920/crop/675/maxAge/1/noanimate/{site}"))
await ctx.send(embed=embed)
However, the bot just sends an empty embed even for a valid URL. Currently, what seems the most obvious to me is that Discord isn't able to recognize this as a valid image as it doesn't end in a image extension like .png or .jpeg, and hence the empty embed.
TBH I don't know any alternate code for what I'm trying to achieve. I searched a lot and I think it's something to do with BytesIO but I don't have the slightest idea on how to achieve this using the module.
What I'm expecting at this point of time is two things:
Fix the current flaw so that I'm able to send the screenshot of the desired website.
Report to the message author if the website is invalid, in the sense that there's no website on the specified domain, or that the request timed out due to delayed response on the website's end.
Thus, I'd like to request the community to help me out with my goal on this command. I'm not asking to be spoon-fed, but this is the only command in my bot till now, for which I don't have the slightest idea how to fix it. I'd like to thank everyone for their considerate reply in advance.
Hearty regards,
Sayan Bhattacharyya.
I did some research and debugging on my end and found that the thing doesn't work with URLs that don't have HTTPS:// or HTTP:// in front of them. If I use a link that has them, it works fine
So now what you have to do
See if user has used http:// or https:// in the start of the link
if not site.startswith("https://", "http://"):
# site doesn't start with http:// or https://
site = "https://" + site # we add https in front of the URL
See if the link is valid
# add this at the top of your file
import re
# define this variable somewhere outside of the command
URL_REGEX = re.compile(r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_#.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+")
# then in your command, use
if not re.fullmatch(URL_REGEX, site):
# URL is invalid, send a message here
await ctx.send("Invalid URL")
return
# the `return` here stops the command from running after sending the message
Your site does not redirect to a valid image file and doesn't end with a good image extension.
So this is not a Discord problem.

telegram bot image from url - undesired cache

I am working on a telegram bot that displays images from several webcams upon request. I fetch the images from urls and then send to the user (using bot.sendPhoto() ) My problem is that for any given webcam the filename does not change and it seems that the photo is sent from telegram's cache. So it will display the image from the first time that image was requested.
I have thought about downloading the image from the url, saving with a variable name (like a name with a timestamp in it) then sending it to the chat, this seems like an inelegant solution and was hoping for something better. Like forcing the image not to be cached on the telegram server.
I am using the python-telegram-bot wrapper, but I am not sure that it's specific to that.
Any ideas? I have tried searching but so far am turning up little.
Thanks in advance.
I had the same problem too, but i've found the simplest solution.
When you call the image, you have to add a parameter with timestamp to the image link.
Example:
http://www.example.com/img/img.jpg?a=TIMESTAMP
Where TIMESTAMP is the timestamp function based on the language you are using.
Simple but tricky ;)
I think the best way is to do the same as we do in React where also, same URL calls are first checked in the cache.
If you are using Python the best way is:
timestamp = datetime.datetime.now().isoformat()
# Above statement returns like: '2013-11-18T08:18:31.809000'
pic_url = '{0}?a={1}'.format(img_url, timestamp)
Hope that helps!
I had the same problem. I wanted to create a bot which sends an image taken by a webcam of a ski slope (webcam.example.com/image.jpg). Unfortunately, the filename and so the url never updates and telegram always sends the cached image. So I decided to alter the url passed to the api. In order to achieve this, I wrote a simple php site (example.com/photo.php) which redirects to the original url of the photo. After that, I created a folder (example.com/getphoto/) on my webspace with a .htaccess file inside. The .htaccess redirects all request in this folder to the photo.php site which redirects to the image (webcam.example.com/image.jpg). So you could add everything to the url of the folder and still get the picture (e. g. example.com/getphoto/42 or example.com/getphoto/hrte8437g). The telegram api seems to cache photos by url, so if you add always another ending to the url passed to the api, telegram doesnt use the cached version and sends the current image instead. The easiest way to always change the url is by adding the current date to it.
example.com/photo.php
<?php
header("Location: http://webcam.example.com/image.jpg");
die();
?>
example.com/getphoto/.htaccess
RewriteEngine on
RewriteRule ^(.*)$ http://example.com/photo.php
in python:
bot.sendPhoto(chat_id, 'example.com/getphoto/' + strftime("%Y-%m-%d_%H-%M-%S", gmtime()))
This workaround should also work in other languages like java or php. You just need to change the way to get the current date.

Python Facebook upload video from external link

I'm trying to upload a video to facebook from an external url. But I got error when I post it. I tried with local videos, and all works fine.
My simple code is :
answer = graph.post(
path="597739293577402/videos",
source='https://d3ldtt2c6t0t08.cloudfront.net/files/rhn4phpt3rh4u/2015/06/17/Z7EO2GVADLFBG6WVMKSD5IBOFI/main_OUTPUT.tmp.mp4',
)
and my error is allways the same :
FacebookError: [6000] There was a problem uploading your video file. Please try again with another file.
I looked into the docs and found the parameter file_url but it still the same issue.
The format of the video is .mp4 so it should work.
Any idea ?
Apparently this error message is very confusing. It's the same message when you've an access_token who doesn't work. For example, I've this error message when I'm trying with my user access token and not if I use the Page access token.
I've never used source, I'm pretty sure that's for reading video data off their API. Instead, I use file_url in my payload when passing video file URLs to Facebook Graph API.
Refer to their API doc for clarity on that...
It's also possible that the tmp.mp4 file extension is causing you problems. I've had issues with valid video URLs with non-typical file extensions similar to that. Is it possible to alter that at the source so that the URL doesn't have the tmp ?
A typical payload pass using Requests module to their API that works for me might look something like this:
fburl = 'https://graph-video.facebook.com/v2.3/156588/videos?access_token='+str(access)
payload = {'name': '%s' %(videoName), 'description': '%s' %(videoDescription), 'file_url': '%s' %(videoUrl)}
flag = requests.post(fburl, data=payload).text
print flag
fb_res = json.loads(flag)
I would also highly recommend that you obtain a permanent page access token. It's the best way to mitigate the complexities of Facebook's oAuth process.
facebook: permanent Page Access Token?

Categories

Resources