What is the credentials.txt format used by selenium? - python

The README for the project at https://github.com/apurvmishra99/facebook-scraper-selenium includes the instruction:
Store your email and password for Facebook login in credentials.txt
This project doesn't contain any code that tries to read or parse credentials.txt, so I assume that the format has to be standard to selenium or to Python.
What exactly is the format this file should be in?

It's not a selenium or python thing. That file was deleted during one of the developer's commits:
I've had a look for you, and if you look in this file in the project: https://github.com/apurvmishra99/facebook-scraper-selenium/blob/master/fb-scraper/settings.py
You can see it gets the email and password:
# User credentials
EMAIL = os.getenv("EMAIL")
PASSWORD = os.getenv("PASSWORD")
os.getenv is python, and does:
os.getenv(key, default=None) Return the value of the environment
variable key if it exists, or default if it doesn’t. key, default and
the result are str.
What you can try is create environment variables called "EMAIL" and "PASSWORD" set the respectively and then run the main.py
Also be aware that in the same settings files the binaries are set as so:
# Required binaries
BROWSER_EXE = '/usr/bin/firefox'
GECKODRIVER = '/usr/bin/geckodriver'
FIREFOX_BINARY = FirefoxBinary(BROWSER_EXE)
You'll need to ensure these reflect your system.

Related

How to authenticate proxy with username and password in Selenium using Python

I am trying to authenticate proxy with username and password in Selenium using Python but the current code is not working. I have tried many solutions but none of them worked.
Proxy example,
IP = xxx.xx.xx.xx
PORT = xxxxx
USERNAME = USERNAME
PASSWORD = PASSWORD
I have used the following code,
driver.execute_script("""
Services.prefs.setIntPref('network.proxy.type', 1);
Services.prefs.setCharPref("network.proxy.http", arguments[0]);
Services.prefs.setIntPref("network.proxy.http_port", arguments[1]);
Services.prefs.setCharPref("network.proxy.ssl", arguments[0]);
Services.prefs.setIntPref("network.proxy.ssl_port", arguments[1]);
Services.prefs.setCharPref('network.proxy.socks', arguments[4]);
Services.prefs.setIntPref('network.proxy.socks_port', arguments[5]);
Services.prefs.setCharPref('network.proxy.socks_username', arguments[6]);
Services.prefs.setCharPref('network.proxy.socks_password', arguments[7]);
""", http_addr, http_port, ssl_addr, ssl_port, socks_addr, socks_port, socks_username, socks_password)
I have tried some other code snippets also. I tried to place values into alert boxes also.
You can achieve this by using AutoIt. And it has Python binding PyAutoIt. Once you installed PyAutoIt using PIP - pip install PyAutoIt, the following code does your job.
import autoit
autoit.win_wait_active("Authentication Required") # title of the dialog box to wait. so it will wait for the Authentication Required dialog
autoit.send("username", 1) # second parameter is the mode (changes how "keys" is processed)
autoit.send("{TAB}") # press tab key to go to the password field
autoit.send("password", 1)
autoit.send("{Enter}") # press enter key
For more information about the second parameter in the send method, here is the code,
def send(send_text, mode=0):
"""
Sends simulated keystrokes to the active window.
:param send_text:
:param mode: Changes how "keys" is processed:
flag = 0 (default), Text contains special characters like + and ! to
indicate SHIFT and ALT key presses.
flag = 1, keys are sent raw.
:return:
"""
AUTO_IT.AU3_Send(LPCWSTR(send_text), INT(mode))

Python: Move data of variable to config ini

I have test.py python file. I use selenium for testing. I have code:
login.fillLoginDataAndClick(self.driver,
emailField="myemail#domain.com",
passwordField="mypassword-qwerty")
I need to move all changeable data such as email and password to defaultconfig.ini file. So I need to move other credentials from general code to my config for further easy editing.
How to do it?
1.Create a file with defaultconfig.ini and then store value in file like below.You can make any level I have given Auth level.
[Auth]
email=myemail#domain.com
password=mypassword-qwerty
Then use the below code to fetch the value from file.
import configparser
config= configparser.ConfigParser()
config.read('D:\defaultconfig.ini')
emailid = config['Auth']['email']
print(emailid)
password= config['Auth']['password']
print(password)

Retrieving user name properties for a file

I am able to use the following code to get the size and last modified date. How can I get the user the file was last modified by?
file = "myFileName.xlsx"
size = str(os.path.getsize(file))
lastModified = str(time.ctime(os.path.getmtime(file)))
Thanks
This is not a perfect solution, but you can get the user's name by prompting the user to enter a password(if you password protect the file). The getpass() function in the getpass module will prompt the user to enter a password when they try modifying the file. At this point, the getuser() function will fetch the user's name from os.environ.get('USERNAME').
import time
import getpass
file = "test.txt"
if file:
getpass.getpass()
user = getpass.getuser()
size = str(os.path.getsize(file))
lastModified = str(time.ctime(os.path.getmtime(file)))
print(size, lastModified, user) #316139 Thu Mar 15 02:11:33 2018 user_name
Depending upon your OS configuration and whether the files are stored locally or on the network (such as shared Active Directory sites), the solution will be different. In this case, your question should be more elaborate.

