ActionScript 3.0 web scraper works in flash, but not browser - actionscript-3

so I have this issue. I recently made a flash webscraper to grab a video source link. It works in flash, however when I test in the browser I only get a blank canvas... It does not work. I have no idea as to what the problem is... I just started learning actionscript today, so I'm far from the greatest at it.
import flash.net.*
import flash.events.*;
import flash.display.*;
var theUrl = "http://www.sockshare.com/file/384F14398D224634";
firstStep();
function firstStep() {
var urlReq: URLRequest = new URLRequest(theUrl);
urlReq.method = URLRequestMethod.POST;
var loader: URLLoader = new URLLoader(urlReq);
loader.addEventListener(Event.COMPLETE, onSuccess);
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(urlReq);
function onSuccess(e: Event): void {
var theContents1: String = String(loader.data);
var pattern1: RegExp = /<input type="hidden" value="(?P<innertext>.*?)" name="hash">/;
var result1 = pattern1.exec(theContents1);
goToSecond(result1.innertext);
}
}
function goToSecond(hash: String) {
var urlReq2: URLRequest = new URLRequest(theUrl);
urlReq2.method = URLRequestMethod.POST;
var urlVars2: URLVariables = new URLVariables();
urlVars2.hash = hash;
urlVars2.confirm = 'Continue+as+Free+User';
urlReq2.data = urlVars2;
var loader2: URLLoader = new URLLoader(urlReq2);
loader2.addEventListener(Event.COMPLETE, onSuccess2);
loader2.dataFormat = URLLoaderDataFormat.VARIABLES;
loader2.load(urlReq2);
function onSuccess2(e: Event): void {
var theContents2: String = String(loader2.data);
var pattern: RegExp = /playlist%3A%20%27%2F(?P<innertext>.*?)%27%2C%0D%0A%09plugins/;
var result = pattern.exec(theContents2);
var linkp1: String = "http://www.sockshare.com/" + unescape(result.innertext);
finalStep(linkp1);
}
}
function finalStep(finalUrl: String) {
var urlReq3: URLRequest = new URLRequest(finalUrl);
urlReq3.method = URLRequestMethod.POST;
var loader3: URLLoader = new URLLoader(urlReq3);
loader3.addEventListener(Event.COMPLETE, onSuccess3);
loader3.dataFormat = URLLoaderDataFormat.TEXT;
loader3.load(urlReq3);
function onSuccess3(e: Event): void {
var theContents3: String = String(loader3.data);
var pattern2: RegExp = /<media:content url="(?P<innertext>.*?)" type="/;
var result2 = pattern2.exec(theContents3);
var finalLink: String = result2.innertext;
trace(finalLink);
mainText.text = finalLink;
}
}

It's sandbox problem, read here, if you'll publish as standalone (ex: Air) project it would work

Related

Match two strings from a text file and user input? (AS3)

I was able to load a text file in a Flash file, but I am unable to match two strings from a text file and the user input.
The purpose of this AS3 code: to match the text file and user input, and if it matches, the score will increase by 1. Else, the score will increase by 0.
Here is my code:
var uScore :Number = 0;
stop();
var textLoader:URLLoader = new URLLoader();
var textURLRequest:URLRequest = new URLRequest("q1.txt");
textLoader.addEventListener(Event.COMPLETE, completeHandler);
function completeHandler(event:Event):void
{
var textData:String = new String(textLoader.data);
dy1.text = textData;
}
textLoader.load(textURLRequest);
function goURL(event:MouseEvent):void {
var textLoader2:URLLoader = new URLLoader();
var textURLRequest2:URLRequest = new URLRequest("answer1.txt");
var textData2:String = new String(textLoader2.data);
var name1 = trace(textData2);
textLoader2.load(textURLRequest2);
var myURL = url1.text;
if(myURL == name1){
uScore += 1;
uScoreURL.text = uScore+"";
nextFrame();
}
else{
uScore+=0;
uScoreURL.text = uScore+"";
nextFrame();
}
}
trace(uScore);
You code has a strange assignment:
var name1 = trace(textData2);
replace it with
var name1 =textData2;
it should work then if there isn't a bug in some other place.
And you don't need to uScore+=0;. Just delete it.
I checked out your code, you were doing a few things out of order - here is the revised code that should get you where you need to be
var uScore :Number = 0;
stop();
var textLoader:URLLoader = new URLLoader();
var textURLRequest:URLRequest = new URLRequest("q1.txt");
textLoader.addEventListener(Event.COMPLETE, completeHandler);
function completeHandler(event:Event):void
{
var textData:String = new String(textLoader.data);
dy1.text = textData;
}
textLoader.load(textURLRequest);
btn.addEventListener(MouseEvent.CLICK,getNumber);
var textLoader2:URLLoader = new URLLoader();
textLoader2.addEventListener(Event.COMPLETE, completeHandler2);
function completeHandler2(event:Event):void
{
var textData2:String = new String(textLoader2.data);
var name1 = textData2;
trace(name1);
var myURL = url1.text;
if(myURL == name1){
uScore += 1;
uScoreURL.text = uScore+"";
nextFrame();
}
else{
uScore+=0;
uScoreURL.text = uScore+"";
nextFrame();
}
}
function getNumber(event:MouseEvent){
var textURLRequest2:URLRequest = new URLRequest("answer1.txt");
textLoader2.load(textURLRequest2);
}
trace(uScore);
The only thing that I really added that you didn't have is a button with the variable name btn to check the answer - you can rework that to however you want to check for the answer.

