Page 1 of 1

Access Connection for standard tags - Python code ..

Posted: Tue Jul 22, 2008 8:25 pm
by Otonicara
Bellow supose to work. Unfortunatly there is still problem beacouse py code is not triggered by hook. Hook is attached to the chanal generated from search results (I need export to access only for news that are matched with keywords). I have tried also with compiled py code (exe) but also no result.

For test I also Commenting win32api.GetProfileVal lines and remove comments on test variables under SQL Parameter Test Values and than manualy double clicked py file. Access was filled with fixed values. Than I change keyword and update the chanal. In case hook would triger py code Access should be filled again with fixed values. Unfortunatly this was not happened. Please check to code bellow to spot pontional abnormalities. I have a feeling that we are now close the final solution..


PY and Hook code are saved on new sub (name AcccesExport) dir of ChannelHooks dir. In hook file I didn't parametrized any [GlobalChannelHookParameterDefinition-1] values as I do not need any those values for control purposes.

PY code: RSSExportToAccess.py

import sys
import pyodbc
import win32api


#get the name of the INI file
configFilename = sys.argv[1]

# get our channel values
xTicker = win32api.GetProfileVal( "ChannelParameters" , "Category" , "" , configFilename )
xTitle = win32api.GetProfileVal( "ChannelParameters" , "Title" , "" , configFilename )
xDescription = win32api.GetProfileVal( "ChannelParameters" , "Description" , "" , configFilename )
xURL = win32api.GetProfileVal( "ChannelParameters" , "Link" , "" , configFilename )
xTime = win32api.GetProfileVal( "ChannelParameters" , "Published" , "" , configFilename )

# Bellow is just for test purpose. In test mentioned above I here removed comments and put comments on above win32api.GetProfileVal lines in order to confirm that SQL/ Access connection is working normaly.
# SQL Parameter Test values ..
#xTicker = "AWA"
#xTitle = "Test naslov"
#xDescription = "Bla bla bla"
#xURL = "http://www.google.com"
#xTime = "20081015"


# Access Database Connection
DBfile = 'C:/Documents and Settings/OtonicarA_01/My Documents/BAZA_BORZA/Stock_Data.mdb'
conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};DBQ='+DBfile, autocommit=True)
cursor = conn.cursor()


# SQL Statement and SQL Excecution
cursor.execute("INSERT INTO TBL_StockNews (Ticker,Title,Description,Link,PublishTime) VALUES (?,?,?,?,?)", (xTicker,xTitle,xDescription,xURL,xTime))

# Closing ..
cursor.close()
conn.close()


Hook: RSSExportToAccess.hook

[Config]
ScriptFilename=RSSExportToAccess.py
DisplayName=Save News-Feed Items to Access db
AuthorName=OtonicarA
AuthorEmailAddress=ales@otonicar.com
Notes=This channel hook saves the channel's individual feed items to Access database.

' ---------------------------------------------------------------------

[Events]
NewFeedItems=1

' ---------------------------------------------------------------------

Re: Access Connection for standard tags - Python code ..

Posted: Wed Jul 23, 2008 6:04 am
by support
You can put some message boxes in or log to a file (like <tt>LogChannelActivity</tt> does) to see what's happening but I'll bet what's happening is quite simple: the channel is not receiving any new items.

Your hook only gets run when a new item is received so if you update the channel and nothing new arrives, the attached hooks won't get called (unless you register for <tt>Begin/EndChannelUpdate</tt> events).

Strange path ..

Posted: Wed Jul 23, 2008 2:42 pm
by Otonicara
ok-ej using updateChanall event solve the problem of running the py code. It is strange as I change key words firts and than run update. This brings new Items into the chanell. But Ok that at least working now ..

Problem is that news values are still not transfered to access. Fixed values were transfred (so connection to access is okej). Values form hook werent transfred (in access I recived empty columns for these values)

Testing:
I inserted bellow to the code to get file location:
# Write INI File Name to AwasuLog.txt file
f = open('C:/Documents and Settings/OtonicarA_01/My Documents/BAZA_BORZA/AwasuLogFile.txt', 'r+')
f.write(configFilename)
f.close()

in TXT file was written:
C:\DOCUME~1\OTONIC~1\LOCALS~1\Temp\awasu121

Later I was traied to write the contents of INI file using code:
# Write INI file contents to AwasuLog.txt file
f.open(configFilename)
INI = f.read()
f.close()
f = open('C:/Documents and Settings/OtonicarA_01/My Documents/BAZA_BORZA/AwasuLogFile.txt', 'r+')
f.write(INI)
f.close()

Code reported error. Nothing was written in TXT file.

It migh be the problem strange path - it is not complete...
C:\DOCUME~1\OTONIC~1\LOCALS~1\Temp\awasu121
Probably this is the reason that win32api.GetProfileVal function reply with empty values...

Questions:
What can be done that command
configFilename = sys.argv[1]
would reply currect full path ..

Re: Strange path ..

Posted: Wed Jul 23, 2008 3:20 pm
by support
This all sounds correct. The path to the temp file you are seeing is correct and complete.

I think the problem is that you're guessing at what the format of the INI file is and trying to read values out of it that don't exist. Attach the <tt>LogChannelActivity</tt> hook to your channel so you can see *exactly* what it is you're receiving. Then you can modify your code to read the correct values out of it.