Get size of Wordpress RSS feed? - wordpress

The scenario:
I have a flash website (here) that imports some of its content from a wordpress site (here) via the Wordpress's RSS (I added a plugin to include pages in the RSS). This isn't a brilliant idea I know, but for a long list of reasons that I won't go into it's the setup I'm stuck with :/
The Problem:
When importing the RSS feed (it's downloaded into the SWF at runtime and read like an XML), I can't get the filesize. This means the preloader isn't accurate (I just manually guessed a number for it to work with for now, which obviously will grow over time). How do I get this file size?
I know there may be certain settings on the server that compresses the feed/files (GZip or such), but I'm very unfamiliar with wordpress architecture and despite my best efforts I couldn't find where this setting would be applied.
This is the preloader update code. The 113*1024 is where I want to put rss_loader.bytesTotal (the loader of the WordPress content), but when I do rss_loader.bytesTotal returns as 0:
function update_loading(e:Event):void
{
var total:Number = stage.loaderInfo.bytesTotal + (113 * 1024) + css_loader.bytesTotal;
var loaded:Number = stage.loaderInfo.bytesLoaded + rss_loader.bytesLoaded + css_loader.bytesLoaded;
if (total == loaded)
{
updatePicture(360);
loading_mc.perc.text = "100%";
removeEventListener(Event.ENTER_FRAME,update_loading);
nextFrame();
}
else
{
var deg:Number = (loaded / total) * 360;
updatePicture(deg);
loading_mc.perc.text = String(int((loaded / total) * 100))+"%";
}
}
Here's the RSS import code, though I don't believe it's very relevant:
var rss_xml:XML = new XML();
var post_xml:XML = new XML();
var page_xml:XML = new XML();
var rss_loader:URLLoader;
rss_loader = new URLLoader(new URLRequest("http://news.sulsc.org/feed"));
function rss_loaded(e:Event):void
{
rss_xml = XML(e.target.data);
var rss_raw:String = String(rss_xml);
rss_raw = rss_raw.split(":encoded").join("");
rss_raw = rss_raw.split("\n").join("");
rss_raw = rss_raw.split('<p style="text-align: right;">').join('<p class="right">');
rss_raw = rss_raw.split('<p style="text-align: center;">').join('<p class="centre">');
rss_raw = rss_raw.split('<p style="text-align: left;">').join('<p class="left">');
rss_raw = rss_raw.split('<p style="text-align: justify;">').join('<p class="just">');
var heading_replace:RegExp = new RegExp("(</h[0-9]>)(<h[0-9]>)","g9");
var underline_replace:RegExp = new RegExp('<span style="text-decoration: underline;">(.*?)</span>',"gi");
var bold_replace:RegExp = new RegExp('<strong>(.*?)</strong>',"gi");
var italic_replace:RegExp = new RegExp('<em>(.*?)</em>',"gi");
rss_raw = rss_raw.replace(heading_replace,'$1<p class="space"></p>$2');
rss_raw = rss_raw.replace(underline_replace,'<u>$1</u>');
rss_raw = rss_raw.replace(bold_replace,'<b>$1</b>');
rss_raw = rss_raw.replace(italic_replace,'<i>$1</i>');
var page_test:RegExp = new RegExp("http://(news.|www.|)sulsc\\.org/(|wordpress/)\\?page_id=[0-9]+","gi");
page_xml = XML(rss_raw);
post_xml = XML(rss_raw);
rss_xml = null;
for (var i:uint; i<post_xml.channel.item.length(); i++)
{
if (page_test.test(post_xml.channel.item[i]) == true)
{
post_xml.channel.item[i] = "";
i--;
}
if (i == post_xml.channel.item.length()-1)
{
rss_loader.bytesLoaded = 113 * 1024;
}
}
}

I ran into a similar issue with a project last year. Turns out the server wasn't returning the correct header for Flash to pick up on. I can't remember which header it needs to populate bytesTotal, unfortunately, but I did come up with a quick work around.
this.urlLoader.addEventListenever( HTTPStatusEvent.HTTP_RESPONSE_STATUS, this.statusResponse );
private function statusResponse(e:HTTPStatusEvent = null):void{
for ( var i:Number = 0; i < e.responseHeaders.length; i++ ) {
var current:Object = e.responseHeaders[i];
if ( current.name.toLowerCase() == "content-length" ) {
this.bytesTotal = current.value;
break;
}
}
}
This worked for me, although it obviously relies on the server sending a Content-Length header.

Related

Restrict typeform to single submission on Wordpress post

Have a simple typeform embedded into a post in Wordpress. Nothing fancy at all. Embed code pulled direct from Typeform.
However, people can submit multiple times. ie. One person could theoretically do it 100 times.
Typeform have advised a cookie will solve this, and restrict a user to a single submission - but really do not know where to begin there. Is there a simple, quick fix that could do such a thing? Any ideas completely welcome!
One of the solutions could be only showing the embedded typeform if the cookie does not exist.
not really sure how you would do this in Wordpress.
But the logic is:
const embedElement = document.querySelector('.target-dom-node')
const displayed = getCookie("displayed_typeform");
if (displayed){
embedElement.innerHTML="<h2>Typeform already displayed once.</h2>"
} else if(!displayed && displayed === "") {
setCookie("displayed_typeform", true, 365);
showEmbed();
}
setCookie and getCookie are two functions that deal with cookie management
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
You can find a complete project that demonstrates this feature here.

Using Page Hits for Segmentation in Adobe CQ5

I am trying to set up personalised content in CQ5 using segmentation. When I use the out of the box "Page Hits" option it doesn't work. Is there some extra configuration I have to do to use Page Hits?
I've set up two segments applied to two teaser pages. For the first one I've used
number of page hits is less than 4.
For the second I've used number of page hist is greater than 3.
Note, the teasers show up when I use Referral Keywords to test so I think the rest of the configuration is correct.
Can anyone give some advice about how to get the Page Hits segmentations to work?
Just in case anyone else has this same problem, I solved it by using a session store and set a cookie on the users browser to record how many times they had been to a particular page. Using that, I was able to configure my segments and personalise areas of the page based on number of visits the user had made to that page.
Code for the session store:
//Create the session store
if (!CQ_Analytics.MyStore) {
CQ_Analytics.MyStore = new CQ_Analytics.PersistedSessionStore();
CQ_Analytics.MyStore.STOREKEY = "MYSTORE";
CQ_Analytics.MyStore.STORENAME = "myclientstore";
CQ_Analytics.MyStore.data={};
CQ_Analytics.MyStore.findPageName = function(){
var locationName = location.pathname;
var n = location.pathname.indexOf("html");
if(n !== -1){
locationName = locationName.split('.')[0];
}
return locationName.split("/").slice(-1);
}
CQ_Analytics.MyStore.title = CQ_Analytics.MyStore.findPageName() + "-pageviews";
CQ_Analytics.MyStore.loadData = function(pageViewed) {
CQ_Analytics.MyStore.data = {"pageviewed":pageViewed};
}
CQ_Analytics.MyStore.getCookie = function(cname) {
console.log("getting the cookie");
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0){
console.log("return value for cookie is " + c.substring(name.length,c.length) );
return c.substring(name.length,c.length);
}
}
return "";
}
CQ_Analytics.MyStore.setCookie = function(cname, cvalue, exdays) {
console.log("setting the cookie");
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
CQ_Analytics.MyStore.checkCookie = function() {
console.log("checking for cookie");
var pViewd = CQ_Analytics.MyStore.getCookie(CQ_Analytics.MyStore.title);
if (pViewd != "") {
console.log("cookie is found and Viewed is " + pViewd);
pViewd = parseInt(pViewd) + 1;
CQ_Analytics.MyStore.setCookie(CQ_Analytics.MyStore.title, pViewd, 365);
CQ_Analytics.MyStore.loadData(pViewd.toString());
} else {
if (pViewd === "" || pViewd === null) {
console.log("cookie not found");
CQ_Analytics.MyStore.setCookie(CQ_Analytics.MyStore.title, "1", 365);
CQ_Analytics.MyStore.loadData("1");
}
}
}
CQ_Analytics.MyStore.checkCookie();
}
//register the session store
if (CQ_Analytics.CCM){
CQ_Analytics.CCM.register(CQ_Analytics.MyStore)
}
The most useful documentation I found was this: https://docs.adobe.com/docs/en/cq/5-6-1/developing/client_context_detail.html#par_title_34

