Create a page using Pywikibot - python

I am trying to create a page in https://dev.wikidebates.org/wiki/Wikidébats:Accueil, it is similar ti wikipedia, so Pywikibot should work the same way. I would like to make a page using Pywikibot. I checked option scripts in Pywikibot https://www.mediawiki.org/wiki/Manual:Pywikibot/Scripts. The script pagefromfile.py is responsible for it. However, I don't see in the code where I should write link on new page of wiki.
Also function inter in class Pagefromfile returns page. How can I check that the page was made?
The code which I try now is the following. Everything works ok, except last line.(Page is not created)
site = pywikibot.Site('dev', 'wikidebates') # The site we want to run our bot on
page = pywikibot.Page(site, "Faut-il légaliser le cannabis ?")
text = page.get() #get the code of the page
wb = open("pages1.txt", "w", encoding="utf-8")
wb.write(str(txt)) #write text to the file, in order to download it then to the wiki
wb.close()
main_page('-file:pages1.txt') #use main function from scrip pagefromfile.py - I renamed it

You haven't shown any further information. Probably there are any further messages from pagefromfile.py script. If you download the text from a wiki you either have to include begin and end markers to the text or you have to use the -textonly option. I also propose to use the -title option.
Refer the doc: https://doc.wikimedia.org/pywikibot/stable/scripts/general.html#module-scripts.pagefromfile
from scripts.pagefromfile import main # or main_page in your modified case
site = pywikibot.Site('wikidebates:dev') # you may use te site name for the constructor function
page = pywikibot.Page(site, 'Faut-il légaliser le cannabis ?')
text = page.text
with open("pages1.txt", "w", encoding="utf-8") as wp: # also closes the file
wb.write(text)
main('-file:pages1.txt', '-textonly', '-title:"The new title"')
To specify the target site, add a -site option to the main function if it is not your default site. For simulating purpose you can use the -simulate option:
main('-file:pages1.txt', '-textonly', '-title:"The new title"', '-simulate', '-site:wikidebates:dev')
Note: all arguments given to the main function must be strings delimited by comma. You cannot give all arguments as a long string except you do it like this:
main(*'-file:pages1.txt -textonly -title:The_new_title'.split())

Related

Python selenium how to make python to take the next notepad