Move Lotus Notes Email to a Different Folder Python

After extracting some of the email's data, I would like to move the email to a specified folder with python. I've searched and haven't seemed to find what I need.
Has anyone done this before?
Per a comment, I've added my current logic in hopes that it will clarify my problem. I loop through my folder, extract the details. After doing that, I want to move the email to a different folder.
import win32com.client
import getpass
import re
'''
Loops through Lotus Notes folder to view messages
'''
def docGenerator(folderName):
# Get credentials
mailServer = 'server'
mailPath = 'PubDir\inbox.nsf'
# Password
pw = getpass.getpass('Enter password: ')
# Connect
session = win32com.client.Dispatch('Lotus.NotesSession')
# Initializing the session and database
session.Initialize(pw)
db = session.GetDatabase(mailServer, mailPath)
# Get folder
folder = db.GetView(folderName)
if not folder:
raise Exception('Folder "%s" not found' % folderName)
# Get the first document
doc = folder.GetFirstDocument()
# If the document exists,
while doc:
# Yield it
yield doc
# Get the next document
doc = folder.GetNextDocument(doc)
# Loop through emails
for doc in docGenerator('Folder\Here'):
# setting variables
subject = doc.GetItemValue('Subject')[0].strip()
invoice = re.findall(r'\d+',subject)[0]
body = doc.GetItemValue('Body')[0].strip()
# Move email after extracting above data
# ???
As you will move the document before getting the next one, I'd recommend to replace your loop with
doc = folder.GetFirstDocument()
while doc:
docN = folder.GetNextDocument(doc)
yield doc
doc = docN
And then to move the message to the proper folder you need
doc.PutInFolder(r"Destination\Folder")
doc.RemoveFromFolder(r"Origin\Folder")
Of course, take care of escaping your backslashes or using raw strings literals to pass correctly the view name.
doc.PutInFolder creates the folder if it doesn't exist. In that case the user needs to have permissions to create public folders, otherwise the created folder will be private. (If the folder already exists, of course, this is not a problem.)

PRAW Errors updating to new Python Distro

So I am trying to create a bot that cross posts from a sub (r/pics) to (r/polpics) using a bit of code from u/GoldenSights. I upgraded to a new python distro and I get a ton of errors, I don't even know where to begin. Here is the code (formatting off, error lines bold):
Traceback (most recent call last):
File "C:\Users\tonyc\AppData\Local\Programs\Python\Python36-32\Lib\site-
packages\praw\subdump.py", line 84, in <module>
r = praw.Reddit(USERAGENT)
File "C:\Users\tonyc\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\praw\reddit.py", line 150, in __init__
raise ClientException(required_message.format(attribute))
praw.exceptions.ClientException: Required configuration setting 'client_id'
missing.
This setting can be provided in a praw.ini file, as a keyword argument to the `Reddit` class constructor, or as an environment variable.
This seems to be related to USERAGENT setting. I don't think I have that configured right.
USERAGENT = ""
# This is a short description of what the bot does. For example
"/u/GoldenSights' Newsletter bot"
SUBREDDIT = "pics"
# This is the sub or list of subs to scan for new posts.
# For a single sub, use "sub1".
# For multiple subs, use "sub1+sub2+sub3+...".
# For all use "all"
KEYWORDS = ["It looks like this post is about US Politics."]
# Any comment containing these words will be saved.
KEYDOMAINS = []
# If non-empty, linkposts must have these strings in their URL
This is the error line:
print('Logging in')
r = praw.Reddit(USERAGENT) <--here, this is error line 84
r.set_oauth_app_info(APP_ID, APP_SECRET, APP_URI)
r.refresh_access_information(APP_REFRESH)
Also in Reddit.py :
raise ClientException(required_message.format(attribute)) <--- error
praw.exceptions.ClientException: Required configuration setting 'client_id'
missing.
This setting can be provided in a praw.ini file, as a keyword argument to
the `Reddit` class constructor, or as an environment variable.
Firstly, you're going to want to have your API credentials stored externally in your praw.ini file. This makes things a lot more secure, and looks like it might go some way to fixing your issue. Here's what a completed praw.ini file looks like, including the useragent, so try to replicate this.
[DEFAULT]
# A boolean to indicate whether or not to check for package updates.
check_for_updates=True
# Object to kind mappings
comment_kind=t1
message_kind=t4
redditor_kind=t2
submission_kind=t3
subreddit_kind=t5
# The URL prefix for OAuth-related requests.
oauth_url=https://oauth.reddit.com
# The URL prefix for regular requests.
reddit_url=https://www.reddit.com
# The URL prefix for short URLs.
short_url=https://redd.it
[appname]
client_id=IE*******T14_w
client_secret=SW***********************CLY
password=******************
username=appname
user_agent=web:appname:1.0.0 (by /u/username)
Let me know how things go after you sort this out.

Categories

Resources