LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   write a script to automatically post to a blog??? (https://www.linuxquestions.org/questions/linux-general-1/write-a-script-to-automatically-post-to-a-blog-368673/)

Hosiah 10-01-2005 03:32 AM

write a script to automatically post to a blog???
 
Just so you know I'm not a spammer, here's my blog .

As you can see, I create 3D graphic artwork and post it to my blog gallery. Since I'm committed to posting at least one new image per day for life, I am interested in automating as much of the process as I can. What I want to be able to do is to create a whole batch of images, upload them to Blogger's server at once, and then dole out the postings at (for example) three per day by having a script email the postings. For instance, I can create 21 images easily working all day Sunday, and then I'd have them auto-posting all week, saving my time for other pursuits and cutting the necessity of interrupting other household members' use of the computer during the week.

Now, I can post by email from my linuxquestions account and it'll show up on the blog. I can use *any* web-based email service and it'll post. But I can't script those! When I try to use a scriptable email tool like Linux's "mail" or "pine" program, it *doesn't* post.

Can anybody at least explain to me how it would "know" what kind of program I'm using to post from? And any ideas for a work-around?

win32sux 10-01-2005 04:04 AM

what does the mail command you are using look like??

Hosiah 10-01-2005 04:21 AM

Quote:

what does the mail command you are using look like??
??? It looks like the text you type in a terminal. The "mail" program is an ed/vi-based command line utility invoked by typing "mail" at the Bash prompt

win32sux 10-01-2005 06:15 PM

Quote:

Originally posted by Hosiah
??? It looks like the text you type in a terminal. The "mail" program is an ed/vi-based command line utility invoked by typing "mail" at the Bash prompt
dude, i know what mail is... :)

what i had asked for is the entire command you are using, WITH ALL THE OPTIONS. like, for example, it might look as simple as:
Code:

cat message.txt | mail -s "This is not SPAM!" user@example.net
since it seems there's something being interpreted as "different" between your CLI-sent mails and the ones you send using a web-based front-end, knowing what the mail command you are using looks like might help in finding a difference...

another thing to consider is what mail program you are actually using... "mail" is a lot of times nothing but a LINK to the actual program... for example, on slackware it's a link to nail...
Code:

bash-3.00$ ls -l /usr/bin/mail
lrwxrwxrwx  1 root root 4 2005-09-30 10:35 /usr/bin/mail -> nail

BTW, one thing you should probably do is email yourself from a known-working (in regards to the blog posts) account and from the CLI (using mail) and then compare the mail headers on the two messages... that's likely to be were the blog servers are getting the information which they use to decide to not allow your CLI messages... maybe they have a policy of making it as difficult as they can to script postings or something... anyways, it's just a thought...


Hosiah 10-01-2005 08:04 PM

Oy! OK, that's what you're talking about.

Slackware being my fave, it is my office machine...but this is El Sombrero de Rojo that's got the only internet connection. On the "family" PC, where the family just got to it from Windows last year, I don't dare force them onto Slackware so soon, or the dormant Borg brain-washing will reassert itself.

I don't give mail any arguments except the address, I just type "mail mysecretpostingaddress@foo.com" and it asks for subject line and then I type the body and type "." for done like in vi and it asks for CC: and then it sends it! I checked my directories - no softlinks, mail really is mail! According to my man page, it's the 4th Berkely dist of the BSD utility, itself. Like I say, I also tried it with pine (*pine*-pine, with the emacs-style interface) - no good.

Sorry, but, as opposed to programming for the machine, I have no proficiency at programming for the web! This is what I'm looking to learn next.

The header with mail sent from the console to webmail like linuxquestions.net shows the sender as "hosiah@localhost.localdomain". Blogger.com's own documentation says that you keep the mail-to-blog address secret, or else *anyone* could post to your blog - sure, anybody but the machine I want to send from! I even tried a free mail-forwarding service, and it will forward mail from webmail addresses but not my own console. I await indefinitely response from Blogger tech support (past experience has taught me not to hope for much from them).

If I knew where to start - is this something between SMTP and POP3? Is there a way to configure mail to use a webmail address? A bug with Blogger? A bug with my ISP (I use DSL)? A bug in my brain?

win32sux 10-01-2005 08:11 PM

Quote:

