Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104930 > unrolled thread

Fetch Gmail Archieved messages

Started byArshpreet Singh <arsh840@gmail.com>
First post2016-03-15 03:48 -0700
Last post2016-03-20 23:06 +0000
Articles 8 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Fetch Gmail Archieved messages Arshpreet Singh <arsh840@gmail.com> - 2016-03-15 03:48 -0700
    Re: Fetch Gmail Archieved messages Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-15 10:02 -0700
      Re: Fetch Gmail Archieved messages Arshpreet Singh <arsh840@gmail.com> - 2016-03-17 21:14 -0700
        Re: Fetch Gmail Archieved messages Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-17 22:44 -0700
          Re: Fetch Gmail Archieved messages Arshpreet Singh <arsh840@gmail.com> - 2016-03-18 07:19 -0700
            Re: Fetch Gmail Archieved messages Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-18 17:08 -0700
              Re: Fetch Gmail Archieved messages Arshpreet Singh <arsh840@gmail.com> - 2016-03-20 12:56 -0700
                Re: Fetch Gmail Archieved messages Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-20 23:06 +0000

#104930 — Fetch Gmail Archieved messages

FromArshpreet Singh <arsh840@gmail.com>
Date2016-03-15 03:48 -0700
SubjectFetch Gmail Archieved messages
Message-ID<2f0a6e59-a636-4c65-9181-df6de05035a6@googlegroups.com>
Hi, I am using imaplib to fetch Gmail's Inbox Archived message but results are not that much accurate. Here is code+logic:


def inbox_week():
    import imaplib
    EMAIL = 'myusername@gmail.com'
    PASSWORD = 'mypassword'
    mail = imaplib.IMAP4_SSL('imap.gmail.com')
    mail.login(  EMAIL, PASSWORD )
    mail = imaplib.IMAP4_SSL('imap.gmail.com')
    mail.select("[Gmail]/All Mail")
    interval = (date.today()-timedelta(d)).strftime("%d-%b-%Y")
    _, data = mail.uid('search', None,'(SENTSINCE{date})'.format(date=interval))
        
    for num in data[0].split():
        _, data = mail.uid('fetch', num, '(BODY.PEEK[])')
    
        for response_part in data:
            if isinstance(response_part, tuple):
                msg = email.message_from_string(response_part[1])
                for header in ['to']:
               
# This is logic for inbox-archieved messages
                    if (EMAIL in str(msg[header]) in str(msg[header])):

                        main_tuple = email.utils.parsedate_tz(msg['Date'])   
                                
                        yield main_tuple

I am not sure how to get the list of only Inbox-messages as well as Sentbox messages from [All-mail]. Do I need to any other library ?

[toc] | [next] | [standalone]


#104945

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2016-03-15 10:02 -0700
Message-ID<29c50bd3-9bdf-444f-891f-9d9d3e314eea@googlegroups.com>
In reply to#104930
On Tuesday, March 15, 2016 at 5:48:15 AM UTC-5, Arshpreet Singh wrote:

> def inbox_week():
>     import imaplib
>     EMAIL = 'myusername@gmail.com'

I admit that this is pedantic, but you should really use
ADDRESS instead of EMAIL. ADDRESS more correctly complements
PASSWORD. But in any event, you did do well by spelling them
as constants, which implicitly means: "Hey, don't mutate
these values!"

>     PASSWORD = 'mypassword'
>     mail = imaplib.IMAP4_SSL('imap.gmail.com')
>     mail.login(  EMAIL, PASSWORD )
>     mail = imaplib.IMAP4_SSL('imap.gmail.com')
>     mail.select("[Gmail]/All Mail")
>     interval = (date.today()-timedelta(d)).strftime("%d-%b-%Y")
>     _, data = mail.uid('search', None,'(SENTSINCE{date})'.format(date=interval))
>         
>     for num in data[0].split():
>         _, data = mail.uid('fetch', num, '(BODY.PEEK[])')
>     
>         for response_part in data:
>             if isinstance(response_part, tuple):
>                 msg = email.message_from_string(response_part[1])
>                 for header in ['to']:
>                
> # This is logic for inbox-archieved messages
>                     if (EMAIL in str(msg[header]) in str(msg[header])):

Is that last line doing what you think it's doing? Let's
break it down... Basically you have one condition, that is
composed of two main components:

    Component-1: EMAIL in str(msg[header])

and 

    Component-2: str(msg[header])
     

"Component-1" will return a Boolean. So in essence you're
asking:

    boolean = EMAIL in str(msg[header])
    if boolean in str(msg[header]):
        do_something()
     
Is that really what you wanted to do? I'm not sure how you
will ever find a Boolean in a string. Unless i've missed
something...? It will also help readability if you only 
applied str() to the value *ONCE*. But, maybe you don't 
even need the str() function???

[toc] | [prev] | [next] | [standalone]


#105175

FromArshpreet Singh <arsh840@gmail.com>
Date2016-03-17 21:14 -0700
Message-ID<85665d75-a26b-470b-a5fd-5e647898319c@googlegroups.com>
In reply to#104945
On Tuesday, 15 March 2016 22:32:42 UTC+5:30, Rick Johnson  wrote:

> Is that last line doing what you think it's doing? Let's
> break it down... Basically you have one condition, that is
> composed of two main components:
> 
>     Component-1: EMAIL in str(msg[header])
> 
> and 
> 
>     Component-2: str(msg[header])
>      
> 
> "Component-1" will return a Boolean. So in essence you're
> asking:
> 
>     boolean = EMAIL in str(msg[header])
>     if boolean in str(msg[header]):
>         do_something()
>      
> Is that really what you wanted to do? I'm not sure how you
> will ever find a Boolean in a string. Unless i've missed
> something...? 

