<commit_before>import datetime
import re
import os
import requests
import json
import uuid
import random
import calendar
import time
import libs.SerializableDict as SerializableDict
import libs.StorageObjects as StorageObjects
import libs.Models as Models
import libs.Loggiz as Loggiz
from pytz import timezone
import pytz
import telegram
import logging
'''
Copyright (c) 2016, Egil Hasting
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
'''
__author__ = "Egil Hasting"
__copyright__ = "Copyright 2016"
__credits__ = ["Egil Hasting"]
__license__ = "BSD"
__version__ = "1.0.0"
__maintainer__ = "Egil Hasting"
__email__ = "egil.hasting@higen.org"
__status__ = "Production"

#         self.db = dbobject
 #       self.uindex = dbobject.Get("userindex")
class GeneralMessageEvent(object):
    def __init__(self):
        self.dbobject = Models.StaticModels()

    def run(self, bot, update, args):
        raise NotImplementedError()

    @classmethod
    def stripusername(self, username):
        if username.startswith("@"):
            # remove @
            return username[1:]
        else:
            return username

class Null(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)

    def run(self, bot, update, args):
        pass

class WebSearchDuckDuckGo(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)

    def _generate_url(self, searchstring):
        searchstring = searchstring.replace(" ", "+")
        print(searchstring)
        return "http://api.duckduckgo.com/?q={}&format=json".format(searchstring)

    def run(self, bot, update, args):
        r = requests.get(self._generate_url(" ".join(args)))
        if r.status_code == 200:
            searchresult = r.json()
            resultcount = len(searchresult["RelatedTopics"])
            outputstring = "{} (found: {})\n".format(searchresult["Heading"], resultcount)
            limitcounter = 0
            for article in searchresult["RelatedTopics"]:
                outputstring += article.get("Result", "") + "\n"
                d = article.get("Result", "")
                if d == "":
                    print(article)
                limitcounter += 1
                if limitcounter == 3:
                    break
            bot.sendMessage(update.message.chat_id, text="{}".format(outputstring), parse_mode="HTML")

class Time(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)

    def run(self, bot, update, args):
        localtime = datetime.datetime.now()
        home = pytz.timezone("Europe/Oslo")
        baltimore = pytz.timezone("US/Eastern")
        denver = pytz.timezone("America/Denver")

        localtime = home.normalize(home.localize(localtime))
        baltimoretime = localtime.astimezone(baltimore)
        denvertime = localtime.astimezone(denver)
        '''
            CLOCK_FACE_ONE_OCLOCK = n(b'\xF0\x9F\x95\x90')
            CLOCK_FACE_TWO_OCLOCK = n(b'\xF0\x9F\x95\x91')
            CLOCK_FACE_THREE_OCLOCK = n(b'\xF0\x9F\x95\x92')
            CLOCK_FACE_FOUR_OCLOCK = n(b'\xF0\x9F\x95\x93')
            CLOCK_FACE_FIVE_OCLOCK = n(b'\xF0\x9F\x95\x94')
            CLOCK_FACE_SIX_OCLOCK = n(b'\xF0\x9F\x95\x95')
            CLOCK_FACE_SEVEN_OCLOCK = n(b'\xF0\x9F\x95\x96')
            CLOCK_FACE_EIGHT_OCLOCK = n(b'\xF0\x9F\x95\x97')
            CLOCK_FACE_NINE_OCLOCK = n(b'\xF0\x9F\x95\x98')
            CLOCK_FACE_TEN_OCLOCK = n(b'\xF0\x9F\x95\x99')
            CLOCK_FACE_ELEVEN_OCLOCK = n(b'\xF0\x9F\x95\x9A')
            CLOCK_FACE_TWELVE_OCLOCK = n(b'\xF0\x9F\x95\x9B')
    '''
        out = "Current Time\n"
        out += "Norway: " + str(localtime.strftime('%X %x %Z')) + "\n"
        out += "Baltimore: " + str(baltimoretime.strftime('%X %x %Z')) + "\n"
        out += "Denver: " + str(denvertime.strftime('%X %x %Z')) + "\n"
        Loggiz.log.write.info(out)
        bot.sendMessage(update.message.chat_id, text="{}".format(out), parse_mode="HTML")

class Stats(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)
        self.seen = self.dbobject.Get("seenlog")
        self.uindex = self.dbobject.Get("userindex")

    def run(self, bot, update, args):
        users = self.seen.usercounter.Get()
        data = users.rawdict()
        output_string = "Most Active User Stats (by words):\n\n"
        place = 1
        for key, user in sorted(data, key=self.sort_by_word, reverse=True):
            username = key
            if username == "":
                continue
            usercountobject = SerializableDict.UserObject(user)
            output_string += "[{}] {}: {} (Lines: {})\n".format(place, username, usercountobject.wordcounter, usercountobject.counter)
            place += 1
        Loggiz.log.write.info(output_string)
        bot.sendMessage(update.message.chat_id, text="{}".format(output_string), parse_mode="HTML")

    def sort_by_word(self, userdict):
        usercountobject = SerializableDict.UserObject(userdict[1])
        if not isinstance(usercountobject.wordcounter, int):
            return 1
        Loggiz.log.write.info(usercountobject.wordcounter)
        return usercountobject.wordcounter

class Help(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)

    def run(self, bot, update, args):
        output_string = "Available commands\n"
        output_string += commands
        ybot.sendMessage(update.message.chat_id, text="{}".format(output_string), parse_mode="HTML")

    @classmethod
    def sort_by_word(cls, userdict):
        usercountobject = SerializableDict.UserObject(userdict)
        if usercountobject.wordcounter == "":
            return 0
        return usercountobject.wordcounter

class AudioTips(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)
        self.tipdb = self.dbobject.Get("tipdb")

class Counter(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)
        self.seen = self.dbobject.Get("seenlog")

    def run(self, bot, update, args):
        user = self.seen.usercounter.Get()
        usercount = user.get(update.message.from_user.username)
        usercountobject = SerializableDict.UserObject(usercount)

        # Line counter
        if usercountobject.counter == "":
            usercountobject.counter = 1
        else:
            usercountobject.counter = usercountobject.counter + 1
        # Word counter
        currentwordcount = re.findall('\w+', update.message.text.lower())
        print("Words: {}".format(len(currentwordcount)))
        if usercountobject.wordcounter == "":
            usercountobject.wordcounter = len(currentwordcount)
        else:
            usercountobject.wordcounter = usercountobject.wordcounter + len(currentwordcount)
        # Last seen
        usercountobject.timestamp = str(datetime.datetime.now().replace(microsecond=0))
        # Metadata
        usercountobject.firstname = update.message.from_user.first_name
        usercountobject.lastname = update.message.from_user.last_name
        usercountobject.username = update.message.from_user.username
        # Store object to dictionary and back to DB
        user.set(update.message.from_user.username, usercountobject.SaveObject())
        self.seen.usercounter.Set(user)

class Seen(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)
        self.seendb = self.dbobject.Get("seenlog")

    def run(self, bot, update, args):
        Loggiz.log.write.info("Gettings Stats")
        user = self.seendb.usercounter.Get()

        if len(args) > 0:
            Loggiz.log.write.info("finding user {}".format(args[0]))
            username = self.stripusername(args[0])

            fetchseenuser = user.get(username)
            userseenobject = SerializableDict.UserObject(fetchseenuser)
            Loggiz.log.write.info(userseenobject.timestamp)
            if userseenobject.timestamp != "":
                bot.sendMessage(update.message.chat_id, text="hey! {} was last seen {} (lines/words: {}/{})".format(username, userseenobject.timestamp, userseenobject.counter, userseenobject.wordcounter))
            else:
                Loggiz.log.write.warn("Did not find any user info!")
        else:
            bot.sendMessage(update.message.chat_id, text="{} U ale wlong!! do like this!! command @<username>".format(telegram.Emoji.PILE_OF_POO))

class QuoteBase(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)
        self.uindex = self.dbobject.Get("userindex")

class AddQuote(QuoteBase):
    def __init__(self):
        QuoteBase.__init__(self)

    def run(self, bot, update, args):
        new_quote_index = str(uuid.uuid4())

        if len(args) < 2:
            Loggiz.log.write.info("Argument length was {}".format(len(args)))
            bot.sendMessage(update.message.chat_id, text='[USAGE] <username> <quote>')
        else:
            username = self.stripusername(args[0])
            if username not in self.uindex.index.Get():
                tmplist = self.uindex.index.Get()
                tmplist.append(args[1])
                self.uindex.index.Set(tmplist)
                Loggiz.log.write.info("user/nick added to index")
            quotetext = StorageObjects.ComnodeObject("quotestext.{}".format(new_quote_index), "str", desc="", hidden=False)
            quotetext.Set(" ".join(args[1:]))

            quotemetausername = StorageObjects.ComnodeObject("quotemap.{}".format(username), "list", desc="", hidden=False)
            qmun = quotemetausername.Get()
            qmun.append(new_quote_index)
            quotemetausername.Set(qmun)
            bot.sendMessage(update.message.chat_id, text="Quote from {} added with id {}".format(username, new_quote_index))

class Quote(QuoteBase):
    def __init__(self):
        QuoteBase.__init__(self)
        self.taken = list()

    def get_quote(self, username):
        quotemetausername = StorageObjects.ComnodeObject("quotemap.{}".format(username), "list", desc="", hidden=False)
        qmun = quotemetausername.Get()
        if len(qmun) > 0:
            random.seed(calendar.timegm(time.gmtime()))
            foundindex = random.randrange(0, len(qmun))
            Loggiz.log.write.info("found: {}, total: {}".format(foundindex, len(qmun)))
            if len(qmun) == foundindex:
                foundindex = foundindex - 1
            if qmun[foundindex] in self.taken:
                return "TAKEN"
            else:
                quotetext = StorageObjects.ComnodeObject("quotestext.{}".format(qmun[foundindex]), "str", desc="", hidden=False)
                self.taken.append(qmun[foundindex])
            return "{}: {}".format(username, quotetext.Get())
        else:
            return None

    def findrandomuser(self):
        userindexlength = len(self.uindex.index.Get())
        if userindexlength == 0:
            return
        random.seed(calendar.timegm(time.gmtime()))
        luckyuser = random.randrange(0, userindexlength)
        if len(self.uindex.index.Get()) == luckyuser:
            luckyuser = luckyuser - 1
        return self.uindex.index.Get()[luckyuser]

    def run(self, bot, update, args):

        if len(args) == 1:
            quoteoutput = "{} random Quotes\n"
            nums = int(args[0])
            cnt = 0
            Loggiz.log.write.info("Args {}".format(str(args)))
            while True:
                currentquote = self.get_quote(self.findrandomuser())
                if currentquote == "TAKEN":
                    continue
                elif currentquote is None:
                    continue
                quoteoutput += currentquote
                if nums < cnt:
                    break
                cnt += 1
        else:
            quoteoutput = self.get_quote(self.findrandomuser())
        if quoteoutput is not None:
            bot.sendMessage(update.message.chat_id, text=quoteoutput)