Originally posted by Hosiah
The header with mail sent from the console to webmail like linuxquestions.net shows the sender as "hosiah@localhost.localdomain".
maybe that's your problem right there??

with nail, for example, you can specify the return address like so:
Code:

cat message.txt | mail -s "Hello!!!" -r hosiah@example.net \
blog-robot@exampleisp.net



Hosiah 10-01-2005 10:59 PM

Well, I've downloaded nail, which I think should "nail" the problem :) ! From the doc, it seems much more capable. I couldn't seem to find the equivalent in mail. I've also dug up a copy of Eris S. Richardson's "Programming Web Server Applications" :study: So I'll get this sorted out.

Thanks for the advice! I was just frustrated yesterday because I led myself to expect the impossible: "Ha! It says *anybody* can email posts, so I just doodle out a Bash script that calls mail! A ten-minute fix!" and then 8-hours later after pulling my 99th idea out of the air ("This time for sure!!!...Doh!"), I'm still where I was 8 hours ago...

On the plus side, my blog-posting address is actually a little more secure than what the Blogger docs would have led me to believe!

UPDATE: That -r switch worked! Holy cow, it never occurred to me to simply *lie* about where the email comes from! Geez, some security!

Hosiah 10-08-2005 08:17 AM

Another Problem
 
OK, right five minutes after my last post, I encountered another problem: In order for the Blogger post to format properly, it has to be formatted in HTML. I can write (even programmatically generate) HTML, and email it, but it shows up in plain text format (things like "img=""" tags ignored and posted verbatim).

*Every* book, webpage, help forum, and resource in the world that I've found, if they address this topic at all, only warns against doing it at all. It's supposed to be the biggest taboo in the world, to hear them tell it. Trouble is, email programs are for sending email, not posting to a blog, so the functionality just isn't there. Come to think of it, I've never heard one person actually claim having successfully *used* the mail-to-Blogger feature, so I'm probably chasing a unicorn. I have literally scoured the web for days, but none of the methods I've found work.

sasho 10-08-2005 09:00 AM

Posting on the blog is form-based, correct?
If so, I believe this can be done by using Perl with the LWP module that will interact with the web page (fully automated, no browser will be open) and do the following:

1. Do a proper login
2. fill out the form and click (submit) any buttons if nwecessary
3. Logout

SImilar to this:

Code:

#!/usr/bin/perl

use HTTP::Request::Common;
use HTTP::Cookies;
use LWP 5.64;
use strict;

  my $browser = LWP::UserAgent->new;
        my $cookie_jar = HTTP::Cookies->new;

        #Perform geoserver authentication:
        my $url =
        'http://somehost.com/login.php';
        my $response = $browser->request(POST $url,
                ['username' => 'someusername', 'password' => 'somepassword', 'submit' => 'Submit']);

        die "Error: ", $response->header('WWW-Authenticate') || 'Error accessing',
        # ('WWW-Authenticate' is the realm-name)
        "\n ", $response->status_line, "\n at $url\n Aborting"
        unless $response->is_success;

        $cookie_jar->extract_cookies($response);

        #Some code here similar to above to fill out the blog entry forms

        #Finally, logout:
        $url =
        'http://somehost.com/logout.php';
        $req = new HTTP::Request POST => $url;
        $cookie_jar->add_cookie_header($req);
        $response = $browser->request($req);


Hosiah 10-09-2005 12:03 AM

Quote:

I believe this can be done by using Perl with the LWP module
Yeah, Perl looks like it can do this (unfortunately, I was naive to think that just because I already carry C,C++, Lisp, CLisp, Python, HTML, and Bash around in my head meant that I was actually prepared for writing any programs that are good for something. But oh, no, I need to learn Perl, too!) and also the program cURL seems like it can work.

I just can't believe I'm going to be turned off my original path by such a trifling thing! The whole email thing works like a charm, but no HTML-formatting. I've tried adding headers to the top of the post including the "From:" and "To:" and "Subject:" and "Content-type: text/html" and leaving a blank line between the header section and the post body - both nail and sendmail stop dead saying no recipient specified. I've searched high and low for a command-line option to specify the content-type - one site assurred me that -a is for that, but it's for "appending" files.

Somehow, I have a feeling that there must be a simpler way - it can't be that everybody who ever used Blogger in a scripted context had to learn Perl?

