Aggiunto script di esempio

Leonardo [2010-06-15 08:12]
Aggiunto script di esempio
Filename
example_script.py
mlmanager.py
diff --git a/example_script.py b/example_script.py
new file mode 100755
index 0000000..64b1d62
--- /dev/null
+++ b/example_script.py
@@ -0,0 +1,93 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# This is an example script for mldonkey
+#
+# To use it you must telnet to mldonkey like this:
+# telnet localhost 4000
+# > auth user password
+# > set file_completed_cmd "/path/to/this/script"
+# > quit
+
+import mlmanager
+
+# Adjust some internal variables, for example add a text estension;
+# You can also append to
+# video_extensions, audio_extensions, cdimage_extensions and archive_extensions
+mlmanager.text_extensions.append ("djvu")
+
+# Change mail addr of the daemon and domain of the server.
+mlmanager.domain = "example.org"
+mlmanager.from_addr = "mldonkey@%s" % mlmanager.domain
+
+# Set SMTP server (this is the default, but you could also use some external smtp
+# server if it doesn't need authentication).
+mlmanager.mail_server = "localhost"
+
+# Users that should be notified on error, default is "owner"
+mlmanager.error_recipients = [ "owner", "admin@example.org" ]
+
+# Our mldonkey supports auto commit so we don't need username and password, but we
+# could also use download = mlmanager.Download(username = "user", password = "password")
+download = mlmanager.Download()
+
+# Start writing an email that will be sent to the right users at the end of
+# the script
+mail_text = "Download of %s completed.\n\n" % download.get_filename()
+
+# Parse download duration. Ignore seconds because nobody cares about it.
+hours, minutes, seconds = d.get_duration ()
+if hours > 1:
+    duration = "%s hours and %s minutes" % (hours, minutes)
+elif hours == 1:
+    duration = "one hour and %s minutes" % minutes
+else:
+    duration = "%s minutes" % minutes
+
+# And add it to the text of email
+mail_text += "Duration of the download: %s.\n\n" % duration
+
+# Some users that need to be notified. "owner" means owner of the download
+recipients = [ "user1@provider.com", "user2@anotherprovider.org" , "owner" ]
+
+# Move download to the right place
+if download.get_type() == "video":
+    download.move("/shared/Films")
+    mail_text += "The file has been recognized as a film so it has been copied\n"
+    mail_text += "in /shared/Films.\n"
+
+elif download.get_type() == "audio":
+    download.move("/shared/Musica")
+    mail_text += "The file has been recognized as music so it has been copied\n"
+    mail_text += "in /shared/Music.\n"
+
+    # Our friend likes music, so copy this in his home directory (mldonkey must have
+    # permission to write there!
+    download.copy ("/home/friend/")
+
+else:
+    download.move("/shared")
+    mail_text += "The file has not been automagically recognized, so it is\n"
+    mail_text += "in /shared waiting for someone to put it in the right place.\n"
+
+if download.is_in_group("remote"):
+    mail_text += "The file will be transfered to a remote server.\n"
+
+
+    # user2 is not interested in files that need to be transferred
+    recipients.remove("user2@anotherprovider.org")
+
+# Script signature
+mail_text += "\nmldonkey <mldonkey@robol.it>\n"
+
+# Notify users by mail
+download.notify_email(recipients,
+                      "Download of %s completed" % download.get_filename(),
+                      mail_text)
+
+
+# Finally transfer the file. This is a blocking call, so leave at the end after
+# mail sending.
+if download.is_in_group("remote"):
+    download.rsync("user@remote_server.org:myfolder/downloads/")
+
diff --git a/mlmanager.py b/mlmanager.py
index 91bbfcb..ed2894f 100644
--- a/mlmanager.py
+++ b/mlmanager.py
@@ -19,11 +19,11 @@

 # The fully qualified (or not fully qualified - it really doesn't matter)
 # domain that the server is part of.
-local_domain = "robol.it"
+domain = "robol.it"

 # This is the mail address that will be set as sender for all
 # the emails generated by the script.
-from_addr = "mldonkey <mldonkey@%s>" % local_domain
+from_addr = "mldonkey <mldonkey@%s>" % domain

 # Mail server that the script will use to deliver emails. It must be properly
 # configured to relay mail from the domain selected.
ViewGit