if __name__ == '__main__':
    pass<commit_msg>changed away from telepot<commit_after>import datetime
import re
import os
import requests
import json
import uuid
import random
import calendar
import time
import libs.SerializableDict as SerializableDict
import libs.StorageObjects as StorageObjects
import libs.Models as Models
import libs.Loggiz as Loggiz
from pytz import timezone
import pytz
import telegram
import logging
'''
Copyright (c) 2016, Egil Hasting
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
'''
__author__ = "Egil Hasting"
__copyright__ = "Copyright 2016"
__credits__ = ["Egil Hasting"]
__license__ = "BSD"
__version__ = "1.0.0"
__maintainer__ = "Egil Hasting"
__email__ = "egil.hasting@higen.org"
__status__ = "Production"

#         self.db = dbobject
 #       self.uindex = dbobject.Get("userindex")
class GeneralMessageEvent(object):
    def __init__(self):
        self.dbobject = Models.StaticModels()

    def run(self, bot, update, args):
        raise NotImplementedError()

    @classmethod
    def stripusername(self, username):
        if username.startswith("@"):
            # remove @
            return username[1:]
        else:
            return username

class Null(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)

    def run(self, bot, update, args):
        pass

class WebSearchDuckDuckGo(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)

    def _generate_url(self, searchstring):
        searchstring = searchstring.replace(" ", "+")
        print(searchstring)
        return "http://api.duckduckgo.com/?q={}&format=json".format(searchstring)

    def run(self, bot, update, args):
        r = requests.get(self._generate_url(" ".join(args)))
        if r.status_code == 200:
            searchresult = r.json()
            resultcount = len(searchresult["RelatedTopics"])
            outputstring = "{} (found: {})\n".format(searchresult["Heading"], resultcount)
            limitcounter = 0
            for article in searchresult["RelatedTopics"]:
                outputstring += article.get("Result", "") + "\n"
                d = article.get("Result", "")
                if d == "":
                    print(article)
                limitcounter += 1
                if limitcounter == 3:
                    break
            bot.sendMessage(update.message.chat_id, text="{}".format(outputstring), parse_mode="HTML")

class Time(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)

    def run(self, bot, update, args):
        localtime = datetime.datetime.now()
        home = pytz.timezone("Europe/Oslo")
        baltimore = pytz.timezone("US/Eastern")
        denver = pytz.timezone("America/Denver")

        localtime = home.normalize(home.localize(localtime))
        baltimoretime = localtime.astimezone(baltimore)
        denvertime = localtime.astimezone(denver)
        '''
            CLOCK_FACE_ONE_OCLOCK = n(b'\xF0\x9F\x95\x90')
            CLOCK_FACE_TWO_OCLOCK = n(b'\xF0\x9F\x95\x91')
            CLOCK_FACE_THREE_OCLOCK = n(b'\xF0\x9F\x95\x92')
            CLOCK_FACE_FOUR_OCLOCK = n(b'\xF0\x9F\x95\x93')
            CLOCK_FACE_FIVE_OCLOCK = n(b'\xF0\x9F\x95\x94')
            CLOCK_FACE_SIX_OCLOCK = n(b'\xF0\x9F\x95\x95')
            CLOCK_FACE_SEVEN_OCLOCK = n(b'\xF0\x9F\x95\x96')
            CLOCK_FACE_EIGHT_OCLOCK = n(b'\xF0\x9F\x95\x97')
            CLOCK_FACE_NINE_OCLOCK = n(b'\xF0\x9F\x95\x98')
            CLOCK_FACE_TEN_OCLOCK = n(b'\xF0\x9F\x95\x99')
            CLOCK_FACE_ELEVEN_OCLOCK = n(b'\xF0\x9F\x95\x9A')
            CLOCK_FACE_TWELVE_OCLOCK = n(b'\xF0\x9F\x95\x9B')
    '''
        out = "Current Time\n"
        out += "Norway: " + str(localtime.strftime('%X %x %Z')) + "\n"
        out += "Baltimore: " + str(baltimoretime.strftime('%X %x %Z')) + "\n"
        out += "Denver: " + str(denvertime.strftime('%X %x %Z')) + "\n"
        Loggiz.log.write.info(out)
        bot.sendMessage(update.message.chat_id, text="{}".format(out), parse_mode="HTML")

class Stats(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)
        self.seen = self.dbobject.Get("seenlog")
        self.uindex = self.dbobject.Get("userindex")

    def run(self, bot, update, args):
        users = self.seen.usercounter.Get()
        data = users.rawdict()
        output_string = "Most Active User Stats (by words):\n\n"
        place = 1
        for key, user in sorted(data, key=self.sort_by_word, reverse=True):
            username = key
            if username == "":
                continue
            usercountobject = SerializableDict.UserObject(user)
            output_string += "[{}] {}: {} (Lines: {})\n".format(place, username, usercountobject.wordcounter, usercountobject.counter)
            place += 1
        Loggiz.log.write.info(output_string)
        bot.sendMessage(update.message.chat_id, text="{}".format(output_string), parse_mode="HTML")

    def sort_by_word(self, userdict):
        usercountobject = SerializableDict.UserObject(userdict[1])
        if not isinstance(usercountobject.wordcounter, int):
            return 1
        Loggiz.log.write.info(usercountobject.wordcounter)
        return usercountobject.wordcounter

class Help(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)

    def run(self, bot, update, args):
        output_string = "Available commands\n"
        output_string += commands
        ybot.sendMessage(update.message.chat_id, text="{}".format(output_string), parse_mode="HTML")

    @classmethod
    def sort_by_word(cls, userdict):
        usercountobject = SerializableDict.UserObject(userdict)
        if usercountobject.wordcounter == "":
            return 0
        return usercountobject.wordcounter

class AudioTips(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)
        self.tipdb = self.dbobject.Get("tipdb")

class Counter(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)
        self.seen = self.dbobject.Get("seenlog")

    def run(self, bot, update, args):
        user = self.seen.usercounter.Get()
        usercount = user.get(update.message.from_user.username)
        usercountobject = SerializableDict.UserObject(usercount)

        # Line counter
        if usercountobject.counter == "":
            usercountobject.counter = 1
        else:
            usercountobject.counter = usercountobject.counter + 1
        # Word counter
        currentwordcount = re.findall('\w+', update.message.text.lower())
        print("Words: {}".format(len(currentwordcount)))
        if usercountobject.wordcounter == "":
            usercountobject.wordcounter = len(currentwordcount)
        else:
            usercountobject.wordcounter = usercountobject.wordcounter + len(currentwordcount)
        # Last seen
        usercountobject.timestamp = str(datetime.datetime.now().replace(microsecond=0))
        # Metadata
        usercountobject.firstname = update.message.from_user.first_name
        usercountobject.lastname = update.message.from_user.last_name
        usercountobject.username = update.message.from_user.username
        # Store object to dictionary and back to DB
        user.set(update.message.from_user.username, usercountobject.SaveObject())
        self.seen.usercounter.Set(user)

class Seen(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)
        self.seendb = self.dbobject.Get("seenlog")

    def run(self, bot, update, args):
        Loggiz.log.write.info("Gettings Stats")
        user = self.seendb.usercounter.Get()

        if len(args) > 0:
            Loggiz.log.write.info("finding user {}".format(args[0]))
            username = self.stripusername(args[0])

            fetchseenuser = user.get(username)
            userseenobject = SerializableDict.UserObject(fetchseenuser)
            Loggiz.log.write.info(userseenobject.timestamp)
            if userseenobject.timestamp != "":
                bot.sendMessage(update.message.chat_id, text="hey! {} was last seen {} (lines/words: {}/{})".format(username, userseenobject.timestamp, userseenobject.counter, userseenobject.wordcounter))
            else:
                Loggiz.log.write.warn("Did not find any user info!")
        else:
            bot.sendMessage(update.message.chat_id, text="{} U ale wlong!! do like this!! command @<username>".format(telegram.Emoji.PILE_OF_POO))

class QuoteBase(GeneralMessageEvent):
    def __init__(self):
        GeneralMessageEvent.__init__(self)
        self.uindex = self.dbobject.Get("userindex")

class AddQuote(QuoteBase):
    def __init__(self):
        QuoteBase.__init__(self)

    def run(self, bot, update, args):
        new_quote_index = str(uuid.uuid4())

        if len(args) < 2:
            Loggiz.log.write.info("Argument length was {}".format(len(args)))
            bot.sendMessage(update.message.chat_id, text='[USAGE] <username> <quote>')
        else:
            username = self.stripusername(args[0])
            if username not in self.uindex.index.Get():
                tmplist = self.uindex.index.Get()
                tmplist.append(args[1])
                self.uindex.index.Set(tmplist)
                Loggiz.log.write.info("user/nick added to index")
            quotetext = StorageObjects.ComnodeObject("quotestext.{}".format(new_quote_index), "str", desc="", hidden=False)
            quotetext.Set(" ".join(args[1:]))

            quotemetausername = StorageObjects.ComnodeObject("quotemap.{}".format(username), "list", desc="", hidden=False)
            qmun = quotemetausername.Get()
            qmun.append(new_quote_index)
            quotemetausername.Set(qmun)
            bot.sendMessage(update.message.chat_id, text="Quote from {} added with id {}".format(username, new_quote_index))