Yes I am looking for in EMAIL string is present in  str(msg[header]) then do_something()

>It will also help readability if you only 
> applied str() to the value *ONCE*. But, maybe you don't 
> even need the str() function???

I also run without str() but sometimes it causes weird exception errors because you can't predict the behaviour msg[header].

[toc] | [prev] | [next] | [standalone]


#105180

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2016-03-17 22:44 -0700
Message-ID<016f02cd-13f1-4eef-8cb9-d682c72dad55@googlegroups.com>
In reply to#105175
On Thursday, March 17, 2016 at 11:14:48 PM UTC-5, Arshpreet Singh wrote:

> Yes I am looking for in EMAIL string is present in
> str(msg[header]) then do_something()
> 
> I also run without str() but sometimes it causes weird
> exception errors because you can't predict the behavior
> msg[header].

My suggestion is that you start over, you have too many
issues here.

  (1) Your importing a module from within a function, that's
  almost always a bad idea.
  
  (2) You spelling your local variable names like constants,
  i wouldn't do that.
  
  (3) You're repeating yourself too much.
  
  (4) You're logic is invalid, and appears to possibly have
  syntax errors.

Start from the beginning, and slowly add code "line-by-
line", checking for proper outputs along the way.
#
# BEGIN CODE
#
import imaplib

def inbox_week():
    emailAddress = '...@gmail.com'
    emailPassword = 'mypassword'
    # START ADDING CODE HERE
#
# END CODE
#

[toc] | [prev] | [next] | [standalone]


#105216

FromArshpreet Singh <arsh840@gmail.com>
Date2016-03-18 07:19 -0700
Message-ID<0d267f26-dc84-4678-9875-2d90164bfbbd@googlegroups.com>
In reply to#105180
On Friday, 18 March 2016 11:14:44 UTC+5:30, Rick Johnson  wrote:

> #
> # BEGIN CODE
> #
> import imaplib
> 
> def inbox_week():
>     emailAddress = '...@gmail.com'
>     emailPassword = 'mypassword'
>     # START ADDING CODE HERE
> #
> # END CODE
> #

Well I am asking for real help.(!suggestions)

[toc] | [prev] | [next] | [standalone]


#105248

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2016-03-18 17:08 -0700
Message-ID<7824f879-02e7-4047-af51-f6b43c98866f@googlegroups.com>
In reply to#105216
On Friday, March 18, 2016 at 9:19:34 AM UTC-5, Arshpreet Singh wrote:
> On Friday, 18 March 2016 11:14:44 UTC+5:30, Rick Johnson  wrote:
> 
> > #
> > # BEGIN CODE
> > #
> > import imaplib
> > 
> > def inbox_week():
> >     emailAddress = '...@gmail.com'
> >     emailPassword = 'mypassword'
> >     # START ADDING CODE HERE
> > #
> > # END CODE
> > #
> 
> Well I am asking for real help.(!suggestions)

I gave you "real help". 

What you want me to do -- write the code for you? Sorry, but Python-list is not a soup kitchen for destitute code. Neither is it a triage center were you can bring your sick code, drop it at the door, and say: "Here, fix this, i'm going to the bar".  

Where is your traceback? 

Does your code run without raising Errors?

Why did you create a loop, simply to iterate over a list-literal containing  one value?

At this point, i can't determine if you're trolling or serious...

[toc] | [prev] | [next] | [standalone]


#105307

FromArshpreet Singh <arsh840@gmail.com>
Date2016-03-20 12:56 -0700
Message-ID<6d3229aa-6ae8-4645-90e6-76e3861f8c30@googlegroups.com>
In reply to#105248
On Saturday, 19 March 2016 05:38:16 UTC+5:30, Rick Johnson  wrote:

> I gave you "real help". 
> 
> What you want me to do -- write the code for you? Sorry, but Python-list is not a soup kitchen for destitute code. Neither is it a triage center were you can bring your sick code, drop it at the door, and say: "Here, fix this, i'm going to the bar".  
> 
> Where is your traceback? 
> 
> Does your code run without raising Errors?
> 
> Why did you create a loop, simply to iterate over a list-literal containing  one value?
> 
> At this point, i can't determine if you're trolling or serious...

Thanks "Big boy"! I just asked for help not any kind of malfunction here. 

[toc] | [prev] | [next] | [standalone]


#105314

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2016-03-20 23:06 +0000
Message-ID<mailman.414.1458515232.12893.python-list@python.org>
In reply to#105307
On 20/03/2016 19:56, Arshpreet Singh wrote:
> On Saturday, 19 March 2016 05:38:16 UTC+5:30, Rick Johnson  wrote:
>
>> I gave you "real help".
>>
>> What you want me to do -- write the code for you? Sorry, but Python-list is not a soup kitchen for destitute code. Neither is it a triage center were you can bring your sick code, drop it at the door, and say: "Here, fix this, i'm going to the bar".
>>
>> Where is your traceback?
>>
>> Does your code run without raising Errors?
>>
>> Why did you create a loop, simply to iterate over a list-literal containing  one value?
>>
>> At this point, i can't determine if you're trolling or serious...
>
> Thanks "Big boy"! I just asked for help not any kind of malfunction here.
>

Please ignore "Ranting Rick", he's a well known troll who's just been 
slapped firmly down over on the Python ideas mailing list.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web