Looking to use Firebase with Actionscript 3 / Air

I have found Firebase, and it looks excellent for javascript / HTML5 usage.
But I was wondering if there is also an Actionscript API?
E.g
var myRootRef = new Firebase('https://myprojectname.firebaseIO-demo.com/');
myRootRef.set('Hello World!');
var dataRef = new Firebase('https://SampleChat.firebaseIO-demo.com/users/fred/name/first');
dataRef.on('value', function(snapshot) {
alert('fred’s first name is ' + snapshot.val());
});
So to set data and have listeners for updated data etc.
Thanks for any help
Matt
Doesn't look like there is an AS3 api available but the good news is that they have a rest api which is cross platform that you can write an AS3 wrapper around. (https://www.firebase.com/docs/rest-api.html) That is what I plan to do. I Want to use it with GameBuilder Studio.
If you are planning on embedding your application inside a web page (that you create) you could use an external javascript interface and communicate to firebase through that.
I'm planning on doing this myself to see how it turns out.
Connecting to Firebase using ActionScript 3 only requires the use of URLRequest and URLLoader. The following examples cover the 4 basic operations (CRUD).
To read data from an specific node from your Firebase database:
private function loadNews():void
{
var request:URLRequest = new URLRequest("https://<YOUR-PROJECT-ID>.firebaseio.com/<Node_to_read>.json");
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, newsLoaded);
loader.load(request);
}
private function newsLoaded(event:flash.events.Event):void
{
trace(event.currentTarget.data);
var rawData:Object = JSON.parse(event.currentTarget.data);
}
To insert data to a specific node:
private function saveEntry(title:String, description:String):void
{
var myObject:Object = new Object();
myObject.title = title;
myObject.description = description;
myObject.timestamp = new Date().getTime();
var request:URLRequest = new URLRequest("https://<YOUR-PROJECT-ID>.firebaseio.com/<Node_to_insert>.json");
request.data = JSON.stringify(myObject);
request.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, entrySent);
loader.load(request);
}
private function entrySent(event:flash.events.Event):void
{
trace(event.currentTarget.data);
}
To delete a specific node:
private function deleteEntry():void
{
var header:URLRequestHeader = new URLRequestHeader("X-HTTP-Method-Override", "DELETE");
var request:URLRequest = new URLRequest("https://<YOUR-PROJECT-ID>.firebaseio.com/<Node_to_delete>.json");
request.method = URLRequestMethod.POST;
request.requestHeaders.push(header);
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, entryDeleted);
loader.load(request);
}
private function entryDeleted(event:flash.events.Event):void
{
trace(event.currentTarget.data);
}
To update/modify data to a specific node:
private function updateEntry(title:String, description:String):void
{
var header:URLRequestHeader = new URLRequestHeader("X-HTTP-Method-Override", "PATCH");
var myObject:Object = new Object();
myObject.title = title;
myObject.description = description;
var request:URLRequest = new URLRequest("https://<YOUR-PROJECT-ID>.firebaseio.com/journal/<Node_to_modify>.json");
request.data = JSON.stringify(myObject);
request.method = URLRequestMethod.POST;
request.requestHeaders.push(header);
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, entryUpdated);
loader.load(request);
}
private function entryUpdated(event:flash.events.Event):void
{
trace(event.currentTarget.data);
}
If you want further information I have written detailed Firebase REST guides and examples on how to use ActionScript 3 and Firebase.

AS3 Video Playback with FLVPlayback