class Quote(QuoteBase):
    def __init__(self):
        QuoteBase.__init__(self)
        self.taken = list()
        random.seed(calendar.timegm(time.gmtime()))

    def get_quote(self, username):
        quotemetausername = StorageObjects.ComnodeObject("quotemap.{}".format(username), "list", desc="", hidden=False)
        qmun = quotemetausername.Get()
        if len(qmun) > 0:
            random.seed(calendar.timegm(time.gmtime()))
            foundindex = random.randrange(0, len(qmun))
            Loggiz.log.write.info("found: {}, total: {}".format(foundindex, len(qmun)))
            if len(qmun) == foundindex:
                foundindex = foundindex - 1
            if qmun[foundindex] in self.taken:
                return "TAKEN"
            else:
                quotetext = StorageObjects.ComnodeObject("quotestext.{}".format(qmun[foundindex]), "str", desc="", hidden=False)
                self.taken.append(qmun[foundindex])
            return "{}: {}".format(username, quotetext.Get())
        else:
            return None

    def findrandomuser(self):
        userindexlength = len(self.uindex.index.Get())
        if userindexlength == 0:
            return
        luckyuser = random.randrange(0, userindexlength)
        if len(self.uindex.index.Get()) == luckyuser:
            luckyuser = luckyuser - 1
        return self.uindex.index.Get()[luckyuser]

    def run(self, bot, update, args):

        if len(args) == 1:
            quoteoutput = "{} random Quotes\n"
            nums = int(args[0])
            if nums > 10:
                nums = 10
            cnt = 0
            Loggiz.log.write.info("Args {}".format(str(args)))
            while True:
                currentquote = self.get_quote(self.findrandomuser())
                if currentquote == "TAKEN":
                    cnt -= 1
                    continue
                elif currentquote is None:
                    continue
                quoteoutput += "{} {}\n".format(telegram.emoji.CLOCK_FACE_ONE_OCLOCK, currentquote)
                if nums < cnt:
                    break
                cnt += 1
        else:
            quoteoutput = self.get_quote(self.findrandomuser())
        if quoteoutput is not None:
            Loggiz.log.write.info(str(self.taken))
            bot.sendMessage(update.message.chat_id, text=quoteoutput, parse_mode="HTML")

if __name__ == '__main__':
    pass<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import json

import re
from urllib import urlencode
import urllib
import urllib2
from urllib2 import HTTPCookieProcessor
from urllib2 import HTTPError
from urllib2 import ProxyHandler
from urllib2 import Request
from urllib2 import build_opener
from bs4 import BeautifulSoup
from datetime import datetime
import MySQLdb

default_header = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0',}

sites = [
     'http://www.pornhub.com/video/search?search=loira',
    # 'http://www.hellxx.com/index.php?module=videos&tag=d',
    # 'http://www.redtube.com/?search=s',
    # 'http://pt.xhamster.com/search.php?from=&new=&q=asasaa&qcat=video',
    # 'http://novinhasdozapzap.com/'
]

content = urllib2.urlopen(sites[0]).read()

soup = BeautifulSoup(content)
conn  = MySQLdb.connect("localhost","root","29367253","tocadasnovinhas" )
for video in soup.find("ul",{"class":"videos"}):
    img = video.find("img")
    time = video.find("var")
    video = video.find("a")
    if type(img) is not int:
        alt = img['alt']
        thumb = img['data-mediumthumb']
        href = video['href']
        tempo = video.contents[0]
        pornolandia = sites[0].split('/')[2]
        siteDetalhes = ''.join([pornolandia,href])
        sock = urllib.urlopen("http://"+siteDetalhes)
        htmlSource = sock.read()
        soup = BeautifulSoup(htmlSource)
        divDetalhes = soup.find("iframe",{"frameborder":"0"})
        print divDetalhes
        # for detalhes in divDetalhes:
        #     inputs = detalhes.find("input")
        #     embed =  inputs.get('value')
        #
        x = conn.cursor()
        try:
            x.execute("""INSERT INTO video (name,description,img,embed,category,tempo) VALUES (%s,%s,%s,%s,%s,%s)""",(alt.lower().replace(" ","-"),alt,thumb,divDetalhes,'loiras',tempo))
            conn.commit()
        except:
            conn.rollback()
<commit_msg>teste<commit_after># -*- coding: utf-8 -*-
import json

import re
from urllib import urlencode
import urllib
import urllib2
from urllib2 import HTTPCookieProcessor
from urllib2 import HTTPError
from urllib2 import ProxyHandler
from urllib2 import Request
from urllib2 import build_opener
from bs4 import BeautifulSoup
from datetime import datetime
import MySQLdb

default_header = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0',}

sites = [
     'http://www.pornhub.com/video/search?search=loira',
    # 'http://www.hellxx.com/index.php?module=videos&tag=d',
    # 'http://www.redtube.com/?search=s',
    # 'http://pt.xhamster.com/search.php?from=&new=&q=asasaa&qcat=video',
    # 'http://novinhasdozapzap.com/'
]

content = urllib2.urlopen(sites[0]).read()

soup = BeautifulSoup(content)
conn  = MySQLdb.connect("localhost","root","29367253","tocadasnovinhas" )
for video in soup.find("ul",{"class":"videos"}):
    img = video.find("img")
    time = video.find("var")
    video = video.find("a")
    if type(img) is not int:
        alt = img['alt']
        thumb = img['data-mediumthumb']
        href = video['href']
        tempo = video.contents[0]
        pornolandia = sites[0].split('/')[2]
        siteDetalhes = ''.join([pornolandia,href])
        sock = urllib.urlopen("http://"+siteDetalhes)
        htmlSource = sock.read()
        soup = BeautifulSoup(htmlSource)
        for i in soup.find("iframe",{"frameborder":"0"})
        print i
        # for detalhes in divDetalhes:
        #     inputs = detalhes.find("input")
        #     embed =  inputs.get('value')
        #
        x = conn.cursor()
        try:
            x.execute("""INSERT INTO video (name,description,img,embed,category,tempo) VALUES (%s,%s,%s,%s,%s,%s)""",(alt.lower().replace(" ","-"),alt,thumb,divDetalhes,'loiras',tempo))
            conn.commit()
        except:
            conn.rollback()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
