Image caching loop goes wrong for my recipe searching program (daemon) - python

I wrote a daemon program, for searching through recipes from Gourmet Recipe Manager's database ( that is a recipe manager for GNU/Linux )
My program reads the information that it needs for each recipe element over a loop from the sqlite database.
(Such a daemon for Ubuntu Linux is called a 'scope'.
Such scopes give ubuntu unity more sources for its searching.)
'model' has the information, which gets delivered over DBUS to Ubuntu Unity.
In theory you are able to use an URI as the source for the image in 'model' ,
but the developers said me in IRC I am not able to use data URI's.
I tested that, too, and for me it did not work.
So I cache(d) the images in /tmp.
Now you are able to see all recipes, and search for specific one's per title, but the image asociation is simply wrong. if you search for the 2.th , the recipe of the 2.th gets shown, but with the image of the fist recipe in the sqlite table.
Here are two images, to understand the problem:
The 2.th recipe gets the image of the first recipe
I already looked in some IRC rooms for help, but no one could help me...
I think you have to save the state of each image somehow.
I would be pleased, if you have a solution that does not requiere to cache images.
The full source file can be viewed here: http://bazaar.launchpad.net/~gotwig/lens-cooking/lens-cooking/view/head:/unity-scope-gourmet
So, here is the specific part of my code:
if row[14]:
open('/tmp/unity-scope-gourmet/icon' + str(i), 'wb').write(row[14])
model.append(uri, '/tmp/unity-scope-gourmet/icon' + str(i), 1, "text/html", title, comment, uri)
else:
if os.path.exists('/tmp/unity-scope-gourmet/icon' + str(i)): os.remove('/tmp/unity-scope-gourmet/icon' + str(i))
model.append(uri, '', 1, "text/html", title, comment, uri)

It sounds like you a describing a simple counter error. It's not clear to me how you are initializing your counter 'i', but if it's off by one, the simple solution is to simply add one to it before using it, ie:
open('/tmp/unity-scope-gourmet/icon' + str(i+1), 'wb').write(row[14])
^^^

I solved the problem with combining the filename of the cached image with each ID of the recipe.
Code:
i = row[0]
See the complete solution here : http://bazaar.launchpad.net/~gotwig/lens-cooking/lens-cooking/revision/32

Related

Can LibreOffice/OpenOffice programmatically add passwords to existing .docx/.xlsx/.pptx files?

