For now, I can use gmail api to get all UNREAD emails or all emails in INBOX.
GMAIL.users().messages().list(userId='me', labelIds=['UNREAD', 'INBOX']).execute()
Because getting all the emails could be annoying, I was wondering is it possible to get only the recent 10 UNREAD emails from gmail api?
Thanks for any hint that will allow me to do such thing.
The documentation tells us that we need to pass maxResults and set it to 10:
GMAIL.users().messages().list(userId='me', labelIds=['UNREAD'], maxResults=10).execute()
Go through this doc
You can query using the advanced search options.
You may use
newer_than:
Example: newer_than:2d
Search for messages older or newer than a time period using d (day), m (month), and y (year)
GMAIL.users().messages().list(
userId="me",
labelIds=["UNREAD", "INBOX"],
maxResults=10,
q="newer_than:6d"
).execute()
Related
Is it possible to use Python to extract timestamps from received email? I'm using the following code but to no avail:
messages = ap.Items
message = messages.GetNext()
receipt = message.ReceivedTime.date()
for i in messages:
print(receipt)
I only get one date published repeatedly for each email. (i.e., 2021-11-22, 2021-11-22, 2021-11-22.......)
Any help will be much appreciated.
Well, of course - you only retrieve ReceivedTime once:
for i in messages:
print(i.ReceivedTime.date())
I am trying to fetch the twitter dms since a specific message id. But it returns all the dms. can anyone help to get this done. I was trying with the following code snippet
since_id = '134902xxxxxxxxx'
while True:
#last_dms = tweepy.Cursor(api.list_direct_messages, max_id=since_id).items()
last_dms = api.list_direct_messages(10, max_id = since_id)
for messages in last_dms:
print(messages._json)
#since_id = last_dms[0]["id"]
print('since_id %d:', since_id)
time.sleep(30)
with the above code i am able to get all dms from the userid. need hel to get the dms since a specific message id.
The list_direct_messages does not support the max_id, see Twitter API reference which is the REST endpoint invoked by Tweepy.
You have 2 options:
delete the DM after it is processed (ie already replied), hence at the next call it won't be returned by Twitter
you need to implement the max_id concept yourself: at every iteration keep the DM id (after you reply) and ignore DMs which have a smaller ID. The inconvenience here is that you need to persist the ID (in a database for example) to ensure it is still available after a restart/redeployment
Getting the following error when trying to delete YouTube comments using Python. I can retrieve and set the moderation status of comments, but can't delete comments. Can't figure this out.
def deleteComments(service):
request = service.comments().delete(id="Ugzl8ec3rKxt6ClZlSR4AaABAg, 2CUswqQvx9q8MllybCuBF4AaABAg")
request.execute()
Error message:
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/youtube/v3/comments?id=Ugzl8ec3rKxt6ClZlSR4AaABAg%2CUswqQvx9q8MllybCuBF4AaABAg returned "The API server failed to successfully process the request. While this can be a transient error, it usually indicates that the request's input is invalid.">
Update:
The following code sample approved a comment, but generated an error message when trying to delete another comment. As suggested by #stvar, I added an exception to handle the error and retry the command after a brief delay to no avail. Not sure what else to do.
Code sample:
request = service.comments().setModerationStatus(
id="UgyVOfo6iFZPZ-lye9V4AaABAg",
moderationStatus=status
)
request.execute()
print("Approved comment "+"UgyVOfo6iFZPZ-lye9V4AaABAg")
i = 4
while True:
try:
request = service.comments().delete(
id="UgyVOfo6iFZPZ-lye9V4AaABAg"
)
request.execute()
print("Deleted comment "+"UgyVOfo6iFZPZ-lye9V4AaABAg")
break
except:
if (i>8):
break
time.sleep(i)
i=i+2
print("retry after 2 seconds")
Output:
Approved comment Ugys7LJAAAqXjruiM0h4AaABAg
retry after 2 seconds
retry after 2 seconds
retry after 2 seconds
I tried using curl, but was not successful because of issues getting an access token. I ended up just setting the moderation status of the comment to rejected. It's unfortunate that deleting comments using Python does not work for me. Maybe something is wrong on YouTube's end. Thank you.
Could it be that your comment ID
Ugzl8ec3rKxt6ClZlSR4AaABAg, 2CUswqQvx9q8MllybCuBF4AaABAg
is invalid for some reason? The IDs I recently saw were of the following format:
^U[a-zA-Z0-9_-]{25}(?:\.[a-zA-Z0-9_-]{22})?$
If you try to delete multiple comments in one shot, note that, according to the docs, the parameter id does not support a comma-separated list of IDs, as some other API endpoints do:
id (string)
The id parameter specifies the comment ID for the resource that is being deleted.
In such case, you'll have to issue separate calls to Comments.delete endpoint for each of your comments you need deleted.
Also note that the ID you pass to the API (the one I quoted above) is not the same as the one embedded within the error message you have shown:
Ugzl8ec3rKxt6ClZlSR4AaABAg,UswqQvx9q8MllybCuBF4AaABAg
This may indicate that the culprit for your error is not the API!
Note that you may remove only the comments that you've written yourself; that is that you cannot remove other people's comments.
Indeed this behavior of the API is not documented as such officially. But Google's staff mentioned that to be working as intended on Dec 6, 2016 00:24 UTC. A more recent mention is from Jun 15, 2019 22:33 UTC.
am wondering which of the reports from https://developers.google.com/adwords/api/docs/appendix/reports/ I should query to get conversion_name, count, value and gclid?
Doubleclick search has https://developers.google.com/doubleclick-search/v2/reference/conversion/get , cannot find something similar for Adwords API.
Thanks a lot in advance!
I sense this answer may be a little a disappointing! I do not think its possible to get a report with gclids and conversions. There is a report 'click_performance_report' which contains the gclid for each click, however there are no columns for conversions. Your best bet is to use a third party tracking tool to log gclids and conversion names/values.
Conversion data is only available within the following reports:
ACCOUNT_PERFORMANCE_REPORT
ADGROUP_PERFORMANCE_REPORT
AD_CUSTOMIZERS_FEED_ITEM_REPORT
AD_PERFORMANCE_REPORT
AGE_RANGE_PERFORMANCE_REPORT
AUDIENCE_PERFORMANCE_REPORT
AUTOMATIC_PLACEMENTS_PERFORMANCE_REPORT
BID_GOAL_PERFORMANCE_REPORT
BUDGET_PERFORMANCE_REPORT
CAMPAIGN_AD_SCHEDULE_TARGET_REPORT
CAMPAIGN_GROUP_PERFORMANCE_REPORT
CAMPAIGN_LOCATION_TARGET_REPORT
CAMPAIGN_PERFORMANCE_REPORT
CRITERIA_PERFORMANCE_REPORT
DESTINATION_URL_REPORT
DISPLAY_KEYWORD_PERFORMANCE_REPORT
DISPLAY_TOPICS_PERFORMANCE_REPORT
FINAL_URL_REPORT
GENDER_PERFORMANCE_REPORT
GEO_PERFORMANCE_REPORT
KEYWORDLESS_CATEGORY_REPORT
KEYWORDLESS_QUERY_REPORT
KEYWORDS_PERFORMANCE_REPORT
PARENTAL_STATUS_PERFORMANCE_REPORT
PLACEHOLDER_FEED_ITEM_REPORT
PLACEHOLDER_REPORT
PLACEMENT_PERFORMANCE_REPORT
PRODUCT_PARTITION_REPORT
SEARCH_QUERY_PERFORMANCE_REPORT
SHOPPING_PERFORMANCE_REPORT
URL_PERFORMANCE_REPORT
USER_AD_DISTANCE_REPORT
VIDEO_PERFORMANCE_REPORT
I am connecting to my gmail account via IMAP to sync some of my emails and parse them. Sometimes I need to download again some emails because I did some kind of fix and now gmail is not returning me the uids of those emails in any way, here is some code to explain myself better:
typ, data = self.connection.uid('search', None, '(SINCE 14-Dec-2012 BEFORE 20-Dec-2012)')
17:05.55 > HJBM3 UID SEARCH (SINCE 14-Dec-2012 BEFORE 20-Dec-2012)
17:05.69 < * SEARCH
17:05.69 < HJBM3 OK SEARCH completed (Success)
('OK', [''])
I have a good bunch of emails on those dates including the ones I want to parse and it doesn't return anything, depending on the date it does return some uids so is not completely broken.
I decided to try if thunderbird synced correctly those emails and it got them no problem.
I am using the python 2.6 imaplib (version 2.58)
Maybe this will help someone so I'll answer it here:
I had in gmail this setting on:
When I changed it to "Do not limit" It worked like a charm.