how to enable firefox addons in Ghost.py - python

Is it possible to run imacro firefox script inside ghost.py?
I want to automate heavy ajax sites.
I'm trying this:
from ghost import Ghost
ghost = Ghost(plugins_enabled=True,plugin_path=['C:\Documents and Settings\my\Desktop\addons\addon-3863-latest.xpi'],)

Within Ghost.py you would find the code snippet:
if plugin_path:
for p in plugin_path:
Ghost._app.addLibraryPath(p)
From the look of things, it's expecting a path (addLibraryPath) and would do its discoveries by itself. So, give it a path containing the .xpi
Note: this isn't tested.
On my Windows 10, I typed this into a Windows address bar:
%APPDATA%\Mozilla\Firefox\Profiles
I then saw a folder named "kswjuot9.default" (it might be named another things on your PC) and clicked it. Finally, I found a folder named "extensions".
Try to give Ghost.py the full address of this "extensions" folder. I would also recommend that you use forward slashes e.g.
C:/Users/iChux/AppData/Roaming/Mozilla/Firefox/Profiles/kswjuot9.default/extensions
I saw an online link on dealing with how to extract the .xpi file

Related

Internet Shortcut in python

I have a problem. Let's say I have a website (e.g. www.google.com). Is there any way to create a file with a .url extension linking to this website in python? (I am currently looking for a flat, and I am trying to save shortcuts on my hard drive only to apartment offers posted online matching my expectations ) I've tried to use the os and requests module to create such files, but with no success. I would really appreciate the help. (I am using python 3.9.6 on Windows 10)
This is pretty straightforward. I had no idea what .URL files were before seeing this post, so I decided to drag its URL to my desktop. It created a file with the following contents which I viewed in Notepad:
[InternetShortcut]
URL=https://stackoverflow.com/questions/68304057/internet-shortcut-in-python
So, you just need to write out the same thing via Python, except replace the URL with the one you want:
test_url = r'https://www.google.com/'
with open('Google.url','w') as f:
f.write(f"""[InternetShortcut]
URL={test_url}
""")
With regards to your current attempts:
I've tried to use os and requests module to create such file
It's not clear what you're using requests or os for, since you didn't provide a Minimal Reproduceable Example of what you'd tried so far; so, if there's a more complex element to this that you didn't specify, such as automatically generating the file while you're in your browser, or something like that, then you need to update your question to include all of your requirements.

Google Chrome cannot read and write to its data directory : selenium

Here's the issue i am facing now.
I could launch chrome driver. However my selenium code suddenly doesnt work and pops up above image.
Hope someone can shed light as i couldn't find a solution online. .
Generally BarthRid's answer is correct, providing full path to userdata folder works with Chrome 90+. But if you store userdata folder in the same directory as your script, you can use pathlib to manage things more easily.
import pathlib
script_directory = pathlib.Path().absolute()
options.add_argument(f"user-data-dir={script_directory}\\userdata")
This way allows you to store actual script's directory in a variable, and via formatted string put it in selenium's options argument. Useful while dealing with multiple instances.
I had a similar problem to yours and #scott degen.
options.add_argument line of your pasted screenshot
I changed mine from:
options.add_argument("user-data-dir=selenium")
to the full directory, and it seems to be working better now.
options.add_argument("user-data-dir=C:\environments\selenium")
I changed mine from:
options.add_argument("user-data-dir=selenium")
to the full directory, and it seems to be working better now.
dir_path = os.getcwd()
chrome_option.add_argument(f'user-data-dir={dir_path}/selenium')
It seems Chrome changes its Current Working Directory during startup and so the relative path won't work. You can try to convert the user-data-dir to absolute path then pass to chrome. See Bug: 1058347

How does "webbrowser" module work?

