emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Add TODO from external app?
@ 2014-04-01 15:41 Lawrence Bottorff
  2014-04-01 17:08 ` Anthony Lander
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Lawrence Bottorff @ 2014-04-01 15:41 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 486 bytes --]

I've seen various things for interacting with org mode from external apps.
I found org-protocol, as well as a Mutt-to-org mode article. Per the
tittle, I want to be able to add new TODO items from, say, an email or a
PHP-based web page form. Is this just brute force external file
manipulation, i.e., e.g., my PHP code would simply open a .org file and
concatenate a properly formatted TODO line . . . or are there more
sophisticated ways of talking to emacs/org-mode from without?

LB

[-- Attachment #2: Type: text/html, Size: 538 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Add TODO from external app?
  2014-04-01 15:41 Add TODO from external app? Lawrence Bottorff
@ 2014-04-01 17:08 ` Anthony Lander
  2014-04-02  3:07 ` Jeff Kowalski
  2014-04-16 16:31 ` Bastien
  2 siblings, 0 replies; 5+ messages in thread
From: Anthony Lander @ 2014-04-01 17:08 UTC (permalink / raw)
  To: Lawrence Bottorff, Carsten Dominik; +Cc: emacs-orgmode@gnu.org Mode


[-- Attachment #1.1: Type: text/plain, Size: 1072 bytes --]

Hi Lawrence,

Here is a Python script I use to scrape TODOs from emails. I haven't
polished it up and put it on github yet, but you are welcome to give it a
whirl (anyone else is too, obviously). You need python 2.5 or greater to
run this. Configure by modifying the variables at the top of the file.

Carsten: If you think this is a worthwhile addition to contrib,  I am happy
to clean it up, and write a bit of documentation for inclusion with org
mode.

Hope this helps,

  -Anthony



On Tue, Apr 1, 2014 at 11:41 AM, Lawrence Bottorff <borgauf@gmail.com>wrote:

> I've seen various things for interacting with org mode from external apps.
> I found org-protocol, as well as a Mutt-to-org mode article. Per the
> tittle, I want to be able to add new TODO items from, say, an email or a
> PHP-based web page form. Is this just brute force external file
> manipulation, i.e., e.g., my PHP code would simply open a .org file and
> concatenate a properly formatted TODO line . . . or are there more
> sophisticated ways of talking to emacs/org-mode from without?
>
> LB
>

[-- Attachment #1.2: Type: text/html, Size: 1589 bytes --]

[-- Attachment #2: todo-email-scraper.py --]
[-- Type: text/x-python-script, Size: 6476 bytes --]

#!/usr/bin/env python

#  todo-email-scraper.py - Watch an email address for TODOs and add them to an org file
#
# Copyright (c) 2014 Free Software Foundation, Inc.
#
# Authors:
#          Anthony Lander <anthony.lander@gmail.com>
#
# Version: 1.0
# Keywords: org, todo, email
#
# This file is not part of GNU Emacs.
#
# This program is free software# you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation# either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY# without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
#
# Commentary:
#
# - Email subject is the TODO line, and body is the TODO body.
# - Add a property line :TODO-TARGET: true to the parent heading where TODOs should be dropped
# - Configure email address, diary file, password below.
# - You can schedule this to run every 30 minutes in a cron job.

# - The scraper scrapes all emails from a designated address (so only you can
# - send yourself todos). as such, it is best to set up a todo email address to
# - receive only todo emails.


import sys
from os import rename
from datetime import datetime
from string import replace
import imaplib
import StringIO
from email.parser import Parser

# ### Configuration
diary_file = "/path/to/org_file.org"  # Replace with your org file
server = 'imap.gmail.com'             # Replace with your imap mail server
username = 'tasklist@gmail.com'       # Replace with the email address that receives todos
password = 'passowrd'                 # Replace with the password for the above email address
authorized_sender = 'you@gmail.com'   # Replace with the email address from which you send todos


def is_diary_file_available():
    try:
        file = open(diary_file, "r")
    except IOError:
        return False
    file.close()
    return True

    
def get_todos():

    imap = imaplib.IMAP4_SSL(server)
    imap.login(username, password)
    imap.select()

    search_criteria = '(UNSEEN FROM "' + authorized_sender + '")'
    typ, data = imap.search(None, search_criteria)

    todos = []
    for num in data[0].split():
        todo_subject = ""
        todo_body = []
        typ, data = imap.fetch(num, '(RFC822)')

        file = StringIO.StringIO(data[0][1])
        message = Parser().parse(file)
        todo_subject = message['subject']
#        print message['Subject'] + "\n"

        body = ""
        for part in message.walk():
            t = part.get_content_type()
            if t and t.lower() == "text/plain":
                # Found the first text/plain part 
                body = part.get_payload(decode=True)
                break
        
 #       print ">", body, "<"
        long_line = ""
        for line in [line.strip() for line in body.splitlines() if len(line) > 0]:
            if line[-1:] == "=":
                long_line += line[:-1]
            else:
                if len(long_line) > 0:  # There is a long line waiting to be written
#                    print "long line: ", long_line
                    todo_body.append(long_line)
                    long_line = ""
                else:
#                    print "line: ", line
                    todo_body.append(line)
                    long_line = ""
                
        if len(long_line) > 0:
#            print "long line (final): ", long_line
            todo_body.append(long_line)
        todos.append({'subject' : todo_subject, 'body' : todo_body})
                
    imap.close()
    imap.logout()
    return todos
    
    
def new_diary_lines_with_todos(todos):
    try:
        file = open(diary_file, "rt+")
        found = False
        done = False
        stars = ""
        timestamp = datetime.now().strftime("[%Y-%m-%d %a %H:%M]")
        log_lines = [":LOGBOOK:", '- State "TODO"       from ""           ' + timestamp, ":END:"]
        
        lines = []
        while not done:
            line = file.readline()
            if len(line) == 0:
                done = True
            else:
                lines.append(line)

                if not found:
                    if line[0] == "*":
                        stars = line.split()[0]

                    if line.strip() == ":TODO-TARGET: true":
                        found = True

                        line = ""
                        while not line.strip() == ":END:":
                            line = file.readline()
                            lines.append(line)

                        for todo in todos:
                            line = "%s%s%s%s" % (stars, "** TODO ", todo['subject'], "\n")
                            lines.append(line)
                            spaces = replace(stars + "** ", "*", " ")
                            for line in log_lines:
                                lines.append(spaces + line + "\n")

                            for line in todo['body']:
                                lines.append(spaces + line + "\n")
        file.close()
    except:
        print "Unexpected error:", sys.exc_info()[0]
        file.close()
        raise
    return lines


def write_todo_file(filename, todos):
    try:
        file = open(filename, "w")
        for todo in todos:
            file.write(todo)
        file.close()
        return True
    except:
        print "Unexpected error:", sys.exc_info()[0]
        file.close()
        raise

    return False

def scrape_todos():
    if not is_diary_file_available():
        print "No diary file available - aborting."
        sys.exit(-1)

    todos = get_todos()
    if len(todos) > 0:
        new_lines = new_diary_lines_with_todos(todos)
        try:
            now = datetime.now().strftime(".%Y-%m-%d-%H-%M")
            diary_backup = diary_file + now + ".orig"
#            print "renaming", diary_file, " to ", diary_backup
            rename(diary_file, diary_backup)
        except:
            print "Unexpected error while renaming:", sys.exc_info()[0]
            raise
        
        write_todo_file(diary_file, new_lines)
        print "Added ", len(todos), " todos at ", now


if __name__ == '__main__':
    scrape_todos()
    sys.exit(0)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Add TODO from external app?
  2014-04-01 15:41 Add TODO from external app? Lawrence Bottorff
  2014-04-01 17:08 ` Anthony Lander
@ 2014-04-02  3:07 ` Jeff Kowalski
  2014-04-16 16:31 ` Bastien
  2 siblings, 0 replies; 5+ messages in thread
From: Jeff Kowalski @ 2014-04-02  3:07 UTC (permalink / raw)
  To: emacs-orgmode

I use org-etml to serve pages from within emacs and use a custom capture 
handler, like this:

####
(defun jeff/capture-handler (request)
  "Handle REQUEST objects meant for 'org-capture'.
GET header should contain a path in form '/capture/KEY/LINK/TITLE/BODY'."
  (with-slots (process headers) request
    (let ((path (cdr (assoc :GET headers))))
      (if (string-match "/capture:?/\\(.*\\)" path)
          (progn
            (org-protocol-capture (match-string 1 path))
            (ws-response-header process 200))
        (ws-send-404 process)))))

(setq jeff/org-ehtml-handler
  '(((:GET  . "/capture") . jeff/capture-handler)
    ((:GET  . ".*") . org-ehtml-file-handler)
    ((:POST . ".*") . org-ehtml-edit-handler)))

(when t
  (mapc (lambda (server)
          (if (= 3333 (port server))
              (ws-stop server)))
        ws-servers)
  (ws-start jeff/org-ehtml-handler 3333))
####

And the relevant org-capture looks like

####
("b" "entry.html" entry (file+headline (concat org-directory "toodledo.org") 
"TASKS")
               "* TODO [#C] %:description\nSCHEDULED: %t\n%:initial\n"
               :immediate-finish t)

####

Then, I post from a hosted form served as entry.html via org-ehtml, like 
this:

###
<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<head>
<title>Org Entry</title>
</head>
<html>

  <style>
    input[type=text] {
    -webkit-appearance: none; -moz-appearance: none;
    display: block;
    margin: 0;
    width: 100%; height: 40px;
    line-height: 40px; font-size: 17px;
    border: 1px solid #bbb;
    }
    input[type=submit],select {
    -webkit-appearance: none; -moz-appearance: none;
    display: block;
    margin: 0;
    height: 40px;
    line-height: 40px; font-size: 17px;
    border: 1px solid #bbb;
    }
  </style>

  <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>

  <form id="target" action="/capture/b">
    <input id="title" width="100%" type="text" name="title" />
    <br>
    <select id="context" name="context">
      <option value="@agendas">@agendas</option>
      <option value="@calls"  >@calls  </option>
      <option value="@errands">@errands</option>
      <option value="@home"   >@home   </option>
      <option value="@quicken">@quicken</option>
      <option value="@view"   >@view   </option>
      <option value="@waiting">@waiting</option>
      <option value="@work"   >@work   </option>
    </select>
    <br>
    <input type="submit" value="Submit" name="submit">
  </form>
  <span></span>

  <script type="text/javascript">
    $("#target").submit (function (event) {
        event.preventDefault();

        var link  = encodeURIComponent("LINK");
        var title = encodeURIComponent($("#title").val() + "  :" + 
$("#context").val() + ":");
        var body  = encodeURIComponent("");
        var xurl   = "/capture/b" + "/" + link + "/" + title + "/" + body;

        $.ajax({
            url: xurl
        }).success(function() {
            $("span").text("captured "+xurl).show().fadeOut(1000);
            $("#title").val("");
            $("#context").val("@agendas");
        }).fail(function(jqXHR, textStatus) {
            $("span").text("failed " + xurl + "<br>" + textStatus).show(); 
//.fadeOut(1000);
        });
    });
  </script>

</html>
####


Forwarding ports from my machine running org-ehtml on emacs means I can 
access the page anywhere to add new tasks even from my cell phone.

You could easily call this emacs-webservice from a PHP page, but it's just 
as easy to simply serve the page from emacs itself.  Take a look at org-
ehtml and the companion webserver that Schulte wrote.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Add TODO from external app?
  2014-04-01 15:41 Add TODO from external app? Lawrence Bottorff
  2014-04-01 17:08 ` Anthony Lander
  2014-04-02  3:07 ` Jeff Kowalski
@ 2014-04-16 16:31 ` Bastien
  2014-04-16 18:15   ` Tim Visher
  2 siblings, 1 reply; 5+ messages in thread
From: Bastien @ 2014-04-16 16:31 UTC (permalink / raw)
  To: Lawrence Bottorff; +Cc: emacs-orgmode

Hi Lawrence,

Lawrence Bottorff <borgauf@gmail.com> writes:

> I've seen various things for interacting with org mode from external
> apps. I found org-protocol, as well as a Mutt-to-org mode article.
> Per the tittle, I want to be able to add new TODO items from, say, an
> email or a PHP-based web page form. Is this just brute force external
> file manipulation, i.e., e.g., my PHP code would simply open a .org
> file and concatenate a properly formatted TODO line . . . or are
> there more sophisticated ways of talking to emacs/org-mode from
> without?

I'm aware of such sophisticated ways, so for now simply open a .org
file and write to it.

-- 
 Bastien

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Add TODO from external app?
  2014-04-16 16:31 ` Bastien
@ 2014-04-16 18:15   ` Tim Visher
  0 siblings, 0 replies; 5+ messages in thread
From: Tim Visher @ 2014-04-16 18:15 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode, Lawrence Bottorff

On Wed, Apr 16, 2014 at 12:31 PM, Bastien <bzg@gnu.org> wrote:
> Hi Lawrence,
>
> Lawrence Bottorff <borgauf@gmail.com> writes:
>
>> I've seen various things for interacting with org mode from external
>> apps. I found org-protocol, as well as a Mutt-to-org mode article.
>> Per the tittle, I want to be able to add new TODO items from, say, an
>> email or a PHP-based web page form. Is this just brute force external
>> file manipulation, i.e., e.g., my PHP code would simply open a .org
>> file and concatenate a properly formatted TODO line . . . or are
>> there more sophisticated ways of talking to emacs/org-mode from
>> without?
>
> I'm aware of such sophisticated ways, so for now simply open a .org
> file and write to it.

Emacs can be used as a scripting environment so if you can figure out
how to do it from elisp that might be a more sophisticated way.

That said, one of the beauties of org is that it's just plain text and
the format for a link is pretty darn simple. So I've always just
written to the file using the format.

--

In Christ,

Timmy V.

http://blog.twonegatives.com/
http://five.sentenc.es/ -- Spend less time on mail

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-04-16 18:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-01 15:41 Add TODO from external app? Lawrence Bottorff
2014-04-01 17:08 ` Anthony Lander
2014-04-02  3:07 ` Jeff Kowalski
2014-04-16 16:31 ` Bastien
2014-04-16 18:15   ` Tim Visher

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).