CS Relief Map Tile Image Generator
"""

from urlparse import urlparse
import os
from math import cos, tan, radians, pi, log
from colorsys import hls_to_rgb

import requests
import numpy as np
from PIL import Image, ImageDraw, ImageChops
from matplotlib import pyplot as plt, cm, colors

import luigi

def deg_to_num(lat_deg, lon_deg, zoom):
    """
    degree to num
    """
    lat_rad = radians(lat_deg)
    n = 2.0 ** zoom
    xtile_f = (lon_deg + 180.0) / 360.0 * n
    ytile_f = (1.0 - log(tan(lat_rad) + (1 / cos(lat_rad))) / pi) / 2.0 * n
    xtile = int(xtile_f)
    ytile = int(ytile_f)
    pos_x = int((xtile_f - xtile) * 256)
    pos_y = int((ytile_f - ytile) * 256)
    return (xtile, ytile, pos_x, pos_y)

class DownloadTile(luigi.Task):
    """
    Download a Tile
    """
    baseUrl = luigi.Parameter()
    baseName = luigi.Parameter()
    x = luigi.IntParameter()
    y = luigi.IntParameter()
    z = luigi.IntParameter()

    def output(self):
        """
        define output
        """
        extension = os.path.splitext(urlparse(self.baseUrl).path)[
            1].replace(".", "")
        output_file = "./var/{}/{}/{}/{}.{}".format(
            self.baseName,
            self.z,
            self.x,
            self.y,
            extension
        )
        return luigi.LocalTarget(output_file)

    def run(self):
        """
        download a tile
        """
        url = self.baseUrl.format(**{"x": self.x, "y": self.y, "z": self.z})
        req = requests.get(url, stream=True)
        with self.output().open("wb") as f_out:
            for chunk in req.iter_content(chunk_size=1024):
                f_out.write(chunk)

class loadDem(DownloadTile):
    """
    標高タイル（基盤地図情報数値標高モデル）
    https://maps.gsi.go.jp/development/ichiran.html
    """
    baseName = "dem"
    baseUrl = None

    def __init__(self, *args, **kwargs):
        super(loadDem, self).__init__(*args, **kwargs)
        if self.z == 15:
            # 15
            self.baseUrl = "http://cyberjapandata.gsi.go.jp/xyz/dem5a/{z}/{x}/{y}.txt"
        elif 0 <= self.z <= 14:
            # 0-14
            self.baseUrl = "http://cyberjapandata.gsi.go.jp/xyz/dem/{z}/{x}/{y}.txt"
        else:
            raise

class calcDemSlope(luigi.Task):
    # http://www.spatialanalysisonline.com/HTML/index.html?profiles_and_curvature.htm
    x = luigi.IntParameter()
    y = luigi.IntParameter()
    z = luigi.IntParameter()
    xRange = luigi.ListParameter(default=[-1, 1000000000])
    yRange = luigi.ListParameter(default=[-1, 1000000000])
    folder_name = "demSlope"

    def requires(self):
        def chooseTask(x, y):
            if(x < min(self.xRange) or x >= max(self.xRange)):
                return None
            if(y < min(self.yRange) or y >= max(self.yRange)):
                return None
            return loadDem(x=x, y=y, z=self.z)
        self.taskList = [
            [chooseTask(x=self.x - 1, y=self.y - 1),
             chooseTask(x=self.x, y=self.y - 1),
             chooseTask(x=self.x + 1, y=self.y - 1)],
            [chooseTask(x=self.x - 1, y=self.y),
             chooseTask(x=self.x, y=self.y),
             chooseTask(x=self.x + 1, y=self.y)],
            [chooseTask(x=self.x - 1, y=self.y + 1),
             chooseTask(x=self.x, y=self.y + 1),
             chooseTask(x=self.x + 1, y=self.y + 1)]
        ]
        return filter(lambda x: x != None, self.taskList)

    def output(self):
        """
        define output
        """
        output_file = "./var/{}/{}/{}/{}.{}".format(
            self.folder_name,
            self.z,
            self.x,
            self.y,
            "txt"
        )
        return luigi.LocalTarget(output_file)

    def _load_tile_txt(self, f):
        tile = np.empty((256, 256))
        try:
            for i, line in enumerate(f):
                tile[i, :] = np.array(
                    [float(x) if x != "e" else np.NAN for x in line.strip().split(",")])
        except:
            tile[:] = np.NAN
        return tile

    def _combine_tiles(self):
        combinedTile = np.empty((256 * 3, 256 * 3))
        for i in range(3):
            for j in range(3):
                with self.taskList[i][j].output().open("r") as input_f:
                    tile = self._load_tile_txt(input_f)
                    combinedTile[256 * i:256 *
                                 (i + 1), 256 * j:256 * (j + 1)] = tile
        return combinedTile

    def run(self):
        combinedTile = self._combine_tiles()
        # print combinedTile
        grd = np.gradient(np.nan_to_num(combinedTile))[
            0][256:256 * 2, 256:256 * 2]
        with self.output().open("w") as output_f:
            np.savetxt(output_f, grd)

class calcDemCurvature(calcDemSlope):
    folder_name = "demCurvature"

    def run(self):
        # http://www.spatialanalysisonline.com/HTML/index.html?profiles_and_curvature.htm
        def dz_dx(a, x, y):
            return (a[x + 1, y] - a[x - 1, y]) / 2

        def dz_dy(a, x, y):
            return (a[x, y + 1] - a[x, y - 1]) / 2

        def dzdz_dxdy(a, x, y):
            return (a[x + 1, y + 1] - a[x - 1, y + 1] -
                    a[x + 1, y - 1] + a[x - 1, y - 1]) / (2 * 2)

        def dzdz_dxdx(a, x, y):
            return (a[x + 1, y] - 2 * a[x, y] + a[x - 1, y])

        def dzdz_dydy(a, x, y):
            return (a[x, y + 1] - 2 * a[x, y] + a[x, y - 1])

        def curvature(a, x, y):
            p = dz_dx(a, x, y)**2 + dz_dy(a, x, y)**2
            q = p + 1
            return (dzdz_dxdx(a, x, y) * (dz_dx(a, x, y)**2) + 2 * dzdz_dxdy(a, x, y) * dz_dx(a, x, y) * dz_dy(a, x, y) + dzdz_dydy(a, x, y) * (dz_dy(a, x, y)**2)) / (p * q**1.5 + 1)

        combined_tile = np.nan_to_num(self._combine_tiles())
        cur = np.empty((256, 256))
        for i in range(256):
            for j in range(256):
                cur[i, j] = curvature(combined_tile, 256 + i, 256 + j)

        with self.output().open("w") as output_f:
            np.savetxt(output_f, np.nan_to_num(cur))

class generateImageSlope(luigi.Task):
    x = luigi.IntParameter()
    y = luigi.IntParameter()
    z = luigi.IntParameter()
    folder_name = "demSlope"
    cmap_name = "Blues"
    cmap_range = [0, 70]
    abs_filter = True

    def __init__(self, *args, **kwargs):
        super(generateImageSlope, self).__init__(*args, **kwargs)
        self.cmapper = self.get_color_mapper()

    def get_color_mapper(self):
        cmapper = cm.ScalarMappable(norm=colors.SymLogNorm(linthresh=0.03, linscale=0.03,
            vmin=min(self.cmap_range), vmax=max(self.cmap_range), clip=True), cmap=plt.get_cmap(self.cmap_name))
        return cmapper

    def output(self):
        output_file = "./var/{}/{}/{}/{}.{}".format(
            self.folder_name,
            self.z,
            self.x,
            self.y,
            "png"
        )
        return luigi.LocalTarget(output_file)

    def requires(self):
        return calcDemSlope(x=self.x, y=self.y, z=self.z)

    def get_color(self, value):
        color = self.cmapper.to_rgba(value)
        return tuple([int(256 * x) for x in color])

    def run(self):
        size_x, size_y = (256, 256)

        with self.input().open("r") as input_f:
            data = np.loadtxt(input_f)

        if self.abs_filter:
            data = np.abs(data)

        img = Image.new('RGBA', (size_x, size_y), (0, 0, 0, 0))
        draw = ImageDraw.Draw(img)
        d_max = np.max(data)
        d_min = np.min(data)
        print (d_max, d_min)
        for img_y in range(0, size_y):
            for img_x in range(0, size_x):
                value = data[img_x, img_y]
                color = self.get_color(value)
                draw.rectangle(
                    ((img_x, img_y), (img_x + 1, img_y + 1)), fill=color)
        img.save(self.output().fn, 'PNG')

class generateImageCurvature(generateImageSlope):
    folder_name = "demCurvature"
    cmap_name = "Reds"
    cmap_range = [-150, 150]
    abs_filter = False

    def get_color_mapper(self):
        cmapper = cm.ScalarMappable(norm=colors.SymLogNorm(linthresh=0.015, linscale=0.03,
                                                           vmin=min(self.cmap_range), vmax=max(self.cmap_range), clip=True), cmap=plt.get_cmap(self.cmap_name))
        return cmapper

    def requires(self):
        return calcDemCurvature(x=self.x, y=self.y, z=self.z)

class generateImageCSReliefMap(luigi.Task):
    x = luigi.IntParameter()
    y = luigi.IntParameter()
    z = luigi.IntParameter()
    folder_name = "CSReliefMap"

    def requires(self):
        return [
            generateImageSlope(x=self.x, y=self.y, z=self.z),
            generateImageCurvature(x=self.x, y=self.y, z=self.z),
        ]

    def output(self):
        output_file = "./var/{}/{}/{}/{}.{}".format(
            self.folder_name,
            self.z,
            self.x,
            self.y,
            "png"
        )
        return luigi.LocalTarget(output_file)

    def run(self):
        size_x, size_y = (256, 256)

        output_img = Image.new('RGBA', (size_x, size_y), (255, 255, 255, 255))
        for source in self.input():
            input_img = Image.open(source.fn)
            output_img = ImageChops.multiply(output_img, input_img)

        with self.output().open("wb") as output_f:
            output_img.save(output_f, 'PNG')

class generateImageBounds(luigi.WrapperTask):
    """
    Schedule Download Tasks
    """
    west = luigi.FloatParameter()
    north = luigi.FloatParameter()
    south = luigi.FloatParameter()
    east = luigi.FloatParameter()
    zoom = luigi.IntParameter()
    targetTask = luigi.TaskParameter(default=generateImageCSReliefMap)

    def requires(self):
        """
        scheduling tasks
        """

        candidateTasks = [generateImageCSReliefMap,
                          generateImageCurvature, generateImageSlope]
        if not self.targetTask in candidateTasks:
            raise

        edge_nw_x, edge_nw_y, _, _ = deg_to_num(
            self.north, self.west, self.zoom)
        edge_se_x, edge_se_y, _, _ = deg_to_num(
            self.south, self.east, self.zoom)
        #xRange = [edge_nw_x, edge_se_x]
        #yRange = [edge_nw_y, edge_se_y]
        print deg_to_num(self.north, self.west, self.zoom) + deg_to_num(self.south, self.east, self.zoom)
        for tile_x in range(edge_nw_x, edge_se_x + 1):
            for tile_y in range(edge_nw_y, edge_se_y + 1):
                yield self.targetTask(x=tile_x, y=tile_y, z=self.zoom)

if __name__ == "__main__":
    luigi.run()
<commit_msg>fixed mirrored rendering result<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
CS Relief Map Tile Image Generator
"""

from urlparse import urlparse
import os
from math import cos, tan, radians, pi, log
from colorsys import hls_to_rgb

import requests
import numpy as np
from PIL import Image, ImageDraw, ImageChops
from matplotlib import pyplot as plt, cm, colors

import luigi

def deg_to_num(lat_deg, lon_deg, zoom):
    """
    degree to num
    """
    lat_rad = radians(lat_deg)
    n = 2.0 ** zoom
    xtile_f = (lon_deg + 180.0) / 360.0 * n
    ytile_f = (1.0 - log(tan(lat_rad) + (1 / cos(lat_rad))) / pi) / 2.0 * n
    xtile = int(xtile_f)
    ytile = int(ytile_f)
    pos_x = int((xtile_f - xtile) * 256)
    pos_y = int((ytile_f - ytile) * 256)
    return (xtile, ytile, pos_x, pos_y)

class DownloadTile(luigi.Task):
    """
    Download a Tile
    """
    baseUrl = luigi.Parameter()
    baseName = luigi.Parameter()
    x = luigi.IntParameter()
    y = luigi.IntParameter()
    z = luigi.IntParameter()

    def output(self):
        """
        define output
        """
        extension = os.path.splitext(urlparse(self.baseUrl).path)[
            1].replace(".", "")
        output_file = "./var/{}/{}/{}/{}.{}".format(
            self.baseName,
            self.z,
            self.x,
            self.y,
            extension
        )
        return luigi.LocalTarget(output_file)

    def run(self):
        """
        download a tile
        """
        url = self.baseUrl.format(**{"x": self.x, "y": self.y, "z": self.z})
        req = requests.get(url, stream=True)
        with self.output().open("wb") as f_out:
            for chunk in req.iter_content(chunk_size=1024):
                f_out.write(chunk)

class loadDem(DownloadTile):
    """
    標高タイル（基盤地図情報数値標高モデル）
    https://maps.gsi.go.jp/development/ichiran.html
    """
    baseName = "dem"
    baseUrl = None

    def __init__(self, *args, **kwargs):
        super(loadDem, self).__init__(*args, **kwargs)
        if self.z == 15:
            # 15
            self.baseUrl = "http://cyberjapandata.gsi.go.jp/xyz/dem5a/{z}/{x}/{y}.txt"
        elif 0 <= self.z <= 14:
            # 0-14
            self.baseUrl = "http://cyberjapandata.gsi.go.jp/xyz/dem/{z}/{x}/{y}.txt"
        else:
            raise

class calcDemSlope(luigi.Task):
    # http://www.spatialanalysisonline.com/HTML/index.html?profiles_and_curvature.htm
    x = luigi.IntParameter()
    y = luigi.IntParameter()
    z = luigi.IntParameter()
    xRange = luigi.ListParameter(default=[-1, 1000000000])
    yRange = luigi.ListParameter(default=[-1, 1000000000])
    folder_name = "demSlope"

    def requires(self):
        def chooseTask(x, y):
            if(x < min(self.xRange) or x >= max(self.xRange)):
                return None
            if(y < min(self.yRange) or y >= max(self.yRange)):
                return None
            return loadDem(x=x, y=y, z=self.z)
        self.taskList = [
            [chooseTask(x=self.x - 1, y=self.y - 1),
             chooseTask(x=self.x, y=self.y - 1),
             chooseTask(x=self.x + 1, y=self.y - 1)],
            [chooseTask(x=self.x - 1, y=self.y),
             chooseTask(x=self.x, y=self.y),
             chooseTask(x=self.x + 1, y=self.y)],
            [chooseTask(x=self.x - 1, y=self.y + 1),
             chooseTask(x=self.x, y=self.y + 1),
             chooseTask(x=self.x + 1, y=self.y + 1)]
        ]
        return filter(lambda x: x != None, self.taskList)

    def output(self):
        """
        define output
        """
        output_file = "./var/{}/{}/{}/{}.{}".format(
            self.folder_name,
            self.z,
            self.x,
            self.y,
            "txt"
        )
        return luigi.LocalTarget(output_file)

    def _load_tile_txt(self, f):
        tile = np.empty((256, 256))
        try:
            for i, line in enumerate(f):
                tile[i, :] = np.array(
                    [float(x) if x != "e" else np.NAN for x in line.strip().split(",")])
        except:
            tile[:] = np.NAN
        return tile

    def _combine_tiles(self):
        combinedTile = np.empty((256 * 3, 256 * 3))
        for i in range(3):
            for j in range(3):
                with self.taskList[i][j].output().open("r") as input_f:
                    tile = self._load_tile_txt(input_f)
                    combinedTile[256 * i:256 *
                                 (i + 1), 256 * j:256 * (j + 1)] = tile
        return combinedTile

    def run(self):
        combinedTile = self._combine_tiles()
        # print combinedTile
        grd = np.gradient(np.nan_to_num(combinedTile))[
            0][256:256 * 2, 256:256 * 2]
        with self.output().open("w") as output_f:
            np.savetxt(output_f, grd)

