This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
campicus's profile picture

Automatically Update Pastebin Programs

Started by campicus, 02 October 2013 - 10:42 PM
campicus #1
Posted 03 October 2013 - 12:42 AM
I want to make a program that automatically updates the programs on my CC computer, from their pastebin locations. If possible, I want to be able to do this without manually entering their pastebin codes (HJKdKJa).

Cranium made an amazing program (smart paste 2.0) that you can log into your pastebin account from in game and view your pastebin programs. I have spent ages looking through his code and I cannot figure out how he did it but I know it must be possible.

As such, I should be able to have something like:


progs = fs.list("/campicus")

function logIn()
  -logs into pastebin-
  pastebinProgs = -get a list of my programs from pastebin-
end

function update()
  for _, file in ipairs (progs) do
	-match file to the correct program in the table "pastebinProgs"-
	-delete old version-
	-get new version from pastebin and save it-
  end
end

Hopefully this isn't too vague and you guys have an idea of what I am wanting to do. I just have no idea how to go about it. I am familiar with the fs and http apis and I have looked through the smart paste code, to no avail.

Help me oh great coders!
svdragster #2
Posted 03 October 2013 - 04:45 AM
I don't really get the question.

Do you want a program to list all your pastebins, or automatically update a program when you run it? The last one is really simple ^^
jay5476 #3
Posted 03 October 2013 - 05:43 AM
I think he wants to be able to edit his pastebin programs( its possible to login and get programs http://www.computercraft.info/forums2/index.php?/topic/6030-smartpaste-20-is-released/ program someone made)
by itterating through /campicus directory and if its name matchs 1 of his paste names update it
AgentE382 #4
Posted 03 October 2013 - 11:59 PM
I'll write something for you tomorrow or Saturday. Your question has given me an idea for an API, besides the program you want.
campicus #5
Posted 04 October 2013 - 09:56 AM
I don't really get the question.

Do you want a program to list all your pastebins, or automatically update a program when you run it? The last one is really simple ^^

A program that will look at the programs on my CC computer and update them (from my pastebin). This way I can edit my programs on my real life computer, upload the updated version to pastebin and my programs will all stay up-to-date ingame.

I'll write something for you tomorrow or Saturday. Your question has given me an idea for an API, besides the program you want.

Sounds good to me, I can learn how by looking at any code you write. Cheers mate
AgentE382 #6
Posted 04 October 2013 - 06:12 PM
Alright. I'm almost finished with the basic functionality of a full Pastebin API. I'll post it when I finish it tonight or tomorrow.

After I finish the API, I'll write a program that will allow you to update all of your programs at once, or can be put in your startup file to automatically update when you restart. Anything beyond that, and you're getting into OS programming.

I'll see if I can make an update detection API while I'm writing the program. I'd have to scrape Pastebin because they don't provide an API for viewing the details about others' pastes.
AgentE382 #7
Posted 05 October 2013 - 06:57 PM
Okay. My Pastebin API is posted here: http://www.computercraft.info/forums2/index.php?/topic/15498-pastebin-api/

The program using it will be done tonight.
jamd315 #8
Posted 05 October 2013 - 09:16 PM
If you are looking to auto update a program, you could upload a pastebin with something like the following, and edit it whenever.

local version = "1.0"
local paste = (The pastebin link to new code here)

And compare version with a version in your program (fs API)

shell.run("pastebin", "get", "reference code", " checkVersion")
local check = fs.open("checkVersion")
local data = check.readLine(1)
return data --(the local version="1.0" from before)
if version > myVersion then
local paste = check.readLine(2)
shell.run("pastebin", "get", paste, "yourprogramhere")
shell.run("yourprogramhere")
else
shell.run("yourprogramhere")
end
I strongly recommend checking my fs API work, as I am about to fall asleep and probably messed it up some how. Goodnight!


EDIT:
The readLine() won't work, use a table instead. Also, fs.open needs to specify if read or write.

local v = {}
local check = fs.open("checkVersion", "r")
local data = check.readAll()
v = return textutils.unserialize(data)
Also, need to delete old program

fs.copy("oldprogram", "oldprogram.bak")
fs.delete("oldprogram")
shell.run("pastebin", "get", "reference code", yourprogramhere")
Finally, use a table

if tonumber(v[version]) > tonumber(myVersion)
campicus #9
Posted 06 October 2013 - 10:05 AM
Will check out your api asap mate, thanks for your work!
Cozzimoto #10
Posted 06 October 2013 - 02:07 PM
from my understanding of looking at files and comparing them to one or another, you can do one of two things, you can have a build version somewhere in a comment or in a function and download it as a temp and look up the file you downloaded and compare it to the file you already have on your computercraft computer, or you can do a line by line check, where you download your so called update file in the background and compare the current one on a line by line basis and if they are the exact same then the downloaded one is discarded, if not it just overrides the one currently on your computer and does a reboot.
Engineer #11
Posted 06 October 2013 - 02:24 PM
To check versions and etc, you need to download the file. Then it doesn't even matter if you update the file or not, cuz you already have downloaded the file.

What I'm trying to say is, don't even check for different things, just write the downloaded content to the file. Then it's the latest update of the file whatsoever.
AgentE382 #12
Posted 06 October 2013 - 03:09 PM
I've been thinking about how to do update detection.

First, if you own the paste (or it's a trending paste), you don't need to download it. Using my Pastebin API, you can get its attributes, including file size. Just compare that and download if different.

Beyond that, I was thinking about scraping Pastebin to find out whether there is a new version of the paste available. Also, I was thinking about scraping the ComputerCraft forums to find new Pastebin links.

Oh, and I was thinking about keeping a database of each paste's attributes, to avoid as many HTTP requests as possible.
mrdawgza #13
Posted 07 October 2013 - 06:51 AM
Okay, I use something like this in my OS.
It deletes the file you wish to update then redownloads it.

Code:

local function update() --update 

--/ Deleting
I used 'menu' as an example.
You can set 'prog' to one of the programs being updated. You have to delete it so there isn't a error with Pastebin.
\-- 

fs.delete("menu")
fs.delete("prog") 
fs.delete("prog2")

--/ Getting
This will now download the programs you deleted above.
Format: pastebin get (pasteid) (destination)
\--

shell.run("pastebin get menuid menu")
shell.run("pastebin get progid prog")
shell.run("pastebin get prog2id prog2")

end

update() --call the function
Sorry if it doesn't work i did this on my phone.
campicus #14
Posted 07 October 2013 - 08:13 PM
I've been thinking about how to do update detection.

First, if you own the paste (or it's a trending paste), you don't need to download it. Using my Pastebin API, you can get its attributes, including file size. Just compare that and download if different.

Beyond that, I was thinking about scraping Pastebin to find out whether there is a new version of the paste available. Also, I was thinking about scraping the ComputerCraft forums to find new Pastebin links.

Oh, and I was thinking about keeping a database of each paste's attributes, to avoid as many HTTP requests as possible.

I'm not sure if it is necessary to check if the code is an updated version or not, just replace everything. That way you always have the most recent code on your CC computer.

An internal database is a good idea, but what if a programs name is changed or something? Then the database would be incorrect (having an extra entry?). So you would still need to build a new database each time.
AgentE382 #15
Posted 07 October 2013 - 09:03 PM
The database would be indexed by paste ID. Name changes don't change that. When the database is updated, it'll get the new name.

Basically, there are two types of updates:
  1. Updates that don't change the Paste ID.
  2. Updates that do.
Handling updates that don't change the paste ID are simple. Like you said, just get the new version from the same URL. However, handling updates that do require a new paste ID are way more tricky, which is where scraping Pastebin/CCForum would come into play.


EDIT: I just realized that you might be talking about how it would affect the files on your system. I was going to keep a mapping of paste ID to filename, so you can specify whatever name you want, or even multiple names for the same paste.

For example, say you have a program that loads a button API using `os.loadAPI("MySuperAwesomeButtonAPI")` Say you like that button API, but you'd like to just call it "button". No problem, just add another reference to the database. In my (unfinished) program, you would do `pm link <pasteID> /Programs/TheCoolOne/MySuperAwesomeButtonAPI` followed by `pm link <pasteID> button`. Then it would write the paste to both locations when updating. That way, you can auto-update the other program without having to manually change the first line to `os.loadAPI("button")`.
campicus #16
Posted 07 October 2013 - 11:45 PM
The database would be indexed by paste ID. Name changes don't change that. When the database is updated, it'll get the new name.

Basically, there are two types of updates:
  1. Updates that don't change the Paste ID.
  2. Updates that do.
Handling updates that don't change the paste ID are simple. Like you said, just get the new version from the same URL. However, handling updates that do require a new paste ID are way more tricky, which is where scraping Pastebin/CCForum would come into play.


EDIT: I just realized that you might be talking about how it would affect the files on your system. I was going to keep a mapping of paste ID to filename, so you can specify whatever name you want, or even multiple names for the same paste.

For example, say you have a program that loads a button API using `os.loadAPI("MySuperAwesomeButtonAPI")` Say you like that button API, but you'd like to just call it "button". No problem, just add another reference to the database. In my (unfinished) program, you would do `pm link <pasteID> /Programs/TheCoolOne/MySuperAwesomeButtonAPI` followed by `pm link <pasteID> button`. Then it would write the paste to both locations when updating. That way, you can auto-update the other program without having to manually change the first line to `os.loadAPI("button")`.

This makes lots of sense :)/> I have not yet had a chance to play with your code (or even test it out). I'm sure it is going to be awesome :D/>
campicus #17
Posted 10 October 2013 - 12:12 AM
I have had a look at your API and things are going well so far, I can get it to retrieve all my program names :D/>


if fs.exists("pastebinAPI") then
  fs.delete("/pastebinAPI")
end
shell.run("pastebin", "get", "5SJ0d1mV", "pastebinAPI")
os.loadAPI("pastebinAPI")
pb = pastebinAPI

legg0028 = pb.login("legg0028", "notMyPW")
print(legg0028)

local allPastes = pb.list(legg0028,1000)

for i,v in ipairs(allPastes) do
  print(allPastes[i]["title"])
end

No I just need to figure out how to match the titles against my programs :P/>
xXLeNinjaXx #18
Posted 12 January 2014 - 04:00 PM
Lol i read all of the post but i need an updater for my programs so when i post something it finds it and next time when the program turns on it finds it and updates

i dint quite understand the posts
AgentE382 #19
Posted 13 January 2014 - 12:41 PM
Download my Pastebin API and put it in a file called "pb", with
pastebin get 5SJ0d1mV pb
. Then, put this in a file called "update" by typing
pastebin get UdtTvRxN update
. Use it like this: update <username> <password> It will download the latest version of all of your Pastebin pastes into the current directory.

If you want it to run every time your computer reboots, create a file named "startup" that just has this in it: update <your pastebin username> <your pastebin password>

Note that I hacked this together in like 5 minutes, so there's absolutely no error handling and it may not work.
Edited on 13 January 2014 - 11:41 AM
Henness #20
Posted 13 January 2014 - 01:52 PM
I don't have any answers for paste bin but if you look at my update program used in this program it updates programs from github. I made it a year ago.

http://www.computercraft.info/forums2/index.php?/topic/1125-advanced-programs-orefinder-tunnel/

First program is uploaded to paste bin and the rest of the programs is uploaded to github along with a config file with all the versions of each program just increment the version number and the updater with download the new version.

Updater program can be found here:
https://github.com/Henness0666/Advanced-Programs/blob/master/advancedupdater

And Hey it even comes with a fancy intro.
Lyqyd #21
Posted 13 January 2014 - 02:39 PM
Download my Pastebin API and put it in a file called "pb", with
pastebin get 5SJ0d1mV pb
. Then, put this in a file called "update" by typing
pastebin get UdtTvRxN update
. Use it like this: update <username> <password> It will download the latest version of all of your Pastebin pastes into the current directory.

If you want it to run every time your computer reboots, create a file named "startup" that just has this in it: update <your pastebin username> <your pastebin password>

Note that I hacked this together in like 5 minutes, so there's absolutely no error handling and it may not work.

You'd actually need a shell.run call, not just the string you'd directly put into the shell.

However, auto-updating from the web on every startup is a Bad Plan.
AgentE382 #22
Posted 13 January 2014 - 03:17 PM
Download my Pastebin API and put it in a file called "pb", with
pastebin get 5SJ0d1mV pb
. Then, put this in a file called "update" by typing
pastebin get UdtTvRxN update
. Use it like this: update <username> <password> It will download the latest version of all of your Pastebin pastes into the current directory.

If you want it to run every time your computer reboots, create a file named "startup" that just has this in it: update <your pastebin username> <your pastebin password>

Note that I hacked this together in like 5 minutes, so there's absolutely no error handling and it may not work.

You'd actually need a shell.run call, not just the string you'd directly put into the shell.

However, auto-updating from the web on every startup is a Bad Plan.
Definitely. I've got a much better system, but I kinda' stopped working on it. I'll have to finish it up and post it on the forums.