I am using the code below to load some data from an .xml file.
I am preloading all data (Audio Paths, Video Paths including a video from xml.
When everything is loaded complete i am loading the video in Frame 2 on FLVPlayback 2.5 with this code:
videoPlayer.source = videofile;
The problem is that the video shows a white screen for 3-4 seconds and then starts play.
At some other pc's it plays normaly when the loading ends.
My Code:
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
stop();
//******************************************************
// XML Loader
//******************************************************
var myLoader:URLLoader = new URLLoader();
//myLoader.load(new URLRequest("myxml.php"));
myLoader.load(new URLRequest("myxml.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void{
var myXml:XML = new XML(e.target.data);
parseXML(myXml);
}
//******************************************************
// Extract XML value and fill up variables
//******************************************************
var thename:XML;
var soundpath:XML;
var theage:XML;
var theplace:XML;
var everyday:XML;
var youwill:XML;
var pic1:XML;
var pic2:XML;
var pic3:XML;
var videofile:XML;
var assetsList:Array;
//-----------------------------
var sound:Sound;
var soundChannel:SoundChannel;
//-----------------------------
function parseXML(xml:XML):void{
thename = xml.paths.thename[0];
soundpath = xml.paths.soundpath[0];
theage = xml.paths.theage[0];
theplace = xml.paths.theplace[0];
everyday = xml.paths.everyday[0];
youwill = xml.paths.youwill[0];
pic1 = xml.paths.pic1[0];
pic2 = xml.paths.pic2[0];
pic3 = xml.paths.pic3[0];
videofile = xml.paths.videofile[0];
txtThename.text = thename;
txtSoundpath.text = soundpath;
txtTheage.text = theage;
txtTheplace.text = theplace;
txtEveryday.text = everyday;
txtYouwill.text = youwill;
txtPic1.text = pic1;
txtPic2.text = pic2;
txtPic3.text = pic3;
txtVideofile.text = videofile;
assetsList = [soundpath,theage,theplace,everyday,youwill,pic1,pic2,pic3,videofile];
preloadAssets();
}
//******************************************************
// preloaded assets
//******************************************************
var assetsLoader:URLLoader
var assetsCtr:Number=0;
function preloadAssets():void{
assetsLoader = new URLLoader ();
var urlRequest:URLRequest = new URLRequest(assetsList[assetsCtr]);
assetsLoader.load(urlRequest);
assetsLoader.addEventListener(Event.COMPLETE, assetLoadedHanlder);
assetsLoader.addEventListener(ProgressEvent.PROGRESS, assetProgressHandler);
}
function assetProgressHandler(evt:ProgressEvent):void{
var bl:uint = evt.bytesLoaded;
var bt:uint = evt.bytesTotal;
var perEachAssets = 1/assetsList.length;
var assetsBlLoaded = ((bl / bt)*perEachAssets)+((assetsCtr)/assetsList.length*100)/100;
var _percentLoaded = Math.floor(assetsBlLoaded*100);
progBar.setProgress(_percentLoaded,100)
//trace("_percentLoaded:",_percentLoaded)
}
function assetLoadedHanlder(evt:Event):void{
assetsCtr+=1;
if(assetsCtr<assetsList.length){
//trace("preloading:"+assetsList[assetsCtr])
var urlRequest:URLRequest = new URLRequest(assetsList[assetsCtr]);
assetsLoader.load(urlRequest);
}else{
//trace("done!")
assetsLoader.removeEventListener(Event.COMPLETE, assetLoadedHanlder);
assetsLoader.removeEventListener(ProgressEvent.PROGRESS, assetProgressHandler);
gotoAndStop(2);
}
}
Rather than adding the component directly to the stage you might want to try creating and adding it with ActionScript.
By doing this you can instantiate the FLVPlayback instance before you need to show it, rather than having to wait until you hit frame 2 on your timeline.
I can't guarantee it will fix your problem but it's worth a go.
var _videoFLV:FLVPlayback;
_videoFLV = new FLVPlayback();
_videoFLV.fullScreenTakeOver = false;
_videoFLV.autoPlay = false;
_videoFLV.autoRewind = true;
_videoFLV.isLive = false;
_videoFLV.skin = null;
_videoFLV.bufferTime = .1;
_videoFLV.width = 320;
_videoFLV.height = 240;
_videoFLV.source = videofile;
_videoFLV.stop();
_videoFLV.x = 240;
_videoFLV.y = 240;
addChild(_videoFLV);

Convert AS 3.0 into AS 2.0 for JSON Encode & Decode

I have written an AS 3.0 code for JSON Encode and Decode, but I need the below code written in AS 2.0. I know AS 3.0 but don't know AS 2.
Here is the code:
stop();
var getFBId:String = ExternalInterface.call("getFBIdFromJS");
var getFBName:String = ExternalInterface.call("getFBNameFromJS");
import com.adobe.serialization.json.JSON;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import fl.transitions.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.display.MovieClip;
import flash.events.MouseEvent;
playerInformation.visible = true;
var loginObj:Object = new Object();
var fbId:String = new String();
var Id:String = new String();
var varUid:String = new String();
var ImageUrl:String = new String();
var CreditBalance:String = new String();
//var PointBalance:String = new String();
var CoinBalance:String = new String();
var IsDailyBonus:String = new String();
var DailyBonusAmount:int = new int();
var fbvarsLoader:URLLoader = new URLLoader();
var fbvarsReq:URLRequest = new URLRequest("fbvars.php");
var fbvarsVariables:URLVariables = new URLVariables();
fbvarsLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
fbvarsReq.method = URLRequestMethod.POST;
fbvarsReq.data = fbvarsVariables;
fbvarsLoader.load(fbvarsReq);
fbvarsLoader.addEventListener(Event.COMPLETE, receiveLoad);
function receiveLoad(evt:Event):void
{
fbId = getFBId;
varUid = getFBName;
ImageUrl = "https://graph.facebook.com/" + getFBId + "/picture";
trace (getFBId);
trace (getFBName);
trace (ImageUrl);
loginObj.FacebookId = fbId;
loginObj.UserName = varUid;
loginObj.PlatformId = 1;
loginReq.data = JSON.encode(loginObj);
loginLoader.load(loginReq);
trace("login ENCODE: " + JSON.encode(loginObj));
FBlogindata.text = JSON.encode(loginObj);
}
//--------------- FB Vars (E)
var serverURL:String = "http://serverURL";
var playerPicLoader:Loader = new Loader();
//--------------------------------
var loginReq: URLRequest = new URLRequest();
loginReq.method = URLRequestMethod.POST;
loginReq.url =
"http://serverURL";
var loginLoader:URLLoader = new URLLoader();
loginLoader.addEventListener(Event.COMPLETE, onComplete_login);
function onComplete_login(e_login:Event)
{
var loginReturn:Object=JSON.decode(e_login.target.data,true);
trace("login DECODE: " + e_login.target.data);
logindata.text = e_login.target.data;
if (loginReturn.Player.Status == "Valid")
{
Id = String(loginReturn.Player.Id);
CreditBalance = String(loginReturn.Player.CreditBalance);
var playerPic:URLRequest = new URLRequest(ImageUrl);
playerPicLoader.load(playerPic);
playerInformation.mcPlayerThumbHolder.addChild(playerPicLoader);
playerInformation.txtPlayerName.text = varUid;
playerInformation.txtPlayerCredit.text = CreditBalance;
playerInformation.visible = true;
UIDShow.text = Id;
FBIDShow.text = fbId;
if (this.parent.parent != null){
trace (CreditBalance);
MovieClip(this.parent.parent).credit = CreditBalance;
}
}
}
Can any one write the above code in AS 2.0?
Here is implementation of AS2 JSON parser. There are two static methods stringify() - to encode into JSON and parse() to decode. So you don't have to write your own implementation. For other stuff you should probably sit and learn some things about AS2 or find someone who still remember AS2 :)

Actionscript 3 - DataGridColumn and having a specific column automatically sorted

I am using the DataGrid component in Flash, that is loaded with data via a external XML file. I have a column, A (Serial), which once loaded, I'd like for the information to be sorted Ascendingly, automatically. Does anyone have any idea on how to do this?
Here is my code:
import fl.controls.DataGrid;
import fl.data.DataProvider;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.controls.ScrollPolicy;
import fl.events.DataGridEvent;
var dp:DataProvider;
var A:DataGridColumn = new DataGridColumn("Serial");
A.headerText = "Serial No.";
A.width = 100;
A.resizable = false;
var B:DataGridColumn = new DataGridColumn("Mold");
B.headerText = "Mold No.";
B.width = 150;
B.resizable = false;
var C:DataGridColumn = new DataGridColumn("Type");
C.headerText = "Grid Type: ";
C.width = 350;
C.resizable = false;
var myDataGrid:DataGrid = new DataGrid();
myDataGrid.addColumn(A);
myDataGrid.addColumn(B);
myDataGrid.addColumn(C);
myDataGrid.verticalScrollPolicy = ScrollPolicy.ON;
myDataGrid.setSize(600, 800);
myDataGrid.move(0, 0);
myDataGrid.addEventListener(DataGridEvent.HEADER_RELEASE, headerReleaseHandler);
addChild(myDataGrid);
var url:String = "xml/TEST.xml";
var req:URLRequest = new URLRequest(url);
var uLdr:URLLoader = new URLLoader();
uLdr.addEventListener(Event.COMPLETE, completeHandler);
uLdr.load(req);
function completeHandler(event:Event):void {
var ldr:URLLoader = event.currentTarget as URLLoader;
var xmlDP:XML = new XML(ldr.data);
dp = new DataProvider(xmlDP);
myDataGrid.dataProvider = dp;
}
function headerReleaseHandler(event:DataGridEvent):void {
var dg:DataGrid = event.currentTarget as DataGrid;
trace("column: " + String(event.dataField));
trace("descending: " + String(dg.sortDescending));
}
Thanx
Try the sortItemsOn() method on the DataGrid.
See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/controls/SelectableList.html#sortItemsOn() for an example how to use it.