Python: how to click on an opened outlook email (win32) - python

I need to click on an opened outlook email, a specific approve link that says "Approve request".
I opened the wanted email correctly, but I can't click on the specific link.
Here is the code:
import win32com.client
outlook=win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox=outlook.GetDefaultFolder(6)
messages = inbox.Items
messages.Sort("[ReceivedTime]", Descending=True)
for i in range(100):
message = messages.GetNext()
print(""+message.Subject, str(message.ReceivedTime))
if message.Subject == "Mail to approve request":
message.Display(False)
else:
pass

You will need to parse the MailItem.HTMLBody property, extract the relevant link, launch the browser with that link.
Also, never loop through all items in a folder, use Items.Find/FindNext or Items.Restrict with a query like "[Subject]='Mail to approve request'"

There are several ways to open a hyperlink programmatically in Python. The How to open a URL in python page explains possible scenarios. For example:
import os
os.system("start \"\" https://example.com")
Use the HTMLBody property which returns the a string representing the HTML body of the specified item. So, you may find the required URL programmatically by parsing the message body and execute it programmatically.
And, finally, to find items that correspond to your conditions use the Find/FindNext or Restrict methods of the Items class. Read more about them in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder
See Filtering Items Using Query Keywords for building a search criteria string properly.
Also you may find the AdvancedSearch method of the Application class helpful. The key benefits of using the AdvancedSearch method in Outlook are:
The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in MSDN. To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
You can stop the search process at any moment using the Stop method of the Search class.
Read more about this method in the Advanced search in Outlook programmatically: C#, VB.NET article.

Related

Scraping information from Outlook email folder

Python newbie here...
I'm attempting to pull information from multiple emails within a folder in Outlook.
Everyday an email containing a table of information is sent to the mailbox and is autofiled into a folder. My aim is to pull the information from the table in these emails for the last 6 months and present this in a pandas dataframe.
I have no idea how to scrape this information from an email and would appreciate any help.
Thanks!!
It seems you need to automate Outlook to get the required information. To get all items for the last 6 months you need to use the Find/FindNext or Restrict methods of the Items class. Read more about these methods in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder

How to get the Facebook Public Page Content Access just to extract data?