class calcDemCurvature(calcDemSlope):
    folder_name = "demCurvature"

    def run(self):
        # http://www.spatialanalysisonline.com/HTML/index.html?profiles_and_curvature.htm
        def dz_dx(a, x, y):
            return (a[x + 1, y] - a[x - 1, y]) / 2

        def dz_dy(a, x, y):
            return (a[x, y + 1] - a[x, y - 1]) / 2

        def dzdz_dxdy(a, x, y):
            return (a[x + 1, y + 1] - a[x - 1, y + 1] -
                    a[x + 1, y - 1] + a[x - 1, y - 1]) / (2 * 2)

        def dzdz_dxdx(a, x, y):
            return (a[x + 1, y] - 2 * a[x, y] + a[x - 1, y])

        def dzdz_dydy(a, x, y):
            return (a[x, y + 1] - 2 * a[x, y] + a[x, y - 1])

        def curvature(a, x, y):
            p = dz_dx(a, x, y)**2 + dz_dy(a, x, y)**2
            q = p + 1
            return (dzdz_dxdx(a, x, y) * (dz_dx(a, x, y)**2) + 2 * dzdz_dxdy(a, x, y) * dz_dx(a, x, y) * dz_dy(a, x, y) + dzdz_dydy(a, x, y) * (dz_dy(a, x, y)**2)) / (p * q**1.5 + 1)

        combined_tile = np.nan_to_num(self._combine_tiles())
        cur = np.empty((256, 256))
        for i in range(256):
            for j in range(256):
                cur[i, j] = curvature(combined_tile, 256 + i, 256 + j)

        with self.output().open("w") as output_f:
            np.savetxt(output_f, np.nan_to_num(cur))

class generateImageSlope(luigi.Task):
    x = luigi.IntParameter()
    y = luigi.IntParameter()
    z = luigi.IntParameter()
    folder_name = "demSlope"
    cmap_name = "Blues"
    cmap_range = [0, 70]
    abs_filter = True

    def __init__(self, *args, **kwargs):
        super(generateImageSlope, self).__init__(*args, **kwargs)
        self.cmapper = self.get_color_mapper()

    def get_color_mapper(self):
        cmapper = cm.ScalarMappable(norm=colors.SymLogNorm(linthresh=0.03, linscale=0.03,
                                                           vmin=min(self.cmap_range), vmax=max(self.cmap_range), clip=True), cmap=plt.get_cmap(self.cmap_name))
        return cmapper

    def output(self):
        output_file = "./var/{}/{}/{}/{}.{}".format(
            self.folder_name,
            self.z,
            self.x,
            self.y,
            "png"
        )
        return luigi.LocalTarget(output_file)

    def requires(self):
        return calcDemSlope(x=self.x, y=self.y, z=self.z)

    def get_color(self, value):
        color = self.cmapper.to_rgba(value)
        return tuple([int(256 * x) for x in color])

    def run(self):
        size_x, size_y = (256, 256)

        with self.input().open("r") as input_f:
            data = np.loadtxt(input_f)

        if self.abs_filter:
            data = np.abs(data)

        img = Image.new('RGBA', (size_x, size_y), (0, 0, 0, 0))
        draw = ImageDraw.Draw(img)
        d_max = np.max(data)
        d_min = np.min(data)
        print (d_max, d_min)
        for img_y in range(0, size_y):
            for img_x in range(0, size_x):
                value = data[size_x - 1 - img_x, size_y - 1 - img_y]
                color = self.get_color(value)
                draw.rectangle(
                    ((img_x, img_y), (img_x + 1, img_y + 1)), fill=color)
        img.save(self.output().fn, 'PNG')

class generateImageCurvature(generateImageSlope):
    folder_name = "demCurvature"
    cmap_name = "Reds"
    cmap_range = [-150, 150]
    abs_filter = False

    def get_color_mapper(self):
        cmapper = cm.ScalarMappable(norm=colors.SymLogNorm(linthresh=0.015, linscale=0.03,
                                                           vmin=min(self.cmap_range), vmax=max(self.cmap_range), clip=True), cmap=plt.get_cmap(self.cmap_name))
        return cmapper

    def requires(self):
        return calcDemCurvature(x=self.x, y=self.y, z=self.z)

class generateImageCSReliefMap(luigi.Task):
    x = luigi.IntParameter()
    y = luigi.IntParameter()
    z = luigi.IntParameter()
    folder_name = "CSReliefMap"

    def requires(self):
        return [
            generateImageSlope(x=self.x, y=self.y, z=self.z),
            generateImageCurvature(x=self.x, y=self.y, z=self.z),
        ]

    def output(self):
        output_file = "./var/{}/{}/{}/{}.{}".format(
            self.folder_name,
            self.z,
            self.x,
            self.y,
            "png"
        )
        return luigi.LocalTarget(output_file)

    def run(self):
        size_x, size_y = (256, 256)

        output_img = Image.new('RGBA', (size_x, size_y), (255, 255, 255, 255))
        for source in self.input():
            input_img = Image.open(source.fn)
            output_img = ImageChops.multiply(output_img, input_img)

        with self.output().open("wb") as output_f:
            output_img.save(output_f, 'PNG')

class generateImageBounds(luigi.WrapperTask):
    """
    Schedule Download Tasks
    """
    west = luigi.FloatParameter()
    north = luigi.FloatParameter()
    south = luigi.FloatParameter()
    east = luigi.FloatParameter()
    zoom = luigi.IntParameter()
    targetTask = luigi.TaskParameter(default=generateImageCSReliefMap)

    def requires(self):
        """
        scheduling tasks
        """

        candidateTasks = [generateImageCSReliefMap,
                          generateImageCurvature, generateImageSlope]
        if not self.targetTask in candidateTasks:
            raise

        edge_nw_x, edge_nw_y, _, _ = deg_to_num(
            self.north, self.west, self.zoom)
        edge_se_x, edge_se_y, _, _ = deg_to_num(
            self.south, self.east, self.zoom)
        #xRange = [edge_nw_x, edge_se_x]
        #yRange = [edge_nw_y, edge_se_y]
        print deg_to_num(self.north, self.west, self.zoom) + deg_to_num(self.south, self.east, self.zoom)
        for tile_x in range(edge_nw_x, edge_se_x + 1):
            for tile_y in range(edge_nw_y, edge_se_y + 1):
                yield self.targetTask(x=tile_x, y=tile_y, z=self.zoom)

if __name__ == "__main__":
    luigi.run()
<|endoftext|>


<commit_before>#-*- coding: utf-8 -*-
#@author: ilyass.tabiai@polymtl.ca
#@author: rolland.delorme@polymtl.ca
#@author: patrick.diehl@polymtl.ca

#from problem.pd import PD_problem
import numpy as np

class elastic_material_dic():

    def __init__(self, deck, problem):
        ## Scalar influence function
        self.w = deck.influence_function

        ## Weighted volume
        self.Weighted_Volume = problem.weighted_volume

        if deck.dim == 1:
            ## Young modulus of the material
            self.Young_Modulus = deck.young_modulus
        if deck.dim == 2:
            ## Bulk modulus of the material
            self.K = deck.bulk_modulus
            ## Shear modulus of the material
            self.Mu = deck.shear_modulus
            ## Poisson ratio of the material
            self.Nu = (3. * self.K - 2. * self.Mu) / (2. * (3. * self.K + self.Mu))

        if deck.dim == 3:
            ## Bulk modulus of the material
            self.K = deck.bulk_modulus
            ## Shear modulus of the material
            self.Mu = deck.shear_modulus
<commit_msg>Remove oldfiles<commit_after><|endoftext|>


<commit_before>#!/usr/bin/python

import pywikibot
import re
from datetime import date
import datetime
import logging
import sys

########
# Changing this to 1 makes your changes live on the report page, do not set to
# live mode unless you have been approved for bot usage. Do not merge commits
# where this is not default to 0
########
live = 0
########
# Style: 0 = with date sectionsl 1 = without
########
style = 0
########
# Version Number
########
version = '0.7.1'
########