Asynchronous multipart uploads to Amazon S3 with ASP.NET

I am able to initiate asynchronous uploads to S3, however they are somehow not ending up as a file inside my S3 bucket and I see an error 'WithPartETags cannot be empty'. Here is the complete code
InitiateMultipartUploadRequest initRequest =
new InitiateMultipartUploadRequest()
.WithBucketName(existingBucketName)
.WithKey(Path.Combine(S3Path + "/", finfo.Name));
InitiateMultipartUploadResponse initResponse =
s3Client.InitiateMultipartUpload(initRequest);
// 2. Upload Parts.
long contentLength = finfo.Length;
long partSize = 15728640;//52428800-50MB 104857600- 100 MB - 5242880 - 5 MB
try
{
long filePosition = 0;
for (int i = 1; filePosition < contentLength; i++)
{
// Create request to upload a part.
UploadPartRequest uploadRequest = new UploadPartRequest()
.WithBucketName(existingBucketName)
.WithKey(Path.Combine(S3Path + "/", finfo.Name))
.WithUploadId(initResponse.UploadId)
.WithPartNumber(i)
.WithPartSize(partSize)
.WithFilePosition(filePosition)
.WithFilePath(finfo.FullName);
// Upload part and add response to our list.
//uploadResponses.Add(s3Client.UploadPart(uploadRequest));
IAsyncResult ar = s3Client.BeginUploadPart(uploadRequest, null, null);
ListObj.Add(new ThreadList() { _iasyncResult = ar });
filePosition += partSize;
Console.WriteLine("Length Written - " + filePosition + " .Content Length - " + contentLength);
}
bool uploadsComplete = false;
while (!uploadsComplete)
{
bool individualuploadscomplete = true;
foreach (var obj in ListObj)
{
if (!obj._iasyncResult.IsCompleted)
{
individualuploadscomplete = false;
break;
}
}
if (individualuploadscomplete)
{
uploadsComplete = true;
}
}
foreach (var obj in ListObj)
{
s3Client.EndUploadPart(obj._iasyncResult);
}
//// Step 3: complete.
CompleteMultipartUploadRequest compRequest =
new CompleteMultipartUploadRequest()
.WithBucketName(existingBucketName)
.WithKey(Path.Combine(S3Path + "/", finfo.Name))
.WithUploadId(initResponse.UploadId);
//.WithPartETags(uploadResponses);
CompleteMultipartUploadResponse completeUploadResponse =
s3Client.CompleteMultipartUpload(compRequest);
Not sure why you have the setting of the PartETags commented out for the complete multipart upload call but you need to add that code back in. Also when you are calling the EndUploadPart method you need to capture that UploadResponse that comes back from that.
You also might want to look into the TransferUtility found in the Amazon.S3.Transfer namespace. Its upload methods are designed to handle what you are attempting to accomplish for large objects, see Using the High-Level .NET API for Multipart Upload for details and example snippets.

Replace multiple different images on one PDF template page with itext (itextsharp)

We have an ASP.NET application that users use to generate certain reports. So far we had one PDF template that had one image on it, and we would just replace that image with our programatically generated one (graph).
We have used code from this site for that:http://blog.rubypdf.com/2007/12/12/how-to-replace-images-in-a-pdf/
Problem now is that we have two different images on one PDF page, and the code from link above selects both images on one page and replaces them all at once with our generated image.
Does anyone have any idea how to replace multiple different images on one page with itext?
Thanks
Ugh. First, let me rewrite some of that source.
PdfReader pdf = new PdfReader("in.pdf");
PdfStamper stp = new PdfStamper(pdf, new FileOutputStream("c:\\out.pdf"));
PdfWriter writer = stp.getWriter();
Image img = Image.getInstance("image.png");
PdfDictionary pg = pdf.getPageN(1);
PdfDictionary res = pg.getAsDict.get(PdfName.RESOURCES);
PdfDictionary xobj = res.getAsDict(PdfName.XOBJECT);
if (xobj != null) {
for (Iterator<PdfName> it = xobj.getKeys().iterator(); it.hasNext(); ) {
PdfObject obj = xobj.get(it.next());
if (obj.isIndirect()) {
PdfDictionary tg = (PdfDictionary)PdfReader.getPdfObject(obj);
PdfName type = tg.getAsName(PdfName.SUBTYPE));
if (PdfName.IMAGE.equals(type)) {
PdfReader.killIndirect(obj);
Image maskImage = img.getImageMask();
if (maskImage != null)
writer.addDirectImageSimple(maskImage);
writer.addDirectImageSimple(img, (PRIndirectReference)obj);
break;
}
}
}
}
Whew. the getAs functions can save you quite a bit of knuckle-grease and make your code much clearer.
Now. You need to be able to differentiate between the various images. If you're willing to hard-code things, you could find out what the resource names are and go that route:
String imageResName[] = {"Img1", "Img2" ... };
Image img[] = {Image.getInstance("foo.png"), Image.getInstance("bar.png"), ... };
for (int i = 0; i < imageResName.length; ++i) {
PdfName curKey = new PdfName(imageResName[i]);
PdfIndirectReference ref = xobj.getAsIndirect(curKey);
PdfReader.killIndirect( ref );
Image maskImage = img[i].getImageMask();
if (maskImage != null) {
writer.addDirectImageSimple(maskImage);
}
writer.addDirectImageSimple(img[i], (PRIndirectReference)ref);
}
If you're not willing to go with hardcoded resource names (and no one would fault you, quite the opposite, particularly when the order they appear (and thus the number on the end) depends on their order in a hash map... [shudder]), you may be able to differentiate based on image width and height.
//keep the original for loop, stepping through resource names
if (PdfName.IMAGE.equals(type)) {
float width = tg.getAsNumber(PdfName.WIDTH).floatValue();
float height = tg.getAsNumber(PdfName.HEIGHT).floatValue();
Image img = getImageFromDimensions(width, height);
Image maskImage = img.getImageMask();
...
}
Just a note that sometimes the image will be nested in a form, so it is wise to make a function that will be called recursively.
Something like this:
public void StartHere()
{
PdfReader pdf = new PdfReader("in.pdf");
PdfStamper stp = new PdfStamper(pdf, new FileOutputStream("c:\\out.pdf"));
PdfWriter writer = stp.getWriter();
Image img = Image.getInstance("image.png");
PdfDictionary pg = pdf.getPageN(1);
replaceImage(pg, writer,img);
}
private void replaceImage(PdfDictionary pg, PdfWriter writer,Image img)
{
PdfDictionary res = pg.getAsDict.get(PdfName.RESOURCES);
PdfDictionary xobj = res.getAsDict(PdfName.XOBJECT);
if (xobj != null) {
for (Iterator<PdfName> it = xobj.getKeys().iterator(); it.hasNext(); ) {
PdfObject obj = xobj.get(it.next());
if (obj.isIndirect()) {
PdfDictionary tg = (PdfDictionary)PdfReader.getPdfObject(obj);
PdfName type = tg.getAsName(PdfName.SUBTYPE));
if (PdfName.IMAGE.equals(type))
{
PdfReader.killIndirect(obj);
Image maskImage = img.getImageMask();
if (maskImage != null)
writer.addDirectImageSimple(maskImage);
writer.addDirectImageSimple(img, (PRIndirectReference)obj);
break;
}
else if(PdfName.FORM.equals(type))
{
replaceImage(tg, writer,img);
}
}
}
}