Here is the code:
Sublime plugin:
File 1: open_in_default_program.py:
# https://github.com/SublimeTextIssues/Core/issues/2368
import webbrowser
import sublime_plugin
class OpenInDefaultProgramCommand(sublime_plugin.TextCommand):
def run(self, edit):
if self.view.file_name():
webbrowser.open_new_tab("file://" + self.view.file_name())
def is_visible(self):
return self.view.file_name() is not None and (
self.view.file_name()[-5:] == ".html" or
self.view.file_name()[-3:] == ".md" or
self.view.file_name()[-4:] == ".ahk")
File 2: Context.sublime-menu:
[
{ "command": "open_in_default_program" },
]
AutoHotkey test file:
Test.ahk:
MsgBox Something
My question:
It works for HTML and Markdown files. It also works for AutoHotkey files - but how? From what I see, it uses browser. AutoHotkey files can't be opened in browser - but actually they are perfectly could be launched with this plugin. Why it works?
Here is another plugin for opening files in default application, but it's much more complex: https://github.com/SublimeText/OpenDefaultApplication/blob/master/OpenDefault.py
This is mentioned in the documentation for webbrowser.open:
Note that on some platforms, trying to open a filename using this function, may work and start the operating system’s associated program. However, this is neither supported nor portable.
The reason for this is that some browsers, when given a file they don't know how to handle, will automatically open it in the default program for that file. For example, on Windows, Internet Explorer is basically the same program as Windows Explorer,1 so asking Internet Explorer to open a file it doesn't know how to handle has basically the same effect as double-clicking that file in Windows Explorer.
Of course other browsers might do nothing, or copy the file to your Downloads directory, or pop up a dialog asking you what you want to do with this file. That's why the docs say "this is neither supported nor portable".
It's also worth noting that, like many of the stdlib modules, the docs for webbrowser have a link to the source code at the top, and the source code is pretty straightforward, simple Python code. You can see that ultimately, it's just using the subprocess module to call something like (depending on your detected browser, and possibly with some browser-specific options to tell it "don't start a whole new browser, tell the existing browser window to open a new tab"):
iexplore.exe file://path/to/your/file
You can easily work out exactly what command it's running and experiment running the same command in your shell/command prompt.
The more complex plugin shows the way to do this as portably as possible:
On Windows, you can call os.startfile.
On other platforms, you run a command-line tool. (The plugin seems to work out the right tool at install time, store it in a settings file, and look it up in that file.)
On macOS, it's open.
On FreeDesktop systems, including most modern Linux distros, it's xdg-open.
Those three options are usually enough to cover 99% of your users, and almost all of the remaining users will be people who know what they're doing and can figure out what to put in your settings file. (Unless, of course, you're developing for mobile, in which case you'll want to write special handlers for iOS and Android.)
1. This isn't really true anymore in modern Windows, but it's close enough to illustrate the point.

Extremely new user to Python. "No module named request" error while trying code to detect image subdomains in a website to extract them to a folder

