def __init__(self, read_command_line=True):
        config = OptConfigReader()
        config.setup()

        opt = OptReader()
        ret = opt.read(read_command_line)
        if read_command_line and ret:
            return ret

        Logger().setup()
        Logger().warn("##########################################################")
        Logger().warn("%s (running as %s)" % (" ".join(sys.argv), os.getpid()))
        Logger().debug("LANG is %s" % os.environ.get("LANG"))

        proxy = FDHProxySettings()
        proxy.setValues(OPT)
        proxy.activate()

        # init of the flickr api
        token = initialisationFlickrApi(OPT)
        if not isinstance(token, (list, tuple)) or len(token) != 2:
            if token != 6:
                Logger().error("Couldn't init flickr api")
                Logger().error(token)

            raise Exception("Couldn't init flickr api %s" % (token,))
        self.api, self.token = token
Exemple #2
0
#!/usr/bin/python

from flickr_download_helper.config import OptConfigReader, OPT
from flickr_download_helper.logger import Logger
from flickr_download_helper.api import API

from flickr_download_helper.html_widget import FDH_page
import cgi

config = OptConfigReader()
config.setup()

## start the logger
Logger().setup()

# init of the flickr api
api = API(False)

###########################################

form = cgi.FieldStorage()
if 'user_id' in form:
    user_id = form['user_id'].value
else:
    user_id = '53753127@N03'

user = api.getUserFromAll(user_id)
username = user['username']

page = FDH_page()
page.init(css = ('pouet.css'))
class LogJabberBot(JabberBot):
    thread_killed = False
    line = {}
    last_log = None
    log_level = 1
    possible_levels = ('0','1','2','3','4')
    display_date = False
    internal = {}
    _pause = False
    _start_at = None

    def __init__(self, jid, password, to_user, res = None):
        super(LogJabberBot, self).__init__(jid, password, res)

        self.config = OptConfigReader()
        self.config.setup()

        self._to_user = to_user
        self.downloads_file = OPT.downloads_file
        self.last_log_file = OPT.jabber_last

        self.day = datetime.datetime.now().strftime('%Y%m%d')

        if self.last_log_file is None:
            self._log('you must specify jabber_last in the conf file')
            sys.exit(2)

        self._file = open(self.downloads_file, 'rb')
        if os.path.exists(self.last_log_file):
            self._log('getting last log date from %s'%self.last_log_file)
            f = open(self.last_log_file, 'rb')
            l = f.readlines()
            f.close()
            if len(l) > 0:
                self._log('#%s#'%l[-1].strip())
                self._start_at = datetime.datetime.strptime(l[-1].strip(), '%Y-%m-%d %H:%M:%S')

    def idle_proc( self):
        if not len(self.line):
            return

        if self._pause:
            self._log('I am in pause')
            return False

        # copy the message queue, then empty it
        messages = self.line
        self.line = {}

        sorted = messages.keys()
        sorted.sort()
        for timestamp in sorted:
            message, level = messages[timestamp]
            self.last_log = timestamp
            if level > self.log_level:
                if self.display_date:
                    message = "%s %s"%(
                        timestamp.strftime('%m%d %H:%M'),
                        message
                    )
                self._log('sending "%s".' % (message))
                self.send(self._to_user, message)
                time.sleep(1)
            else:
                self._log('not sending "%s" (ll %s)'%(message, level))

    def can_send(self, m):
        if m is not None:
            if self._start_at != None:
                if self._start_at > datetime.datetime.strptime(m.group(1), '%Y-%m-%d %H:%M:%S'):
                    return False
            return True
        return False

    def thread_proc( self):
        if datetime.datetime.now().strftime('%Y%m%d') != self.day:
            # change day : reinits
            self.internal = {}
            self._file.close()
            self._file = open(self.downloads_file, 'rb')

        try:
            while not self.thread_killed:
                lines = self._file.readlines()
                if lines:
                    for line in lines:
                        timestamp = datetime.datetime.now()
                        # get downloads
                        m = re.search('(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\.\d+ (\d+) (.+)', line)
                        if self.can_send(m):
                            if m.group(3) not in self.internal:
                                self.internal[m.group(3)] = 0
                            self.internal[m.group(3)] += int(m.group(2))
                            self.line[timestamp] = ("%s %s"%(m.group(2), m.group(3)), 2)

                        # get video
                        m = re.search('(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\.\d+ video (.+)', line)
                        if self.can_send(m):
                            self.line[timestamp] = ("video : %s"%(m.group(2)), 3)

                time.sleep(1)
            self.finish()
        except Exception, e:
            self._log(e)
            return