I'm making a python script that will automatically post to Instagram. It's a skateboarding niche page and I want to give credit to people I repost... (I'm posting thrue facebook creator studio)
This is how the video picker looks like...
skatenr = int(skatenr) + int("1")
SendKeys = "C://Users/User/PycharmProjects/skateboard auto post/skate/"+str(memenr)+".mp4"
driver.find_element_by_xpath('/html/body/div[6]/div/div/div/div/div/a/div[2]/input').send_keys(SendKeys)
You could see how I got it to take the next post from the folder I store my vids in. My idea was that I store the captions in another folder and name it skate1.txt skate2.txt ...... then like the videos it takes the notepad and puts it in the caption place. How do I make Python pick the next notepad.
def read_line():
f = open('skate1.txt')
The = (f.read())
return The
print(read_line())
any ideas?
Regards your friendly guy who started coding yesterday
if you run the code at all time you can use this formular
from os import path
x=0
The=[]
while path.exists('skate%s.txt'%str(x)):
with open("skate%s.txt"%str(x),'r') as file:
k=file.read()
The.append(k)
x+=1
now all of your caption will be in the "The" list

How to call R Markdown knit function from Python script

I have a Python script that creates multiple .Rmd files, and I wanted a way to automatically turn them into .html's without having to manually knit each within RStudio. I've probably spent around 4 hours researching and trying different options, and although I've managed to make it work by calling a .R script with
subprocess.Popen(['Rscript', '--vanilla', 'rmd2html.R'], shell=False)
that then does the knitting with
rmarkdown::render("dicionarioNew.Rmd", "html_document"),
this for some reason does not use UTF-8 (which I need) and doesn't easily allow me to store the number of times the program has been run (necessary for giving a different name to each html file).
Your title question is answered in your question's body but you have more specific needs not met by your current implmentation:
how to render .Rmd using UTF-8
how to render .Rmd to html with custom filename output
I suggest you try seeking out the answers to these individually.
As a general answer to this question however, I suggest you consider using the Rscript command to run a custom R script which does what you want based on the source of rmd2html.R. You might also use R -e to execute a line or few of R code hardcoded as a string in your python script.
If you want to break things out further in python, there are many options for rendering at the chunk or file level individually using the sweave, rmarkdown, stationary, and other R packages.
Given a more specific example of what you are trying to accomplish someone may be able to help point you towards which of these many options would be right for your use case.
An alternative is to add a chunk that will knit the .rmd file when it is executed. See my approach below - it also opens the .html file in the view panel of Rstudio after knitting. That way you can probably also build in the tracker of how many times the code is run and add that into the file-name.
For the "auto_knit_chunk" shown below to work you need to setup two variables, KNITreport (TRUE/FALSE) and the output directory (or remove their use if you don't need them) in a chuck above the "auto_knit_chunk".
#IN A CHUNK ABOVE THE auto_knit_chunk SET THE VARIABLES
#if this RMD file should be knitted, set KINITreport = TRUE
KNITreport <- TRUE
#set an output path
dir_output <- "./output/"
The "auto_knit_chunk" chunk should only be executed IF knitr is not currently executed to avoid an infinite loop of calling knitr from each document that is knitted. So in the header do: eval = !=isTRUE(getOption('knitr.in.progress'))
# THIS IS THE HEADER ```{r, auto_knit_chunk, echo = FALSE, eval = !isTRUE(getOption('knitr.in.progress'))}
if(KNITreport)
{
#saving the currently open Rstudio file - needs to be saved bevore knitting to knit the most up to date version
rstudioapi::documentSave(rstudioapi::getActiveDocumentContext()$id)
#obtaining the file name of the currently active document
RMD_fileName <- str_replace(rstudioapi::getActiveDocumentContext()$path, paste0(sub("\\/[^\\/]*$", "",rstudioapi::getActiveDocumentContext()$path),"/"),"")
#setting up the file name and output directory to save the knitted file
outputFileName <- paste0(str_replace(RMD_fileName,".Rmd",""), "_KNITTED")
outputPath <- paste0(getwd(),str_replace(dir_output,".",""))
#if there is an RMD_fileName knit it
if(RMD_fileName!=""){
rmarkdown::render(input = RMD_fileName, output_file = outputFileName, output_dir = outputPath)
print(paste("Summary file:",outputFileName," is at",outputPath))
}else print("No RMD file retrieved")
#a function to display HTML content in the Rstudio viewer
viewerpane.html <- function(xfile, vsize=NULL){
# viewerpane.html was written by anwhite03 and published here:
# https://community.rstudio.com/t/rstudio-knit-explicit-r-command-for-preview-option-after-rmd-is-knit/11368
# Function: viewerpane.html version 1.00 23July2018
# Purpose: view RMarkdown Knit-generated html file in RStudio Viewer pane
# Status: Dev/Test
# Args:
# xfile = quoted name of html file (and path if not located in current directory)
# vsize = viewer arg height, default=NULL; alt values: "maximize", or numeric {3 to 8}
# Example: x <- "RMD-Demo-Viridis-002x.html"
# References:
# 1. https://rstudio.github.io/rstudio-extensions/rstudio_viewer.html
# 2. https://rstudio.github.io/rstudio-extensions/pkgdown/rstudioapi/reference/viewer.html
# 3. https://rstudio.github.io/rstudio-extensions/rstudioapi.html
#
# library(rstudioapi)
xfile.b <- basename(xfile)
tempDir <- tempfile()
dir.create(tempDir)
htmlFile <- file.path(tempDir, xfile.b)
# (code to write some content to the file) -- see next line
file.copy(xfile, htmlFile)
viewer <- getOption("viewer")
viewer(htmlFile, height = vsize)
}
#calling the function to display the HTML document
viewerpane.html(xfile = paste0(outputPath,outputFileName,".html"))
}

In Python, urllib.urlretrieve downloads a file which says "Go away"

I'm trying to download the (APK) files from links such as https://www.apkmirror.com/wp-content/themes/APKMirror/download.php?id=215041. When you enter the link in your browser, it brings up a dialog to open or save the file (see below).
I would like to save the file using a Python script. I've tried the following:
import urllib
download_link = 'https://www.apkmirror.com/wp-content/themes/APKMirror/download.php?id=215041'
download_file = '/tmp/apkmirror_test/youtube.apk'
if __name__ == "__main__":
urllib.urlretrieve(url=download_link, filename=download_file)
but the resulting youtube.apk contains only the words "Go away".
Since I am able to download the file by pasting the link in my browser's address bar, there must be some difference between that and urllib.urlretrieve that makes this not work. Can someone explain this difference and how to eliminate it?
You should not programmatically access that download page as it is disallowed in the robots.txt:
https://www.apkmirror.com/robots.txt
That being said, your request header is different. Python by default sets User-Agent to something like "Python...". That is the most likely cause of detection.

How to create page links?

I know current user's location. It can be one of the following url:
(1) http://myapp.appspot.com/something/something-else/
(2) http://myapp.appspot.com/something/something-else
(3) http://myapp.appspot.com/something/something-else/page1
(4) http://myapp.appspot.com/something/something-else/page3
(actually, addresses 1, 2 and 3 are for the same page1)
I need to display on these pages link for page2:
http://myapp.appspot.com/something/something-else/page2
The question is how to generate such link?
I've tried to use relative links: /page2 and page2 - doesn't work properly. I am not sure how to create absolute link with self.request.path - it doesn't work properly also.
/page2 will never work; the leading / makes it relative to the website root rather than the current directory.
page2 should work for everything except #2; without a trailing slash, something-else is interpreted as a file rather than the current directory.
One solution would be to link to /something/something-else/page2 so your link doesn't change based on the user's address.
import something #refers to your .py file with the template handler
...
application = webapp.WSGIApplication([
('/something/something-else/', something.SomeThingElseHandler),
('/something/something-else', something.SomeThingElseHandler),
('/something/something-else/' + '([^/]+)/', something.PageHandler),
#The above pattern will be recognized if you close the url with /
#If you want your url to end without the slash your remove it for the reg ex. like
('/something/something-else/' + '([^/]+)', something.PageHandler),
],
debug=config.DEBUG)
util.run_wsgi_app(application)
In something.py, in your class PageHandler you have to parse the key or the id you are parsing manually to render the correct content.

Problem with exiting a Word doc using Python

This is my first time using this so be kind :) basically my question is I am making a program that opens many Microsoft Word 2007 docs and reads from a certain table in that document and writes that info to an excel file there is well in excess of 1000 word docs. I have all of this working but the only problem when I run my code it does not close MSword after opening each doc I have to manually do this at the end of the program run by opening word and selecting exit word option from the Home menu. Another problem is also if a run this program consecutively on the second run everything goes to hell it prints the same thing repeatedly no matter which doc is selected I think this may have to do with how MSword is deciding which doc is active e.g. is it still opening the last active document that was not closed from the last run. Anyways here is my code for the opening and closing part I wont bore you guys with the rest::
MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = 0
# Open a specific file
#myWordDoc = tkFileDialog.askopenfilename()
MSWord.Documents.Open("C:\\Documents and Settings\\fdosier" + chosen_doc)
#Get the textual content
docText = MSWord.Documents[0].Content
charText = MSWord.Documents[0].Characters
# Get a list of tables
ListTables = MSWord.Documents[0].Tables
------Main Code---------
MSWord.Documents.Close
MSWord.Documents.Quit
del MSWord
Basically, Python is not VBA, so this:
MSWord.Documents.Close
is equivalent to:
getattr(MSWord.Documents, "Close")
i.e. you just get some method object and do nothing with it. You need to call the method with the call operator (the parentheses :) :
MSWord.Documents.Close()
Accordingly for .Quit.
Before your MSWord.Quit did you try using:
MSWord.ActiveWindow.Close
Or even more simpley just doing
MSWord.Quit
I dont really understand if you are trying to close a document or the application.
I think you need a MSWord.Quit at the end (before and/or instead of the the del)

Categories