I may sound rather uninformed writing this, and unfortunately, my current issue may require a very articulate answer to fix. Therefore, I will try to be specific as possible as to ensure that my problem can be concisely understood.
My apologizes for that- as this Python code was merely obtained from a friend of mine who wrote it for me in order to complete a certain task. I myself had had extremely minimal programming knowledge.
Essentially, I am running Python 3.6 on a Mac. I am trying to work out a code that allows Python to scan through a bulk of a particular website's potentially existent subdomains in order to find possibly-existent JPG images files contained within said subdomains, and download any and all of the resulting found files to a distinct folder on my Desktop.
The Setup-
The code itself, named "download.py" on my computer, is written as follows:
import urllib.request
start = int(input("Start range:100000"))
stop = int(input("End range:199999"))
for i in range(start, stop + 1):
filename = str(i).rjust(6, '0') + ".jpg"
url = "http://website.com/Image_" + filename
urllib.request.urlretrieve(url, filename)
print(url)
(Note that the words "website" and "Image" have been substituted for the actual text included in my code).
Before I proceed, perhaps some explanation would be necessary.
Basically, the website in question contains several subdomains that include .JPG images, however, the majority of the exact URLs that allow the user to access these sub-domains are unknown and are a hidden component of the internal website itself. The format is "website.com/Image_xxxxxx.jpg", wherein x indicates a particular digit, and there are 6 total numerical digits by which only when combined to make a valid code pertain to each of the existent images on the site.
So as you can see, I have calibrated the code so that Python will initially search through number values in the aforementioned URL format from 100000 to 199999, and upon discovering any .JPG images attributed to any of the thousands of link combinations, will directly download all existent uncovered images to a specific folder that resides within my Desktop. The aim would be to start from that specific portion of number values, and upon running the code and fetching any images (or not), continually renumbering the code to work my way through all of the possible 6-digit combos until the operation is ultimately a success.
(Possible Side-Issue- Although I am fairly confident that my friend's code is written in a manner so that Python will only download .JPG files to my computer from images that actually do exist on that particular URL, rather than swarming my folder with blank/bare files from every single one of URL attempts regardless of whether that URL happens to be successful or not, I am admittedly not completely certain. If the latter is the case, informing me of a more suitable edit to my code would be tremendously appreciated.)
The Execution-
Right off the bat, the code experienced a large error. I'll list through the series of steps that led to the creation of said error.
#1- Of course, I first copy-pasted the code into a text document, and saved it as "download.py". I saved it inside of a folder named "Images" where I sought the images to be directly downloaded to. I used BBEdit.
#2- I proceeded, in Terminal, to input the commands "cd Desktop/Images" (to account for the file being held within the "Images" folder on my Desktop), followed by the command "Python download.py" (to actually run the code).
As you can see, the error which I obtained following my attempt to run the code was the ImportError: No module named request. Despite me guessing that the answer to solving this is simple, I can legitimately say I have got such minimal knowledge regarding Python that I've absolutely no idea how to solve this.
Hint: Prior to making the download.py file, the folder, and typing the Terminal code the only interactions I made with Python were downloading the program (3.6) and placing it in my toolbar. I'm not even quite sure if I am required to create any additional scripts/text files, or make any additional downloads before a script like this would work and successfully download the resulting images into my "Images" folder as is my desired goal. If I sincerely missed something integral at any point during this long read, hopefully, someone in here can provide a thoroughly detailed explanation as to how to solve my issue.
Finishing statements for those who've managed to stick along this far:
Thank you. I know this is one hell of a read, and I'm getting more tired as I go along. What I hope to get out of this question is
1.) Obviously, what would constitute a direct solution to the "No module named request" Input Error in Terminal. In other words, what I did wrong there or am missing.
2.) Any other helpful information that you know would assist this code, for example, if there is any integral step or condition I've missed or failed to meet that would ultimately cause the entirety of my code to cease to work. If you do see a fault in this, I only ask of you to be specific, as I've not got much experience in the programming world. After all, I know there is a lot of developers out here that are far more informed and experienced than am I. Thanks.
urllib.request is in Python 3 only. When running 'python' on a Mac, you're running Python 2 by default. Try running executing with python3.
python --version
might need to
brew install python3
urllib.request is a Python 3 construct. Most systems run Python 2 as default and this is what you get when you run simply python.
To install Python 3, go to https://brew.sh/ and follow the instructions to install the Hombrew package manager. Then run
brew install python3
python3 download.py

Edit Mozilla configuration file in Python

Settings for the Mozilla Firefox browser my be changed by entering about:config in the address bar, which will open a configuration file. I would like to be able to edit, delete and add parameters to this file in Python. How can I do this?
The configuration files used in firefox are actually a couple of javascript files that you can find under the profile directory: prefs.js and user.js.
You can use python to append lines to those files (user.js recommended) like:
user_pref(<preference>, <value>)
However, I don't really think is a good idea to use python for this.
You can check
Firefox Profiles
to get to the OS dependent profile,
also,
here is a duplicate question to point you in the right direction.
Also, maybe
this Javascript Python bridge will better assist you editing the files.

Categories