Created
March 31, 2017 08:38
-
-
Save ogero/6eb77d651fc83293fb3a456f1db00b77 to your computer and use it in GitHub Desktop.
Change modified/created times of paths and files of Kodi library based on Movie release date, for correct order display.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
_ "github.com/mattn/go-sqlite3" | |
"log" | |
"fmt" | |
"os" | |
"time" | |
"io/ioutil" | |
"encoding/json" | |
"net/http" | |
"strings" | |
"strconv" | |
) | |
func main() { | |
CACHE_DB := "./cache.db" | |
KODI_DB := "\\\\OSMC\\osmc\\.kodi\\userdata\\Database\\MyVideos107.db" | |
TMDB_QUERY := "https://api.themoviedb.org/3/find/{}?api_key=f7f51775877e0bb6703520952b3c7840&language=en-US&external_source=imdb_id" | |
cacheDB, err := sql.Open("sqlite3", CACHE_DB) | |
if err != nil { | |
panic(err) | |
} | |
defer cacheDB.Close() | |
_, err = cacheDB.Exec("CREATE TABLE IF NOT EXISTS release_dates (id INTEGER NOT NULL PRIMARY KEY, uniqueid_id TEXT, release_date TEXT);") | |
if err != nil { | |
log.Printf("%q: %s\n", err, "Failed to create cache table") | |
return | |
} | |
kodiDB, err := sql.Open("sqlite3", KODI_DB) | |
if err != nil { | |
panic(err) | |
} | |
defer kodiDB.Close() | |
rows, err := kodiDB.Query("SELECT movie.c00, movie.c22, uniqueid.value FROM movie LEFT JOIN uniqueid ON uniqueid.uniqueid_id=movie.c09") | |
if err != nil { | |
panic(err) | |
} | |
defer rows.Close() | |
for rows.Next() { | |
var name string | |
var path string | |
var id string | |
err = rows.Scan(&name, &path, &id) | |
if err != nil { | |
panic(err) | |
} else { | |
path = strings.Replace(path, "smb://", "\\\\", 1) | |
path = strings.Replace(path, "/", "\\", -1) | |
var count int | |
rows := cacheDB.QueryRow("SELECT COUNT(*) from release_dates WHERE uniqueid_id=?", id) | |
rows.Scan(&count) | |
if count == 0 { | |
fmt.Println("id=", id, " ", name, " at ", path, " needs update ") | |
url := strings.Replace(TMDB_QUERY, "{}", id, 1) | |
r := &TMDBResult{} | |
if err := GetJson(url, r); err == nil && len(r.Movies) > 0 { | |
cacheDB.Exec("INSERT INTO release_dates(uniqueid_id,release_date) VALUES (?,?)", id, r.Movies[0].ReleaseDate) | |
p := strings.Split(r.Movies[0].ReleaseDate, "-") | |
if len(p) == 3 { | |
y, _ := strconv.Atoi(p[0]) | |
m, _ := strconv.Atoi(p[1]) | |
d, _ := strconv.Atoi(p[2]) | |
Chtimes(path, y, time.Month(m), d) | |
} else { | |
fmt.Printf("Odd release date on %+v\n", r) | |
} | |
} else { | |
fmt.Printf("Failed to get release date of %s\n", name) | |
} | |
} else { | |
fmt.Println(name, " already updated") | |
} | |
} | |
} | |
err = rows.Err() | |
if err != nil { | |
panic(err) | |
} | |
} | |
type Movie struct { | |
Title string `json:"title"` | |
ReleaseDate string `json:"release_date"` | |
} | |
type TMDBResult struct { | |
Movies []Movie `json:"movie_results"` | |
} | |
func Chtimes(path string, year int, month time.Month, day int) { | |
fmt.Println("Updating date of " + path) | |
mtime := time.Date(year, month, day, 15, 0, 0, 0, time.UTC) | |
if err := os.Chtimes(path, mtime, mtime); err != nil { | |
panic(err) | |
} | |
file, err := os.Open(path) | |
if err != nil { | |
panic(err) | |
} | |
stat, err := file.Stat() | |
defer file.Close() | |
if err != nil { | |
panic(err) | |
} | |
if stat.IsDir() { | |
files, _ := ioutil.ReadDir(path) | |
for _, f := range files { | |
Chtimes(path + string(os.PathSeparator) + f.Name(), year, month, day) | |
} | |
} | |
} | |
func GetJson(url string, target interface{}) error { | |
res, err := http.Get(url) | |
if err != nil { | |
return err | |
} | |
defer res.Body.Close() | |
body, err := ioutil.ReadAll(res.Body) | |
return json.Unmarshal(body, &target) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment