예제 #1
0
파일: mail.py 프로젝트: CrabbyPete/spot
def main():
    """ Checks mailbox and sends valid messages to the database
    """
    mailbox = MailBox()
    mailbox.login(settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD)
    
    # Set the cron job to send mail every week Mon at 12:00AM
    cron = pycron(latency = 240)
    jobs = cron.add_job(' 0 0 0 * * * ', 'fish','fish')
#    jobs = cron.add_job(' 0 * * * * * ', 'fish','anyfish')
    if not jobs:
        print "Cron job not added"
    else:
        print "Cron jobs"
        cron.print_jobs()
    # When this goes off string 'fish' will be passed to cron goes off @ 12:00AM
 
    
    # Continually poll
    while True:

        # Gets the mail list: If nothing there quit in order to clean up
        mail_msgs = mailbox.new_messages()
        if not mail_msgs or len(mail_msgs) == 0:
            mailbox.kill()
            time.sleep(30)
            jobs = cron.get_matched_jobs()
            cronjob( jobs )
            continue

        # Get each message
        for msg in mail_msgs:
            msg_num = mailbox.fetch(msg)
            em, sentby, group = mailbox.header(msg_num)

            # Check to see if you got an Undeliverable messages from the mail server
            if  em == "" or sentby == EMAIL:
                # Done with this email delete it
                mailbox.mark_deleted(msg)
                continue
   
            # See if this is a phone number
            pn = re_mobile.search(em)
            if pn != None:
                pn = pn.group(0)
            
                # Now look up sender in the database and add the message
                try:
                    profile = SpotUser.objects.get( mobile = pn )
                except Exception:
                    usr = None
                else:
                    usr = profile.user
                
            # No its just a regular email address
            elif em != None:
                print "Message from: "+em
                try:
                    usr = User.objects.get( email = em )
                except Exception:
                    usr = None
                else:
                    profile = usr.get_profile()
            
            # Make sure you have a user or dump the message
            if not usr:
                mailbox.mark_deleted(msg)
                continue


            # Save the text part of the message Decode quoted printable characters
            message = SpotMessage( user = usr )
    
            # Get the message parts
            text, html, media = email_parts( mailbox, msg_num, em )
            if text:
                message.text = text
            message.save()

            # Save each image, with this message.
            if media:
                for img in media:
                    
                    # Just get the file name and the relative url
                    path, title = split(img)
                    path = join('photos//', title)
                    photo = SpotPhoto( image = path, title = title )
                    
                    # Get the exif data before you adjust the photo
                    rotate = photo.set_exif()

                    # Resize the image and convert to jpeg
                    name = title.split('.')
                    img_type = name[-1].lower()
                    if img_type in PHOTO_TYPES:
                        ms = Image.open(img)
                        """ Check Orientation
                        1: 'Horizontal (normal)',
                        2: 'Mirrored horizontal',
                        3: 'Rotated 180',
                        4: 'Mirrored vertical',
                        5: 'Mirrored horizontal then rotated 90 CCW',
                        6: 'Rotated 90 CW',
                        7: 'Mirrored horizontal then rotated 90 CW',
                        8: 'Rotated 90 CCW'}),
                        """
                        if rotate != 0:
                            if '90' in rotate:
                                if 'CW' in rotate:
                                    ms = ms.rotate(270)
                                elif 'CCW' in rotate:
                                    ms = ms.rotate(90)
                            elif '180' in rotate:
                                ms = ms.rotate(180) 
                        
                        size = 400, 400
                        ms.thumbnail(size, Image.ANTIALIAS)
                        ms.save(img)

                    try:
                        photo.save()
                    except Exception, error:
                        err_msg = 'Photo %s failed to save because %s'% ( title, str(error) )
                        print err_msg
                        LOGGER.error(err_msg)
                    else:
                        message.photos.add(photo)
                        try:
                            message.save()
                        except Exception, error:
                            err_msg = 'Photo %s failed to save to message because %s'% (title, str(error)) 
                            print err_msg
                            LOGGER.error(err_msg)
    
            # Check if this is a group message
            send_to = []
            if group and SpotGroup.objects.filter(name = group):
                group = SpotGroup.objects.get(name = group)

                # If this is a member of the group save the message
                if group.is_member(usr):
                    message.group = group
                    message.save()
                    
                    members = group.get_members()
                    for member in members:
                        if member == usr:
                            continue
                        
                        follow = group.follows_by(member)
                        if follow == 'email':
                            send_to.append(member.email)
                        
                        elif follow == 'phone':
                            m_profile = member.get_profile()
                            send_to.append(m_profile.phone_addr)
                        
                        notice_send(usr, member, "message_for_group", {'group':group})

            # Find my friends and send them a notice and email, or MMS
            else:
                friends = Friendship.objects.friends_who_follow(usr)
                for person in friends:
                    friend = person['friend']
                    notice_send(usr, friend, "message_from_friend", {'user':friend})
                    f_profile = friend.get_profile()
                    if person['follow'] == 'email':
                        if friend.email != '':
                            send_to.append(friend.email)
                    if person['follow'] == 'phone':
                        f_profile = friend.get_profile()
                        if f_profile.phone_addr != '':
                            send_to.append(f_profile.phone_addr)

            # Send email to everyone who is following this one
            if len(send_to) > 0:
                subject = 'SpotBurn Message From %s %s'% ( usr.first_name, usr.last_name )
                mail_to( subject, text, EMAIL, send_to, media )

            # Done with this email delete it
            mailbox.mark_deleted(msg)
예제 #2
0
def get_restaurants():
    data = open("restaurants.txt")
    restaurants = []
    for line in data:
        restaurants.append(line)
    return restaurants


def make_selection(restaurants):
    return random.sample(restaurants, 1)


def send_mail(selection, config):
    message = MIMEText("Today we eat at %s" % selection)
    message["To"] = config.get("mail", "to")
    message["From"] = config.get("mail", "from")
    message["Subject"] = "Lunch time!"

    server = smtplib.SMTP(config.get("mail", "server"))
    server.sendmail("Lunchomaten", config.get("mail", "to"), message.as_string())
    server.quit()


if __name__ == "__main__":
    cron = pycron.pycron()
    cron.add_job("1 5 5 * * 1-5", main, "main job")
    while True:
        time.sleep(1)
        for match in cron.get_matched_jobs():
            match()