'''
Copyright (c) 2016 Wugpodes

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''

def monthConvert(name):
    '''
    Takes in either the name of the month or the number of the month and returns
    the opposite. An input of str(July) would return int(7) while an input of
    int(6) would return str(June).
    Takes:   int OR string
    Returns: string OR int
    '''
    if type(name) is str:
        if name == "January": return 1
        elif name == "February": return 2
        elif name == "March": return 3
        elif name == "April": return 4
        elif name == "May": return 5
        elif name == "June": return 6
        elif name == "July": return 7
        elif name == "August": return 8
        elif name == "September": return 9
        elif name == "October": return 10
        elif name == "November": return 11
        elif name == "December": return 12
        else: raise ValueError
    elif type(name) is int:
        if name == 1:return('January')
        elif name == 2:return('February')
        elif name == 3:return('March')
        elif name == 4:return('April')
        elif name == 5:return('May')
        elif name == 6:return('June')
        elif name == 7:return('July')
        elif name == 8:return('August')
        elif name == 9:return('September')
        elif name == 10:return('October')
        elif name == 11: return('November')
        elif name == 12: return('December')
        else: raise ValueError

def checkPage(title):
    '''
    Takes the title of a DYK nom page and loads the wikitext. Looks for a DYK
    tick that shows it was approved or approved (AGF). If so it adds it to
    various lists so that it can be added to the approved page.
    Returns None, implicitly
    '''
    global dates
    global nonDate
    global entries
    global problem
    title = title.lstrip('{').rstrip('}')
    approved = computeNomStatus(title)
    if approved == -1:
        entries.pop()
    elif approved == 1:
        dates[-1][1].append('{{'+title+'}}')
        nonDate.append('{{'+title+'}}')
        entries.pop()
    else:
        pass

def computeNomStatus(link,status=0):
    link = link.lstrip('{').rstrip('}')
    if 'Template:' not in link:
        link = 'Template:'+link
    page = pywikibot.Page(site,link)
    if 'Please do not modify this page.' in page.text:
        status=-1
    else:
        for line in page.text.split('\n'):
            if '[[File:Symbol confirmed.svg|16px]]' in line \
            or '[[File:Symbol voting keep.svg|16px]]' in line:
                status = 1
            elif '[[File:Symbol question.svg|16px]]' in line \
            or '[[File:Symbol possible vote.svg|16px]]' in line \
            or '[[File:Symbol delete vote.svg|16px]]' in line \
            or '[[File:Symbol redirect vote 4.svg|16px]]' in line:
                status = 0
    return(status)

def mergeNominations(item = ''):
    '''
    Takes an index for the list dates and appends the entries in that section
    to the proper section on the approved page. Returns None, implicitly
    '''
    global approvedPageDates
    global dates
    if item == '':
        pass
    else:
        for entry in approvedPageDates:
            if dates[item][0] in entry:
                entry[1]+=dates[item][1][1:]

def computeYear(oMonth):
    '''
    Gives correct year over one year change, ie, for 11 months. Sections older
    than 11 months will not yield the correct results
    '''
    now = datetime.datetime.now()
    cMonth = now.month
    cYear = now.year
    if oMonth <= cMonth:
        year = cYear
    else:
        year = cYear - 1
    return(year)

def startLogging(loglevel):
    numeric_level = getattr(logging, loglevel.upper(), None)
    if numeric_level != None:
        logging.basicConfig(
            level=numeric_level,
            filename='Testing.log',
            format='%(asctime)s - %(levelname)s - %(message)s',
            datefmt='%Y-%m-%d %I:%M:%S %p'
        )
    else:
        logging.basicConfig(
            level=logging.WARNING,
            filename='./log/DYKMoverBot.log',
            format='%(asctime)s - %(levelname)s - %(message)s',
            datefmt='%Y-%m-%d %I:%M:%S %p'
        )
        logging.warning('Invalid log level \'%s\'. ' \
                       +'Defaulting to WARNING' % loglevel)

def checkArgs(arg):
    arg = arg.split('=')
    if arg[0] == '--log' or arg[0] == '-l':
        startLogging(arg[1])
    else:
        raise ValueError('Unknown command line argument \'%s\'' % arg[0])

# Start log
#for i in range(1,len(sys.argv)):
#    checkArgs(sys.argv[i])

logging.info("### Starting new run ###")
logging.info("live is set to %s" % live)
logging.info("style is set to %s" % style)
logging.info("DYKMoverBot version %s" % version)

# Load the various pages
logging.info("Loading pages")
site = pywikibot.Site('en', 'wikipedia')
nomPage      = pywikibot.Page(site,'Template talk:Did you know')
approvedPage = pywikibot.Page(site,'Template talk:Did you know/Approved')
#approvedPage = pywikibot.Page(site,'User:Wugapodes/DYKTest/Approved')

dateRegex = re.compile(r'on (.*?) (\d+)=')

DYKpage = nomPage.text.split('\n')
dates   = []
nonDate = []
entries = []
problem = []

# Get approved nominations on the DYK page
logging.info("Reading Nomination page")
for line in DYKpage:
    entries.append(line)
    if '==Articles' in line:
        matches=dateRegex.search(line)
        month = monthConvert(str(matches.group(1)))
        day = int(matches.group(2))
        dt = datetime.date(month=month,day=day,year=computeYear(month))
        section = line
        dates.append([dt,[section]])
    elif 'Did you know nominations/' in line and '<!--' not in line:
        if '}}{{' in line:
            logging.debug("Multiple nominations on one line.\n"\
                         +"Section: %s" % line)
            splitLine = line.split('}}{{')
            for title in splitLine:
                try:
                    checkPage(title)
                except Exception as e:
                    logging.error(
                        "Function checkPage did not complete successfully.\n"\
                        +str(e)+"\n"+title
                    )
                    logging.debug(line)
                    print(e)
                    problem.append([line,e])
                    continue
        else:
            line = line.split('}')[0]
            try:
                checkPage(line)
            except Exception as e:
                logging.error(
                    "Function checkPage did not complete successfully.\n"\
                    +str(e)+"\n"+line
                )
                print(e)
                problem.append([line,e])
                continue

approveddates=[]
for item in dates:
    if len(item[-1]) > 1:
        approveddates.append(item)

# Get the text on the approved page and then update the proper sections
# with entries newly approved
logging.info("Reading Approved page")
approvedPageText = approvedPage.text.split('\n')
sectionName = ''
approvedPageDates = []
oldLine = ''
datesToRemove = []
if style != 1:
    logging.info("Starting style 0 parsing")
    for line in approvedPageText:
        if '==Articles' in line:
            mergeNominations(oldLine)
            matches=dateRegex.search(line)
            month = monthConvert(str(matches.group(1)))
            day = int(matches.group(2))
            dt = datetime.date(month=month,day=day,year=computeYear(month))
            sectionName = line
            approvedPageDates.append([dt,[sectionName]])
            for item in approveddates:
                if dt in item:
                    approvedPageDates[-1][1]+=item[-1][1:]
                else:
                    continue
        elif 'Did you know nominations/' in line and '<!--' not in line:
            if '}}{{' in line:
                splitLine = line.split('}}{{')
                for title in splitLine:
                    if computeNomStatus(title) >= 0:
                        if '{{'+title+'}}' in approvedPageDates[-1][1]:
                            continue
                        try:
                            approvedPageDates[-1][1].append('{{'+title+'}}')
                        except Exception as e:
                            logging.warning("The approved page is empty?\n"+str(e))
            else:
                line = line.split('}')[0]
                title = line.split('{')[-1]
                if computeNomStatus(title) >= 0:
                    if '{{'+title+'}}' in approvedPageDates[-1][1]:
                        continue
                    try:
                        approvedPageDates[-1][1].append('{{'+title+'}}')
                    except Exception as e:
                        logging.warning("The approved page is empty?\n"+str(e))
        if '==Special occasion holding area==' in line:
            break
    adts = [x[0] for x in approvedPageDates]
    newSections = [x for x in dates if x[0] not in adts]
    approvedPageDates+=newSections
    toPrint=[]
    approvedPageDates.sort(key=lambda x: x[0])
    for entry in approvedPageDates:
        if len(entry[1]) > 1:
            toPrint+=entry[1]

# Create the page text to be output
logging.info("Creating output text")
passed = 0
approvedText = [
        "{{/top}}\n",
        "=Nominations=\n",
        "==Approved nominations==\n",
        "<!-- This section will hold approved nominations, with the templates "\
        +"transcluded in the same manner as the regular nominations page. "\
        +"While the exact format of the section has not yet been decided-while"\
        +" it seems unlikely that it will be by date, it may be divided into "\
        +"other sections-it is likely that the oldest approvals will go at the"\
        +" top and the most recent ones at the bottom of each section. -->\n"
    ]

approvedText.append('\n'.join(toPrint))
for line in approvedPage.text.split('\n'):
    if '==Special occasion holding area==' in line:
        passed = 1
        approvedText.append('\n'+line+'\n')
    elif passed == 1:
        approvedText.append(line+'\n')

# Determine if the bot should write to a live page or the test page. Defaults to
#     test page. Value of -1 tests backlog update (not standard because the file
#     size is very big).
if live == 1:
    abort = 0
    page = pywikibot.Page(site,'Template talk:Did you know/Approved')
    if page.text != approvedPage.text:
        abort = 1
        logging.error('Approved page has changed, aborting to avoid edit conflict')
        page = pywikibot.Page(site,'User talk:WugBot')
        page.text+='I ran into an edit conflict on [[WP:DYKN/A]]'\
            +'at ~~~~~ and did not write the page to avoid an edit conflict '\
            +'~~~~'
        page.save('Posting a note about failed run due to edit conflict')
    else:
        page.text=''.join(approvedText)
    nom = pywikibot.Page(site,'Template talk:Did you know')
    if nom.text != nomPage.text:
        abort = 1
        logging.error('Nom page has changed, aborting to avoid edit conflict')
        page = pywikibot.Page(site,'User talk:WugBot')
        page.text+='I ran into an edit conflict on [[WP:DYKN]]'\
            +'at ~~~~~ and did not write the page to avoid an edit conflict '\
            +'~~~~'
        page.save('Posting a note about failed run due to edit conflict')
    else:
        nom.text='\n'.join(entries)
    if abort == 0:
        nom.save(str(len(nonDate))+' approved nominations to [[/Approved|'\
              +'approved page]], WugBot v'+version)
        page.save('moving '+str(len(nonDate))+' tentatively approved nominations '\
              +'from [[WP:DYKN]], WugBot v'+version)
else:
    abort = 0
    page = pywikibot.Page(site,'Template talk:Did you know/Approved')
    if page.text != approvedPage.text:
        abort = 1
        logging.error('Approved page has changed, aborting to avoid edit conflict')
        page = pywikibot.Page(site,'User talk:WugBot')
        page.text+='During a test I ran into an edit conflict on [[WP:DYKN/A]]'\
            +'at ~~~~~ and did not write the page to avoid an edit conflict '\
            +'~~~~'
        page.save('Posting a note about failed test run due to edit conflict')
    else:
        aText=''.join(approvedText)
    nom = pywikibot.Page(site,'Template talk:Did you know')
    if nom.text != nomPage.text:
        abort = 1
        logging.error('Nom page has changed, aborting to avoid edit conflict')
        page = pywikibot.Page(site,'User talk:WugBot')
        page.text+='During a test I ran into an edit conflict on [[WP:DYKN]]'\
            +'at ~~~~~ and did not write the page to avoid an edit conflict '\
            +'~~~~'
        page.save('Posting a note about failed test run due to edit conflict')
    else:
        nText='\n'.join(entries)
    if abort == 0:
        page = pywikibot.Page(site,'User:Wugapodes/DYKTest')
        page.text=nText
        page.save(str(len(nonDate))+' approved nominations to [[/Approved|'\
              +'approved page]], WugBot v'+version)
        page = pywikibot.Page(site,'User:Wugapodes/DYKTest/Approved')
        page.text=aText
        page.save('moving '+str(len(nonDate))+' tentatively approved nominations '\
              +'from [[WP:DYKN]], WugBot v'+version)

print('Done')
<commit_msg>edit summary now says how many nominations were closed, v0.8.0<commit_after>#!/usr/bin/python

import pywikibot
import re
from datetime import date
import datetime
import logging
import sys

########
# Changing this to 1 makes your changes live on the report page, do not set to
# live mode unless you have been approved for bot usage. Do not merge commits
# where this is not default to 0
########
live = 0
########
# Style: 0 = with date sectionsl 1 = without
########
style = 0
########
# Version Number
########
version = '0.8.0'
########

'''
Copyright (c) 2016 Wugpodes

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''

