How to get colored emails from crontab? - python

I call a Python script from crontab. The script does generates colored output using ANSI escapes but when crontab is sending the mail with the output I see the escapes instead of colors.
What is happening is logic but I would like to know if it would be possible to generate a html message instead.
I would like a solution that does not require to implement the email notification myself.

Maybe you can try with some txt to html converter, for example, http://txt2html.sourceforge.net/, you can also use pygments with some modifications.

Related

string formatting appears differently on different environment

I'm trying to send text from python to slack by using slackbotAPI.
I processed text like this: (looks neat)
I formatted string by this code line:
But after sending this text by slackbot API, text appeared on slack like this:
I guess the width of each character(include space)is different on slack, but not on python prompt.
How can I see the text neatly on slack like on python?
Thank you.
I think the best way to achieve what you're looking for isn't with text characters like tabs etc, but to use fields inside a section block.
That will make sure that regardless of interface, they're arranged correctly.
Here's an example

What's the best way to send the data from a Repl.it to my backend?

I'm storing Python code in my DB as plain text. I want to fetch it, edited in the frontend, and then post it back to the DB. If relevant, this is inside a Vue project.
I've seen alternatives such as Repl.it. It partially solves the problem: You can edit a piece of code, it gets saved on Repl.it's backend.
But I want to save such code as plain text on my DB. How can I do that?
Is Repl.it not the best alternative at all? I thought about placing everything in a tag. But ideally, the code would be written in an IDE-kind of environment. That's why I thought about Repl.it.
Would you like to run the code in the browser after fetching it? If so, yes, try a service like glitch, codesandbox, etc
If you only want to show it and edit it, use a rich text input, hopefully with code highlighting.

Can't enter txt file contents as query string using python

I can’t get python to open a link that uses the contents of a .txt file as a query string. I’m working on Python 3.7.0 and was able to write code that opens the website and checks a string that I’ve input directly, as well as open my text file and print the contents, but when I try to make the text file’s contents a query it throws an error.
I added lines that print the link that I would need to open to make sure it comes out correctly and that works fine, I can copy and paste it into my browser and get a correct result.
Here's the code I used
And a screenshot of the error I get
I'm a total beginner at this so any suggestions or explanations would be lifesavers!
The error is with the string being passed to the urlopen(). When it tries to open the link you get an HTTP 400 : Bad request error which means that something is wrong with the link you provided. The text possibly has spaces and you aren't escaping the characters properly. Here is the link which could help you.
Alternatively, you could also use the Python Requests library.
(Please include the code in the question rather than screenshot)
Check out the http you’re requesting does ‘actually’ exists. Moreover, I’m not sure how’s your .txt file looks like, but reexamine the code (.read() part) to make sure the data you wanted to add as a query is being handled correctly.

HTML Decoding in Python

I am writing a python script for mass-replacement of links(actually image and script sources) in HTML files; I am using lxml. There is one problem, the html files are quizzes and they have data packaged like this(there is also some Cyrillic here):
<input class="question_data" value="{"text":"<p>[1] је наука која се бави чувањем, обрадом и преносом информација помоћу рачунара.</p>","fields":[{"id":"1","type":"fill","element":{"sirina":"103","maxDuzina":"12","odgovor":["Информатика"]}}]}" name="question:1:data" id="id3a1"/>
When I try to print out this data in python using:
print "OLD_DATA:", data
It just prints out the error "UnicodeEncodeError: character maps to undefined". There are more of these elements. My goal is to change the links of images in the value part of input, but I can't change the links if I don't know how to print this data(or how it should be written to the file). How does Python handle(interpret) this? Please help. Thanks!!! :)
You're running into the same problem I've hit many times in the past. That error almost always means that the console environment you're using can't display the characters it's trying to print. It might be worth trying to log to a file instead, then opening the log in an editor that can display the characters.
If you really want to be able to see it on your console, it might be worth writing a function to screen the strings you're printing for unprintable characters
I also found a couple other StackOverflow posts that might be helpful in your efforts:
How do I get Cyrillic in the output, Python?
What is right way to use cyrillic in python lxml library
I would also recommend this article and python manual entry:
https://docs.python.org/2/howto/unicode.html
http://www.joelonsoftware.com/articles/Unicode.html

Extra Tabs in IMAP HTML Text

I'm using Python and imaplib to obtain emails from a IMAP server (supports all kinds of IMAP servers - GMail, etc.).
My problem is: Using the IMAP BODY[INDEX] command to fetch a specific body part, the HTML comes with extra tabs. As in:
(...)</a>\t\t\t\t\t\t\t\t<a>(...)
When showing the HTML the tabs are obviously extra:
(The screenshot is in the Portuguese language but I believe that is not relevant.
I have searched IMAP documentation but found nothing that helps. I am guessing these \t are always following tag closes (such as \t\t\t\t\t), so I could just find all tabs that come after a tag close and delete them, but I don't know if that would be a reliable method at all.
Thank you
I found a solution (for the time being at least).
When receiving data from a IMAP call response, there are \\r\\n characters delimiting the lines. I remove these.
However, I discovered that besides these there are also \\t characters coupled with these in some instances. For example:
\\r\\n\\t\\t\\t\t
If I remove the \\t together with the \\r\\n, the HTML is presented perfectly.

Categories

Resources