TL;DR version - I need to programmatically add a password to .docx/.xlsx/.pptx files using LibreOffice and it doesn't work, and no errors are reported back either, my request to add a password is simply ignored, and a password-less version of the same file is saved.
In-depth:
I'm trying to script the ability to password-protect existing .docx/.xlsx/.pptx files using LibreOffice.
I'm using 64-bit LibreOffice 6.2.5.2 which is the latest version at the time of writing, on Windows 8.1 64-bit Professional.
Whilst I can do this manually via the UI - specifically, I open the "plain" document, do "Save As" and then tick "Save with Password", and enter the password in there, I cannot get this to work via any kind of automation. I'm been trying via Python/Uno, but to no gain. Although the code below correctly opens and saves the document, my attempt to add a password is completely ignored. Curiously, the file size shrinks from 12kb to 9kb when I do this.
Here is my code:
import socket
import uno
import sys
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
from com.sun.star.beans import PropertyValue
properties=[]
oDocB = desktop.loadComponentFromURL ("file:///C:/Docs/PlainDoc.docx","_blank",0, tuple(properties) )
sp=[]
sp1=PropertyValue()
sp1.Name='FilterName'
sp1.Value='MS Word 2007 XML'
sp.append(sp1)
sp2=PropertyValue()
sp2.Name='Password'
sp2.Value='secret'
sp.append(sp2)
oDocB.storeToURL("file:///C:/Docs/PasswordDoc.docx",sp)
oDocB.dispose()
I've had great results using Python/Uno to open password-protected files, but I cannot get it to protect a previously unprotected document. I've tried enabling the macro recorder and recording my actions - it recorded the following LibreOffice BASIC code:
sub SaveDoc
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem ----------------------------------------------------------------------
dim args1(2) as new com.sun.star.beans.PropertyValue
args1(0).Name = "URL"
args1(0).Value = "file:///C:/Docs/PasswordDoc.docx"
args1(1).Name = "FilterName"
args1(1).Value = "MS Word 2007 XML"
args1(2).Name = "EncryptionData"
args1(2).Value = Array(Array("OOXPassword","secret"))
dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, args1())
end sub
Even when I try to run that, it...saves an unprotected document, with no password encryption. I've even tried converting the macro above into the equivalent Python code, but to no avail either. I don't get any errors, it simply doesn't protect the document.
Finally, out of desperation, I've even tried other approaches that don't include LibreOffice, for example, using the Apache POI library as per the following existing StackOverflow question:
Python or LibreOffice Save xlsx file encrypted with password
...but I just get an error saying "Error: Could not find or load main class org.python.util.jython". I've tried upgrading my JDK, tweaking the paths used in the example, i.e. had an "intelligent" go, but still no joy. I suspect the error above is trivial to fix, but I'm not a Java developer and lack the experience in this area.
Does anyone have any solution? Do you have some LibreOffice code that can do this (password-protect .docx/.xlsx/.pptx files)? Or OpenOffice for that matter, I'm not precious about which package I use. Or something else entirely!
NOTE: I appreciate this is trivial using full-fat Microsoft Office, but thanks to Microsoft's licensing restrictions, is a complete no-go for this project - I have to use an alternative.
The following example is from page 40 (file page 56) of Useful Macro Information
For OpenOffice.org by Andrew Pitonyak (http://www.pitonyak.org/AndrewMacro.odt). The document is directed to OpenOffice.org Basic but is generally applicable to LibreOffice as well. The example differs from the macro recorder version primarily in its use of the documented API rather than dispatch calls.
5.8.3. Save a document with a password
To save a document with a password, you must set the “Password”
attribute.
Listing 5.19: Save a document using a password.
Sub SaveDocumentWithPassword
Dim args(0) As New com.sun.star.beans.PropertyValue
Dim sURL$
args(0).Name ="Password"
args(0).Value = "test"
sURL=ConvertToURL("/andrew0/home/andy/test.odt")
ThisComponent.storeToURL(sURL, args())
End Sub
The argument name is case sensitive, so “password” will not work.

python-docx does not add picture

I'm trying to insert a picture into a Word document using python-docx but running into errors.
The code is simply:
document.add_picture("test.jpg", width = Cm(2.0))
From looking at the python-docx documentation I can see that the following XML should be generated:
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPr id="1" name="python-powered.png"/>
<pic:cNvPicPr/>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="rId7"/>
<a:stretch>
<a:fillRect/>
</a:stretch>
</pic:blipFill>
<pic:spPr>
<a:xfrm>
<a:off x="0" y="0"/>
<a:ext cx="859536" cy="343814"/>
</a:xfrm>
<a:prstGeom prst="rect"/>
</pic:spPr>
</pic:pic>
This does in fact get generated in my document.xml file. (When unzipping the docx file). However looking into the OOXML format I can see that the image should also be saved under the media folder and the relationship should be mapped in word/_rels/document.xml:
<Relationship Id="rId20"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
Target="media/image20.png"/>
None of this is happens however, and when I open the Word document I'm met with a "The picture can't be displayed" placeholder.
Can anyone help me understand what is going on?
It looks like the image is not embedded the way it should be and I need to insert it in the media folder and add the mapping for it, however as a well documented feature this should be working as expected.
UPDATE:
Testing it out with an empty docx file that image does get added as expected which leads me to believe it might have something to do with the python-docx-template library. (https://github.com/elapouya/python-docx-template)
It uses python-docx and jinja to allow templating capabilities but runs and works the same way python-docx should. I added the image to a subdoc which then gets inserted into a full document at a given place.
A sample code can be seen below (from https://github.com/elapouya/python-docx-template/blob/master/tests/subdoc.py):
from docxtpl import DocxTemplate
from docx.shared import Inches
tpl=DocxTemplate('test_files/subdoc_tpl.docx')
sd = tpl.new_subdoc()
sd.add_paragraph('A picture :')
sd.add_picture('test_files/python_logo.png', width=Inches(1.25))
context = {
'mysubdoc' : sd,
}
tpl.render(context)
tpl.save('test_files/subdoc.docx')
I'll keep this up in case anyone else manages to make the same mistake as I did :) I managed to debug it in the end.
The problem was in how I used the python-docx-template library. I opened up a DocxTemplate like so:
report_output = DocxTemplate(template_path)
DoThings(value,template_path)
report_output.render(dictionary)
report_output.save(output_path)
But I accidentally opened it up twice. Instead of passing the template to a function, when working with it, I passed a path to it and opened it again when creating subdocs and building them.
def DoThings(data,template_path):
doc = DocxTemplate(template_path)
temp_finding = doc.new_subdoc()
#DO THINGS
Finally after I had the subdocs built, I rendered the first template which seemed to work fine for paragraphs and such but I'm guessing the images were added to the "second" opened template and not to the first one that I was actually rendering. After passing the template to the function it started working as expected!
I came acrossed with this problem and it was solved after the parameter width=(1.0) in method add_picture removed.
when parameter width=(1.0) was added, I could not see the pic in test.docx
so, it MIGHT BE resulted from an unappropriate size was set to the picture,
to add pictures, headings, paragraphs to existing document:
doc = Document(full_path) # open an existing document with existing styles
for row in tableData: # list from the json api ...
print ('row {}'.format(row))
level = row['level']
levelStyle = 'Heading ' + str(level)
title = row['title']
heading = doc.add_heading( title , level)
heading.style = doc.styles[levelStyle]
p = doc.add_paragraph(row['description'])
if row['img_http_path']:
ip = doc.add_paragraph()
r = ip.add_run()
r.add_text(row['img_name'])
r.add_text("\n")
r.add_picture(row['img_http_path'], width = Cm(15.0))
doc.save(full_path)

Post a photo in FB with GraphAPI in Python

Right now my job is advertise different kinds of business.
In order to do that i need to post in multiple facebook groups different products but that eats pretty much all my time so i'm trying to develop my own post-bot so i can post whatever i want in the moment in all the specific local facebook groups immediately.
I can post text in the feed, but can't make the code post a picture with text.
I get this error:
Traceback (most recent call last):
File "C:\Users\Ventas\Desktop\Post2.py", line 31, in
graph.put_object(parent_object=group_id, connection_name='photos', caption=titulo, message=mensaje, source=foto) File
"C:\Python27\lib\site-packages\facebook__init__.py", line 144, in
put_object
method="POST") File "C:\Python27\lib\site-packages\facebook__init__.py", line 272, in
request
raise GraphAPIError(result) GraphAPIError: (#324) Requires upload file
I don't know what i'm doing wrong, this is my code:
#SETUP Photo with file selection dialog
Tkinter.Tk().withdraw()
dir = tkFileDialog.askopenfilename()
photo = open(dir, "rb").read()
#POST IN GROUPS
for group_id in groups:
print("Posting to " + 'https://www.facebook.com/groups/' + str(group_id))
graph.put_object(parent_object=group_id, connection_name='photos', caption=titulo, message=mensaje, source=photo)
Ok, i tried selenium, something that a guy on facebook recommended me, but it did not worked, so i went back to python.
I was playing around with the api, but the documentation was not very clear in how to post in a group specifically.
So i started surfing the most deep part in google and found THIS
It made me reach to the conclusion of the code, the freakin' solution! :D (OMG FINALLY) i've been working on this thing like 4 days already.
I just changed this:
graph.put_object(parent_object=group_id, connection_name='photos', caption=titulo, message=mensaje, source=photo)
To this:
graph.put_photo(image=photo, message=mensaje, album_path=group_id + "/photos")
Also i noticed that the original documentation says that the value "album_path" should be:
album_path=group_id + "/pictures"
But thats ONLY and STRICTLY for pages.
It seems that for groups is:
album_path=group_id + "/photos"
Thank you to anyone who bothered to at least read my question, and very special thanks to the users "Jérémy de Gail" and "Amit Singh" from facebook, since no one bothered to even try to help me.
This code is very important for my job.
Hope my own answer may help someone in the future.
Regards to everyone! :D
Try to put the picture in the same file where you have your .py script.
Then, do:
photo = "nameofyourpic.jpg"
Instead of:
photo = open(dir, "rb").read()
As of the new API release, you can no longer upload images through a local path but rather a relative URL (Image stored on server).
graph.put_object(parent_object=group_id, connection_name='photos', caption=titulo, message=mensaje, source="https://1.bp.blogspot.com/-VqCH72pVnok/X-WgDpD5jSI/AAAAAAAA20E/pLCoCXR_7tsme8etcjYAOJhNa9GdPh4ewCLcBGAsYHQ/w640-h428/iherb_holiday.png")

How to generate HTML report in Python?

I'm looking for a way to print out all my graphs (from matplotlib but already saved as png files) and some data frames in HTML, just like what I usually do with R2HTML.
However I couldn't find detailed descriptions about Python module or functions doing this. Could anyone give me some suggestions?
Looks like a couple of tools exist. I've focused on simple html text writers since I'll be crafting my report page structures completely from scratch. This may differ from the R2HTML which I would guess has a lot of convenience functionality for the sort of things one wishes to stuff into pages from R objects.
HTMLTags
This fella wrote a module from scratch at this ActiveState community page: HTMLTags - generate HTML in Python (Python recipe). In the comments I discovered most of the tools I enumerate for this answer.
htmlgen
This package looks pretty good and basic. I'm not sure if its the same module described in this old article.
XIST
this one looks pretty legit and includes a parser as well. Here's some sample page generation code using the format that you'll need to use to interject all the appropriate python commands inbetween various html elements. The other format utilizes a bunch of nested function calls which will make the python interjection very awkward at best.
with xsc.build() :
with xsc.Frag() as myHtmlReport :
+xml.XML()
+html.DocTypeXHTML10transitional()
with html.html() :
reportTitle = "My report title"
with html.head() :
+meta.contenttype()
+html.title( reportTitle )
with html.body() :
# Insert title header
+html.h1( reportTitle )
with html.table() :
# Header Row
with html.tr() :
with html.td() :
+xsc.Text( "Col 1 Header" )
with html.td() :
+xsc.Text( "Col 2 Header" )
# Data Rows
for i in [ 1, 2, 3, 4, 5 ] :
with html.tr() :
with html.td() :
+xsc.Text( "data1_" + str(i) )
with html.td() :
+xsc.Text( "data2_" + str(i) )
# Write the report to disk
with open( "MyReportfileName.html" , "wb" ) as f:
f.write( myHtmlReport.bytes( encoding="us-ascii" ) )
libxml2
through Python bindings there's a plain vanilla xmlwriter module, which is probably too generic but good to know about nonetheless. Windows binaries for this package can be found here.
first install the package
https://pypi.org/project/html-testRunner/
and use
if __name__ == "__main__":
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(
output= r"here the place where you want to store the your
HTML report(mean to say folder path"))

abstracting the conversion between id3 tags, m4a tags, flac tags

I'm looking for a resource in python or bash that will make it easy to take, for example, mp3 file X and m4a file Y and say "copy X's tags to Y".
Python's "mutagen" module is great for manupulating tags in general, but there's no abstract concept of "artist field" that spans different types of tag; I want a library that handles all the fiddly bits and knows fieldname equivalences. For things not all tag systems can express, I'm okay with information being lost or best-guessed.
(Use case: I encode lossless files to mp3, then go use the mp3s for listening. Every month or so, I want to be able to update the 'master' lossless files with whatever tag changes I've made to the mp3s. I'm tired of stubbing my toes on implementation differences among formats.)
I needed this exact thing, and I, too, realized quickly that mutagen is not a distant enough abstraction to do this kind of thing. Fortunately, the authors of mutagen needed it for their media player QuodLibet.
I had to dig through the QuodLibet source to find out how to use it, but once I understood it, I wrote a utility called sequitur which is intended to be a command line equivalent to ExFalso (QuodLibet's tagging component). It uses this abstraction mechanism and provides some added abstraction and functionality.
If you want to check out the source, here's a link to the latest tarball. The package is actually a set of three command line scripts and a module for interfacing with QL. If you want to install the whole thing, you can use:
easy_install QLCLI
One thing to keep in mind about exfalso/quodlibet (and consequently sequitur) is that they actually implement audio metadata properly, which means that all tags support multiple values (unless the file type prohibits it, which there aren't many that do). So, doing something like:
print qllib.AudioFile('foo.mp3')['artist']
Will not output a single string, but will output a list of strings like:
[u'The First Artist', u'The Second Artist']
The way you might use it to copy tags would be something like:
import os.path
import qllib # this is the module that comes with QLCLI
def update_tags(mp3_fn, flac_fn):
mp3 = qllib.AudioFile(mp3_fn)
flac = qllib.AudioFile(flac_fn)
# you can iterate over the tag names
# they will be the same for all file types
for tag_name in mp3:
flac[tag_name] = mp3[tag_name]
flac.write()
mp3_filenames = ['foo.mp3', 'bar.mp3', 'baz.mp3']
for mp3_fn in mp3_filenames:
flac_fn = os.path.splitext(mp3_fn)[0] + '.flac'
if os.path.getmtime(mp3_fn) != os.path.getmtime(flac_fn):
update_tags(mp3_fn, flac_fn)
I have a bash script that does exactly that, atwat-tagger. It supports flac, mp3, ogg and mp4 files.
usage: `atwat-tagger.sh inputfile.mp3 outputfile.ogg`
I know your project is already finished, but somebody who finds this page through a search engine might find it useful.
Here's some example code, a script that I wrote to copy tags between
files using Quod Libet's music format classes (not mutagen's!). To run
it, just do copytags.py src1 dest1 src2 dest2 src3 dest3, and it
will copy the tags in sec1 to dest1 (after deleting any existing tags
on dest1!), and so on. Note the blacklist, which you should tweak to
your own preference. The blacklist will not only prevent certain tags
from being copied, it will also prevent them from being clobbered in
the destination file.
To be clear, Quod Libet's format-agnostic tagging is not a feature of mutagen; it is implemented on top of mutagen. So if you want format-agnostic tagging, you need to use quodlibet.formats.MusicFile to open your files instead of mutagen.File.
Code can now be found here: https://github.com/DarwinAwardWinner/copytags
If you also want to do transcoding at the same time, use this: https://github.com/DarwinAwardWinner/transfercoder
One critical detail for me was that Quod Libet's music format classes
expect QL's configuration to be loaded, hence the config.init line in my
script. Without that, I get all sorts of errors when loading or saving
files.
I have tested this script for copying between flac, ogg, and mp3, with "standard" tags, as well as arbitrary tags. It has worked perfectly so far.
As for the reason that I didn't use QLLib, it didn't work for me. I suspect it was getting the same config-related errors as I was, but was silently ignoring them and simply failing to write tags.
You can just write a simple app with a mapping of each tag name in each format to an "abstract tag" type, and then its easy to convert from one to the other. You don't even have to know all available types - just those that you are interested in.
Seems to me like a weekend-project type of time investment, possibly less. Have fun, and I won't mind taking a peek at your implementation and even using it - if you won't mind releasing it of course :-) .
There's also tagpy, which seems to work well.
Since the other solutions have mostly fallen off the net, here is what I came up, based on the python mediafile library (python3-mediafile in Debian GNU/Linux).
#!/usr/bin/python3
import sys
from mediafile import MediaFile
src = MediaFile (sys.argv [1])
dst = MediaFile (sys.argv [2])
for field in src.fields ():
try:
setattr (dst, field, getattr (src, field))
except:
pass
dst.save ()
Usage: mediafile-mergetags srcfile dstfile
It copies (merges) all tags from srcfile into dstfile, and seems to work properly with flac, opus, mp3 and so on, including copying album art.

Categories

Resources