def monthConvert(name):
    '''
    Takes in either the name of the month or the number of the month and returns
    the opposite. An input of str(July) would return int(7) while an input of
    int(6) would return str(June).
    Takes:   int OR string
    Returns: string OR int
    '''
    if type(name) is str:
        if name == "January": return 1
        elif name == "February": return 2
        elif name == "March": return 3
        elif name == "April": return 4
        elif name == "May": return 5
        elif name == "June": return 6
        elif name == "July": return 7
        elif name == "August": return 8
        elif name == "September": return 9
        elif name == "October": return 10
        elif name == "November": return 11
        elif name == "December": return 12
        else: raise ValueError
    elif type(name) is int:
        if name == 1:return('January')
        elif name == 2:return('February')
        elif name == 3:return('March')
        elif name == 4:return('April')
        elif name == 5:return('May')
        elif name == 6:return('June')
        elif name == 7:return('July')
        elif name == 8:return('August')
        elif name == 9:return('September')
        elif name == 10:return('October')
        elif name == 11: return('November')
        elif name == 12: return('December')
        else: raise ValueError

def checkPage(title):
    '''
    Takes the title of a DYK nom page and loads the wikitext. Looks for a DYK
    tick that shows it was approved or approved (AGF). If so it adds it to
    various lists so that it can be added to the approved page.
    Returns None, implicitly
    '''
    global dates
    global nonDate
    global entries
    global problem
    title = title.lstrip('{').rstrip('}')
    approved = computeNomStatus(title)
    if approved == -1:
        entries.pop()
    elif approved == 1:
        dates[-1][1].append('{{'+title+'}}')
        nonDate.append('{{'+title+'}}')
        entries.pop()
    else:
        pass

def computeNomStatus(link,status=0,pageName):
	global nclosed
	global aclosed
	global pageCnt
    link = link.lstrip('{').rstrip('}')
    if 'Template:' not in link:
        link = 'Template:'+link
    page = pywikibot.Page(site,link)
    if 'Please do not modify this page.' in page.text:
    	if pageCnt == 0:
    		nclosed+=1
    	else:
    		aclosed+=1
        status=-1
    else:
        for line in page.text.split('\n'):
            if '[[File:Symbol confirmed.svg|16px]]' in line \
            or '[[File:Symbol voting keep.svg|16px]]' in line:
                status = 1
            elif '[[File:Symbol question.svg|16px]]' in line \
            or '[[File:Symbol possible vote.svg|16px]]' in line \
            or '[[File:Symbol delete vote.svg|16px]]' in line \
            or '[[File:Symbol redirect vote 4.svg|16px]]' in line:
                status = 0
    return(status)

def mergeNominations(item = ''):
    '''
    Takes an index for the list dates and appends the entries in that section
    to the proper section on the approved page. Returns None, implicitly
    '''
    global approvedPageDates
    global dates
    if item == '':
        pass
    else:
        for entry in approvedPageDates:
            if dates[item][0] in entry:
                entry[1]+=dates[item][1][1:]

def computeYear(oMonth):
    '''
    Gives correct year over one year change, ie, for 11 months. Sections older
    than 11 months will not yield the correct results
    '''
    now = datetime.datetime.now()
    cMonth = now.month
    cYear = now.year
    if oMonth <= cMonth:
        year = cYear
    else:
        year = cYear - 1
    return(year)

def startLogging(loglevel):
    numeric_level = getattr(logging, loglevel.upper(), None)
    if numeric_level != None:
        logging.basicConfig(
            level=numeric_level,
            filename='Testing.log',
            format='%(asctime)s - %(levelname)s - %(message)s',
            datefmt='%Y-%m-%d %I:%M:%S %p'
        )
    else:
        logging.basicConfig(
            level=logging.WARNING,
            filename='./log/DYKMoverBot.log',
            format='%(asctime)s - %(levelname)s - %(message)s',
            datefmt='%Y-%m-%d %I:%M:%S %p'
        )
        logging.warning('Invalid log level \'%s\'. ' \
                       +'Defaulting to WARNING' % loglevel)

def checkArgs(arg):
    arg = arg.split('=')
    if arg[0] == '--log' or arg[0] == '-l':
        startLogging(arg[1])
    else:
        raise ValueError('Unknown command line argument \'%s\'' % arg[0])

def writeOut(p):
    global approvedPage
    global nomPage
    global entries
    global approvedText
    global nonDate
    global version
    abort = 0
    page = pywikibot.Page(site,'Template talk:Did you know/Approved')
    if page.text != approvedPage.text:
        abort = 1
        logging.error('Approved page has changed, aborting to avoid edit conflict')
        page = pywikibot.Page(site,'User talk:WugBot')
        page.text+='During a test I ran into an edit conflict on [[WP:DYKN/A]]'\
            +'at ~~~~~ and did not write the page to avoid an edit conflict '\
            +'~~~~'
        page.save('Posting a note about failed test run due to edit conflict')
    else:
        aText=''.join(approvedText)
    nom = pywikibot.Page(site,'Template talk:Did you know')
    if nom.text != nomPage.text:
        abort = 1
        logging.error('Nom page has changed, aborting to avoid edit conflict')
        page = pywikibot.Page(site,'User talk:WugBot')
        page.text+='During a test I ran into an edit conflict on [[WP:DYKN]]'\
            +'at ~~~~~ and did not write the page to avoid an edit conflict '\
            +'~~~~'
        page.save('Posting a note about failed test run due to edit conflict')
    else:
        nText='\n'.join(entries)
    if abort == 0:
        page = pywikibot.Page(site,p)
        page.text=nText
        page.save(str(len(nonDate))+' approved nominations to [[/Approved|'\
              +'approved page]], removing '+str(nclosed)+' closed nominations,'\
              +' WugBot v'+version)
        page = pywikibot.Page(site,p+'/Approved')
        page.text=aText
        page.save('moving '+str(len(nonDate))+' tentatively approved nominations '\
              +'from [[WP:DYKN]], removing '+str(aclosed)+' closed nominations,'\
              +' WugBot v'+version)

# Start log
#for i in range(1,len(sys.argv)):
#    checkArgs(sys.argv[i])

logging.info("### Starting new run ###")
logging.info("live is set to %s" % live)
logging.info("style is set to %s" % style)
logging.info("DYKMoverBot version %s" % version)

# Load the various pages
logging.info("Loading pages")
site = pywikibot.Site('en', 'wikipedia')
nomPage      = pywikibot.Page(site,'Template talk:Did you know')
approvedPage = pywikibot.Page(site,'Template talk:Did you know/Approved')
#approvedPage = pywikibot.Page(site,'User:Wugapodes/DYKTest/Approved')

dateRegex = re.compile(r'on (.*?) (\d+)=')

DYKpage = nomPage.text.split('\n')
dates   = []
nonDate = []
entries = []
problem = []
nclosed = 0
aclosed = 0
pageCnt = 0

# Get approved nominations on the DYK page
logging.info("Reading Nomination page")
for line in DYKpage:
    entries.append(line)
    if '==Articles' in line:
        matches=dateRegex.search(line)
        month = monthConvert(str(matches.group(1)))
        day = int(matches.group(2))
        dt = datetime.date(month=month,day=day,year=computeYear(month))
        section = line
        dates.append([dt,[section]])
    elif 'Did you know nominations/' in line and '<!--' not in line:
        if '}}{{' in line:
            logging.debug("Multiple nominations on one line.\n"\
                         +"Section: %s" % line)
            splitLine = line.split('}}{{')
            for title in splitLine:
                try:
                    checkPage(title)
                except Exception as e:
                    logging.error(
                        "Function checkPage did not complete successfully.\n"\
                        +str(e)+"\n"+title
                    )
                    logging.debug(line)
                    print(e)
                    problem.append([line,e])
                    continue
        else:
            line = line.split('}')[0]
            try:
                checkPage(line)
            except Exception as e:
                logging.error(
                    "Function checkPage did not complete successfully.\n"\
                    +str(e)+"\n"+line
                )
                print(e)
                problem.append([line,e])
                continue

approveddates=[]
for item in dates:
    if len(item[-1]) > 1:
        approveddates.append(item)

pageCnt = 1
# Get the text on the approved page and then update the proper sections
# with entries newly approved
logging.info("Reading Approved page")
approvedPageText = approvedPage.text.split('\n')
sectionName = ''
approvedPageDates = []
oldLine = ''
datesToRemove = []
if style != 1:
    logging.info("Starting style 0 parsing")
    for line in approvedPageText:
        if '==Articles' in line:
            mergeNominations(oldLine)
            matches=dateRegex.search(line)
            month = monthConvert(str(matches.group(1)))
            day = int(matches.group(2))
            dt = datetime.date(month=month,day=day,year=computeYear(month))
            sectionName = line
            approvedPageDates.append([dt,[sectionName]])
            for item in approveddates:
                if dt in item:
                    approvedPageDates[-1][1]+=item[-1][1:]
                else:
                    continue
        elif 'Did you know nominations/' in line and '<!--' not in line:
            if '}}{{' in line:
                splitLine = line.split('}}{{')
                for title in splitLine:
                    if computeNomStatus(title) >= 0:
                        if '{{'+title+'}}' in approvedPageDates[-1][1]:
                            continue
                        try:
                            approvedPageDates[-1][1].append('{{'+title+'}}')
                        except Exception as e:
                            logging.warning("The approved page is empty?\n"+str(e))
            else:
                line = line.split('}')[0]
                title = line.split('{')[-1]
                if computeNomStatus(title) >= 0:
                    if '{{'+title+'}}' in approvedPageDates[-1][1]:
                        continue
                    try:
                        approvedPageDates[-1][1].append('{{'+title+'}}')
                    except Exception as e:
                        logging.warning("The approved page is empty?\n"+str(e))
        if '==Special occasion holding area==' in line:
            break
    adts = [x[0] for x in approvedPageDates]
    newSections = [x for x in dates if x[0] not in adts]
    approvedPageDates+=newSections
    toPrint=[]
    approvedPageDates.sort(key=lambda x: x[0])
    for entry in approvedPageDates:
        if len(entry[1]) > 1:
            toPrint+=entry[1]

# Create the page text to be output
logging.info("Creating output text")
passed = 0
approvedText = [
        "{{/top}}\n",
        "=Nominations=\n",
        "==Approved nominations==\n",
        "<!-- This section will hold approved nominations, with the templates "\
        +"transcluded in the same manner as the regular nominations page. "\
        +"While the exact format of the section has not yet been decided-while"\
        +" it seems unlikely that it will be by date, it may be divided into "\
        +"other sections-it is likely that the oldest approvals will go at the"\
        +" top and the most recent ones at the bottom of each section. -->\n"
    ]