For a project at university I need to extract data such as posts and reviews from same Facebook pages. Everything was fine couple of months ago but now to get data from pages you need the Public Page Content Access.
In order to get my app reviewed I need to add:
A platform where I'd use the app
A screencast that shows "how a person sees this feature used in your app"
An explanation of how I'd be using Page Public Content Access to enhance the experience of my app.
Privacy Policy URL
As a student who just needs to extract some data for an exam I don't have any website/platform where I'd use the app. I'm using the Facebook Graph API on Python.
I looked on this website for a Privacy Policy Generator but I don't have any website nor mobile apps where I'd use the API...
Is there some way for my situation to extract data by API without this requirements or it's better for me to find other solutions, such as web scraping?
To be able to extract data from Facebook using a python code you need to register as a developer on Facebook and then have an access token. Here are the steps for it.
Go to link developers.facebook.com, create an account there. Go to
link developers.facebook.com/tools/explorer. Go to “My apps” drop
down in the top right corner and select “add a new app”. Choose a
display name and a category and then “Create App ID”. Again get back
to the same link developers.facebook.com/tools/explorer. You will see
“Graph API Explorer” below “My Apps” in the top right corner. From
“Graph API Explorer” drop down, select your app. Then, select “Get
Token”. From this drop down, select “Get User Access Token”. Select
permissions from the menu that appears and then select “Get Access
Token.” Go to link developers.facebook.com/tools/accesstoken. Select
“Debug” corresponding to “User Token”. Go to “Extend Token Access”.
This will ensure that your token does not expire every two hours.
Python Code to Access Facebook Public Data:
Go to link https://developers.facebook.com/docs/graph-api if want to collect data on anything that is available publicly. See https://developers.facebook.com/docs/graph-api/reference/v2.7/. From this documentation, choose any field you want from which you want to extract data such as “groups” or “pages” etc. Go to examples of codes after having selected these and then select “facebook graph api” and you will get hints on how to extract information. This blog is primarily on getting events data.
First of all, import ‘urllib3’, ‘facebook’, ‘requests’ if they are already available. If not, download these libraries. Define a variable token and set its value to what you got above as “User Access Token”.
token= ‘aiufniqaefncqiuhfencioaeusKJBNfljabicnlkjshniuwnscslkjjndfi’
Getting list of Events:
Now to find information on events for any search term say “Poetry” and limiting those events’ number to 10000:
graph = facebook.GraphAPI(access_token=token, version = 2.7)
events = graph.request(‘/search?q=Poetry&type=event&limit=10000’)
This will give a dictionary of all the events that have been created on Facebook and has string “Poetry” in its name. To get the list of events, do:
eventList = events[‘data’]
Extracting all information for a event from the list of events extracted above:
Get the EventID of the first event in the list by
eventid = eventList[1][‘id’]
For this EventID, get all information and set few variables which will be used later by:
event1=graph.get_object(id=eventid,fields=’attending_count,can_guests_invite,category,cover,declined_count,description,end_time,guest_list_enabled,interested_count,is_canceled,is_page_owned,is_viewer_admin,maybe_count,noreply_count,owner,parent_group,place,ticket_uri,timezone,type,updated_time’)
attenderscount = event1[‘attending_count’]
declinerscount = event1[‘declined_count’]
interestedcount = event1[‘interested_count’]
maybecount = event1[‘maybe_count’]
noreplycount = event1[‘noreply_count’]
Getting the list of all those who are attending an event and converting the response into json format:
attenders = requests.get(“https://graph.facebook.com/v2.7/"+eventid+"/attending?
access_token="+token+”&limit=”+str(attenderscount))
attenders_json = attenders.json()
Getting the admins of the event:
admins = requests.get(“https://graph.facebook.com/v2.7/"+eventid+"/admins?
access_token="+token)
admins_json = admins.json()
And similarly you can extract other information such as photos/videos/feed of that event if you want.
Go to https://developers.facebook.com/docs/graph-api/reference/event/ and see “Edges” part in the documentation.

Python - Get Result of Google Search

My goal is to create a small sript that find all the result of a google search but in "raw".
I don't speak english very well so i prefer to give an exemple to show you what i would like :
I Type : elephant
The script return
www.elephant.com
www.bluelephant.com
www.ebay.com/elephant
.....
I was thinking about urllib.request but the return value will not be usable to that !
I found some tutorials but not adapted at all to my wish !
Like i told you my goal is to have an .txt file as output witch contains alls the website who match with my query !
Thanks all
One simple way is to make a request to google search, then parse the html result. You can use some Python libraries such us Beautiful Soup to parse the html content easily, finally get the url link you need.
These seem to change often, so hopefully this answer remains useful for a little while...
First, you'll need to create a Google Custom Search, by visiting their site or following the instructions provided here https://developers.google.com/custom-search/docs/tutorial/creatingcse.
This will provide you with both
Custom Search Engine ID
API Key
credentials which are needed to use the service.
In your python script, you'll want to import the following package:
from googleapiclient.discovery import build
which will enable you to create a build object:
service = build("customsearch", developerKey=my_api_key)
According to the docs, this constructs a resource for interacting with the API.
When you want to return search results, call execute() on service's cse().list() method:
res = service.cse().list(q=my_search_keyword, cx=my_cse_id, **kwargs).execute()
to return a list of search results, where each result is a dictionary object. The i'th result's URL can be accessed with the "link" key:
ithresult = res[i]['link']
Note that you can only return 10 results in a single call, so make use of the start keyword argument in .list(), and consider embedding this call in a loop to generate several links at a time.
You should be able to find plenty of SO answers about saving your search results to a text file.
N.B. One more thing that confused me at first - presumably you'll want to search the entire web, and not just a single site. But when creating your CSE you will be asked to specify a single site, or list of sites, to search. Don't worry, just enter any old thing, you can delete it later. Even Google endorses this hack:
Convert a search engine to search the entire web: On the Custom Search
home page, click the search engine you want. Click Setup, and then
click the Basics tab. Select Search the entire web but emphasize
included sites. In the Sites to search section, delete the site you
entered during the initial setup process.
I just add 2 points to "9th Dimension" answer.
Use this guide to find your Custom Search Engine ID
A small modification should be made in the second line of the code: as the following, the "version" should be added as an argument
service = build('customsearch','v1',developerKey= my_api_key)
You have 2 options - using API or make a request like a browser does and then parse HTML.
First option is rather tricky to set up and is limited - 100 free queries/day, then 1000 for $5.
Second option is easier but it violates Google's ToS.

Using Python To Access E-mail (Lotus Notes)

Basically, I need to write a Python script that can download all of the attachment files in an e-mail, and then organize them based on their name. I am new to using Python to interact with other applications, so I was wondering if you had any pointers regarding this, mainly how to create a link to Lotus Notes (API)?
You can do this in LotusScript as an export of data. This could be an agent that walks down a view in Notes, selects a document, document attachments could be put into a directory. Then with those objects in the directory(ies) you can run any script you like like a shell script or whatever.
With LotusScript you can grab out meta data or other meaningful text for your directory name. Detach the objects you want from richtext then move to the next document. The view that you travel down will effect the type of documents that you are working with.

Check if a MediaWiki page exists (Python)

I'm working on a Python script that transforms this:
foo
bar
Into this:
[[Component foo]]
[[bar]]
The script checks (per input line) if the page "Component foo" exists. If it exists then a link to that page is created, if it doesn't exist then a direct link is created.
The problem is that I need a quick & cheap way to check if a lot of wiki pages exist.I don't want to (try to) download all the 'Component' pages.
I already figured out a fast way to do this by hand: Edit a new wiki page. paste all the 'component' links into the page, press preview, and then save the resulting preview HTML page. The resulting HTML file contains a different link for existing pages than for non-existing pages.
So to rephrase my question: How can I save a mediawiki preview page in Python?
(I don't have local access to the database.)
You can definitely use the API to check if a page exists:
# assuming words is a list of words you wish to query for
import urllib
# replace en.wikipedia.org with the address of the wiki you want to access
query = "http://en.wikipedia.org/w/api.php?action=query&titles=%s&format=xml" % "|".join(words)
pages = urllib.urlopen(query)
Now pages you will contain xml like this:
<?xml version="1.0"?><api><query><pages>
<page ns="0" title="DOESNOTEXIST" missing="" />
<page pageid="600799" ns="0" title="FOO" />
<page pageid="11178" ns="0" title="Foobar" />
</pages></query></api>
Pages which don't exist will appear here but they have the missing="" attribute set, as can be seen above. You can also check for the invalid attribute to be on the save side.
Now you can use your favorite xml parser to check for these attributes and react accordingly.
See also: http://www.mediawiki.org/wiki/API:Query
Use Pywikibot to interact with the MediaWiki software. It's probably the most powerful bot framework available.
The Python Wikipediabot Framework (pywikipedia or PyWikipediaBot) is a
collection of tools that automate work on MediaWiki sites. Originally
designed for Wikipedia, it is now used throughout the Wikimedia
Foundation's projects and on many other MediaWiki wikis. It's written
in Python, which is a free, cross-platform programming language. This
page provides links to general information for people who want to use
the bot software.
If you have local access to the wiki database, it might be easiest to do a query against the database to see whether each page exists.
If you only have HTTP access, you might try the mechanize library which lets you programmatically automate tasks that would otherwise require a browser.
You should be able to use the MediaWiki API.
http://www.mediawiki.org/wiki/API (maybe under Queries or Creating/Editing)
I'm not too familiar with it, but for example, you could compare the output of an existing page with a nonexistent page.
http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Bill_Gates&rvprop=timestamp
http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=NONEXISTENT_PAGE&rvprop=timestamp

Categories