Programmatically Mixdown of audio tracks (no playback)

I've found some excellent demos of how to mix together sound objects together for live playback. See the working example bellow...
But can it be done programmatically without any playback so I can just output the mixed file? Also I'll be adding some volume change info along the way so it'll need to be added in small chunks like how the play buffer works.
[Embed(source = "audio/track01.mp3")]
private var Track1:Class;
[Embed(source = "audio/track02.mp3")]
private var Track2:Class;
[Embed(source = "audio/track03.mp3")]
private var Track3:Class;
[Embed(source = "audio/track04.mp3")]
private var Track4:Class;[Embed(source = "AudioMixerFilter2.pbj",mimeType = "application/octet-stream")]
private var EmbedShader:Class;
private var shader:Shader = new Shader(new EmbedShader());
private var sound:Vector.<Sound> = new Vector.<Sound>();
private var bytes:Vector.<ByteArray> = new Vector.<ByteArray>();
private var sliders:Vector.<Number> = new Vector.<Number>();
private var sliderVol:int = 1;
private var BUFFER_SIZE:int = 0x800;
public var playback:Sound = new Sound();
public function startAudioMixer(event:FlexEvent):void{
sound.push(new Track1(), new Track2(), new Track3(), new Track4());
sliders.push(sliderVol,sliderVol,sliderVol,sliderVol);
playback.addEventListener(SampleDataEvent.SAMPLE_DATA, onSoundData);
playback.play();
}
private function onSoundData(event:SampleDataEvent):void {
for(var i:int = 0; i < sound.length; i++){
bytes[i] = new ByteArray();
bytes[i].length = BUFFER_SIZE * 4 * 2;
sound[i].extract(bytes[i], BUFFER_SIZE);
var volume:Number = 0;
bytes[i].position = 0;
for(var j:int = 0; j < BUFFER_SIZE; j++){
volume += Math.abs(bytes[i].readFloat());
volume += Math.abs(bytes[i].readFloat());
}
volume = (volume / (BUFFER_SIZE * .5)) * sliderVol; // SLIDER VOL WILL CHANGE
shader.data['track' + (i + 1)].width = BUFFER_SIZE / 1024;
shader.data['track' + (i + 1)].height = 512;
shader.data['track' + (i + 1)].input = bytes[i];
shader.data['vol' + (i + 1)].value = [sliders[i]];
}
var shaderJob:ShaderJob = new ShaderJob(shader,event.data,BUFFER_SIZE / 1024,512);
shaderJob.start(true);
}
Easiest way would be to just forget about the Pixel Bender stuff.
Once the Sounds are loaded, use an ENTER_FRAME that uses Sound.extract to get a smallish ByteArray from each Sound, then read through all four extracted ByteArrays doing some basic math to arrive at the 'mixed' values for the left & right signals. Write those values to the "final/mixed/output" ByteArray. Repeat the process each frame until you're at the end of the sounds. If the Sounds aren't all the identical length, you'll need to figure out how to handle that as well.
If you need to perform a mix where the amplitude of each track changes over time, it'd be a good challenge, but would take time to set up.
While you're at it, check out Andre Michelle's Tonfall project... It's a complex but great place to start with understanding the ins/outs of audio in AS3.

Resources