EDIT UPDATE: The above analysis is wrong. I have now verified in multiple test emails to diffent accounts that there is, in fact, nothing wrong with nail or sendmail. It's a wierd Blogger behavior. So I'm going the cURL route. This is good for me, I need to conquor web programming the same way I tackled single-machine programming. I've been putting it off too long, for reasons just like this case shows: half the factors are black boxes.

sasho 10-09-2005 08:52 PM

Python most likely definitely does what you need to.
I just don't know Python.
I know that C++ can do this, but I can't point you to a solution right away.

Hosiah 10-14-2005 11:30 AM

Solution Found!
 
I always try to post the solution I went with, in case other people searching for an answer to the same question find this thread. So, here's how I did it (this is only one possible solution, countless more exist, but you have to find the one that works for you!):

In listing the languages I knew previously, I forgot all about Tcl/Tk (yes, I forgot that I learned one of the languages I *know*. How's that for absent-minded?). And Tcl/Tk has Expect, a language specifically designed to script interaction with other programs. Running "autoexpect" during a session where I used Lynx to post a test entry to my blog was proof-of-concept that it could be done. Unfortunately, the script generated this way proved unusable, but pointed me in the right direction. Here's an example of what works:

Code:

#!/usr/bin/expect -f
set force_conservative 1  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}

spawn lynx -accept_all_cookies http://www.blogger.com/start
expect "Username:"
send -- "User"
send -- "\t"
send -- "\t"
expect "Password:"
send -- "password"
send -- "\r"
send -- "\r"
expect "REFRESH"
send -- "\r"
expect "Blogger"
sleep 1
send -- "g"
send -- "http://www.blogger.com/post-create.g?blogID=#deadbeef"
send -- "\r"
expect "MyBlog"
sleep 1
send -- "\t"
send -- "\t"
send -- "\t"
send -- "\t"
send -- "\t"
send -- "\t"
send -- "\t"
send -- "\t"
send -- "\t"
send -- "\t"
send -- "\t"
send -- "Blog Title"
send -- "\t"
send -- "My test post. Here is an HTML link: <a href=\"http://www.gnu.org\">GNU software</a>"
send -- "\t"
send -- "\t"
send -- "\t"
send -- "\t"
send -- "\t"
send -- "\t"
send -- "\t"
send -- "\t"
send -- "\r"
send -- "Q"
expect eof

Where:
User=your user name
password is your password for login
#deadbeef is the number of your blog's ID, assigned by Blogger. To get this unique number, get to your "dashboard" on Blogger in lynx, highlight the link to "create new post" for that blog and press "=", noting the contents of "current link". Or just watch your address bar when you publish in Firefox, or such. I do it this way because I have multiple blogs, and it's a toss-up which one will be on top depending on which one you fiddled with last.

I put all the tabs (\t) on individual lines to make them easier to count - simulating the exact way a person using lynx would probably navagate to a link. Or, you can use the "g - http://link " syntax like I use to connect to the individual blog. Both methods have success. The various "sleep" statements are necessary to let the connection catch up. Note that much error-checking should be added, here. This is about as bullet-proof as jello.

Note that parts of HTML code sent from the command line (which is how expect treats it's scripts, just like a Bash script) have to be escaped with slash's, such as the quotes around the website address. The whole point of this, of course, is to have a machine-generatable post ready to go, so you can have another script that takes your HTML blog post all ready to go, runs sed on it to escape special characters, adds everything before the post body in this script as a "header" and everything after it as a "trailer", then executes the whole thing!

This is not to deprecate sasho's Perl code. It might be better, who knows. One of these days, I really, really have to learn Perl - it's awesome from what I see it can do. But I like to learn a language as a project in itself, not as a footnote to another project; that's too distracting to read the documentation the whole while thinking "but how do I do *that* *one* *thing*?"

Also, if anybody's thinking of trying the email-to-post route, don't. While I was working on this, I left the email feature turned on, and sure enough, somebody spammed the blog! Talk about insult to injury! Obviously, this was somebody's automatic bot sending garbage to random email addresses. As Blogger support says, there's no security with the email feature. I turned the email feature back off and deleted the post, of course.


All times are GMT -5. The time now is 11:45 AM.