approvedText.append('\n'.join(toPrint))
for line in approvedPage.text.split('\n'):
    if '==Special occasion holding area==' in line:
        passed = 1
        approvedText.append('\n'+line+'\n')
    elif passed == 1:
        approvedText.append(line+'\n')

# Determine if the bot should write to a live page or the test page. Defaults to
#     test page. Value of -1 tests backlog update (not standard because the file
#     size is very big).
if live == 1:
    writeOut('Template talk:Did you know')
else:
    writeOut('User:Wugapodes/DYKTest')

print('Done')
<|endoftext|>


<commit_before>#!/usr/bin/env python

import time
import argparse
import pygame
from FaceTrain import Model
import cv2
import subprocess
import random

face_cascade = cv2.CascadeClassifier('opencv_config/haarcascade_frontalface_default.xml')
model = Model()
model.load()
alert_track = 1
sayHello=["Do you remember me", "I am trying to remember your name", "I think you are super star", "You looks great today", "I like your eyes", "God bless you", "Do you have free style"]

def detect_faces_from_picture(pic_file_path):
    print(">>> Let me check this picture: " + pic_file_path)
    frame = cv2.imread(pic_file_path)

    # Detect faces in the frame
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray_frame, 1.3, 5)
    #faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.2, minNeighbors=3, minSize=(10, 10))

    # Match the detected faces with the trained model
    if len(faces) > 0:
        print(">>> Someone is in the picture!")
        for (x, y, w, h) in faces:
            face = frame[y:y+h, x:x+w]
            result = model.predict(face)
            for index, name in model.getTrainCfg():
                if result == index:
                    print(">>> Aha, it's %s!" % name)

def detect_faces_from_camera_video_stream(exec_time=60):
    # Perform the detection every n seconds
    detect_interval = 2

    cap = cv2.VideoCapture(0)
    exec_start = time.time()
    interval_start = time.time()

    # For calculation of FPS (frame per second)
    frame_num = 0

    detected_name = "World"
    bossName = "Leo"
    alert_interval = 20
    alert_start = 0

    while True:
        if time.time() - exec_start > exec_time:
            break

        # Capture frame-by-frame
        _, frame = cap.read()
        frame_num += 1

        if time.time() - interval_start > detect_interval:
            interval_start = time.time()

            # Detect faces in the frame
            gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            faces = face_cascade.detectMultiScale(gray_frame, 1.3, 5)
            #faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.2, minNeighbors=3, minSize=(10, 10))

            # Match the detected faces with the trained model
            if len(faces) == 1:
                print(">>> Someone is out there!")
                #otherFace()
                isBoss = False
                for (x, y, w, h) in faces:
                    face = frame[y:y+h, x:x+w]

                    # Draw rectangles which point out the faces
                    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

                    label,probe = model.predict(face)
                    if probe > 0.95:
                        for index, name in model.getTrainCfg():
                            if label == index:
                                if name == detected_name:
                                    isSamePeople = True
                                else:
                                    isSamePeople = False

                                detected_name = name

                                if detected_name == bossName and time.time() - alert_start > alert_interval:
                                        alert_start = time.time()
                                        playAlert()

                                if not isSamePeople:
                                    print(">>> Aha, it's %s!" % name)
                                    #subprocess.Popen(["espeak", "hello {}, have a nice day".format(name)])
                                    subprocess.Popen(["flite", "-t", "hello {}, have a nice day".format(name)])
                                else:
                                    i = random.randint(0,len(sayHello)-1)
                                    #subprocess.Popen(["espeak", "{}".format(sayHello[i])])
                                    subprocess.Popen(["flite","-t", "{}".format(sayHello[i])])
                                break
            elif len(faces) > 1:
                print("Too many people here, I am going to die!")

        # Display the camero video
        cv2.putText(frame, "Press 'q' to quit", (20, 40),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
        cv2.putText(frame, ("FPS: %0.2f" % (frame_num / (time.time() - exec_start))),
                    (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
        cv2.putText(frame, ("Hello, %s!" % detected_name), (20, 120), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
        cv2.imshow('Camera', frame)

        # Wait for 'q' on the Camera window to quit before entire capturing job finished
        if cv2.waitKey(1) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

    cap.release()

def playAlert():
    global alert_track
    print(">>> Boss is comming, Alert!!!")
    print("\n")
    array=["./dialog/boss-ye.wav","./dialog/leo.wav", "./dialog/clark.wav"]
    pygame.mixer.init()
    # pygame.time.delay(2000)
    pygame.mixer.music.load(array[(alert_track%3) - 1])
    pygame.mixer.music.play()
    alert_track += 1

def otherFace():
    array=["Hello","How are you","Good luck"]
    randomValue = random.randint(0,len(array)-1)
    #subprocess.Popen(["espeak", "{}".format(array[randomValue])])
    subprocess.Popen(["flite","-t", "{}".format(array[randomValue])])

if __name__ == '__main__':
    # Parse the command line arguments
    parser = argparse.ArgumentParser(description='Detect faces from the camera video stream or given picutre')
    parser.add_argument('-p', type=str, help='the path of picture to detect faces')
    parser.add_argument('-t', type=int, help='the execution time to detect faces from the camera video stream. Default: 60 seconds')
    parser.set_defaults(t=60)
    args = parser.parse_args()

    # Start detecting
    if args.p == None:
        detect_faces_from_camera_video_stream(args.t)
    else:
        detect_faces_from_picture(args.p)
<commit_msg>add hello message<commit_after>#!/usr/bin/env python

import time
import argparse
import pygame
from FaceTrain import Model
import cv2
import subprocess
import random

face_cascade = cv2.CascadeClassifier('opencv_config/haarcascade_frontalface_default.xml')
model = Model()
model.load()
alert_track = 1
sayHello=["Do you remember me", "I am trying to remember your name", "I think you are super star", "You looks great today", "I like your eyes", "God bless you", "Do you have free style"]

def detect_faces_from_picture(pic_file_path):
    print(">>> Let me check this picture: " + pic_file_path)
    frame = cv2.imread(pic_file_path)

    # Detect faces in the frame
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray_frame, 1.3, 5)
    #faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.2, minNeighbors=3, minSize=(10, 10))

    # Match the detected faces with the trained model
    if len(faces) > 0:
        print(">>> Someone is in the picture!")
        for (x, y, w, h) in faces:
            face = frame[y:y+h, x:x+w]
            result = model.predict(face)
            for index, name in model.getTrainCfg():
                if result == index:
                    print(">>> Aha, it's %s!" % name)

def detect_faces_from_camera_video_stream(exec_time=60):
    # Perform the detection every n seconds
    detect_interval = 2

    cap = cv2.VideoCapture(0)
    exec_start = time.time()
    interval_start = time.time()

    # For calculation of FPS (frame per second)
    frame_num = 0

    detected_name = "World"
    bossName = "Leo"
    alert_interval = 20
    alert_start = 0

    while True:
        if time.time() - exec_start > exec_time:
            break

        # Capture frame-by-frame
        _, frame = cap.read()
        frame_num += 1

        if time.time() - interval_start > detect_interval:
            interval_start = time.time()

            # Detect faces in the frame
            gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            faces = face_cascade.detectMultiScale(gray_frame, 1.3, 5)
            #faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.2, minNeighbors=3, minSize=(10, 10))

            # Match the detected faces with the trained model
            if len(faces) == 1:
                print(">>> Someone is out there!")
                otherFace()
                isBoss = False
                for (x, y, w, h) in faces:
                    face = frame[y:y+h, x:x+w]

                    # Draw rectangles which point out the faces
                    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

                    label,probe = model.predict(face)
                    if probe > 0.95:
                        for index, name in model.getTrainCfg():
                            if label == index:
                                if name == detected_name:
                                    isSamePeople = True
                                else:
                                    isSamePeople = False

                                detected_name = name

                                if detected_name == bossName and time.time() - alert_start > alert_interval:
                                        alert_start = time.time()
                                        playAlert()

                                if not isSamePeople:
                                    print(">>> Aha, it's %s!" % name)
                                    #subprocess.Popen(["espeak", "hello {}, have a nice day".format(name)])
                                    subprocess.Popen(["flite", "-t", "{}".format(name)])
                                else:
                                    i = random.randint(0,len(sayHello)-1)
                                    #subprocess.Popen(["espeak", "{}".format(sayHello[i])])
                                    subprocess.Popen(["flite","-t", "{}".format(sayHello[i])])
                                break
            elif len(faces) > 1:
                print("Too many people here, I am going to die!")

        # Display the camero video
        cv2.putText(frame, "Press 'q' to quit", (20, 40),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
        cv2.putText(frame, ("FPS: %0.2f" % (frame_num / (time.time() - exec_start))),
                    (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
        cv2.putText(frame, ("Hello, %s!" % detected_name), (20, 120), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
        cv2.imshow('Camera', frame)

        # Wait for 'q' on the Camera window to quit before entire capturing job finished
        if cv2.waitKey(1) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

    cap.release()

def playAlert():
    global alert_track
    print(">>> Boss is comming, Alert!!!")
    print("\n")
    array=["./dialog/boss-ye.wav","./dialog/leo.wav", "./dialog/clark.wav"]
    pygame.mixer.init()
    # pygame.time.delay(2000)
    pygame.mixer.music.load(array[(alert_track%3) - 1])
    pygame.mixer.music.play()
    alert_track += 1

def otherFace():
    array=["Hello","How are you","Good luck", "Hahaha", "Good afternoon", "Hey hey", "Lu lu lu", "Sa wa di ka", "Ah yi xi", "Ka wa yi","kou ni qi wa"]
    randomValue = random.randint(0,len(array)-1)
    #subprocess.Popen(["espeak", "{}".format(array[randomValue])])
    subprocess.Popen(["flite","-t", "{}".format(array[randomValue])])

if __name__ == '__main__':
    # Parse the command line arguments
    parser = argparse.ArgumentParser(description='Detect faces from the camera video stream or given picutre')
    parser.add_argument('-p', type=str, help='the path of picture to detect faces')
    parser.add_argument('-t', type=int, help='the execution time to detect faces from the camera video stream. Default: 60 seconds')
    parser.set_defaults(t=60)
    args = parser.parse_args()

    # Start detecting
    if args.p == None:
        detect_faces_from_camera_video_stream(args.t)
    else:
        detect_faces_from_picture(args.p)
<|endoftext|>