Skip to content

Instantly share code, notes, and snippets.

View vladbatushkov's full-sized avatar
🥥
Sabai sabai

Vlad Batushkov vladbatushkov

🥥
Sabai sabai
View GitHub Profile
@vladbatushkov
vladbatushkov / omgc-may-9-2.cypher
Last active May 9, 2019 16:00
List ordered by year with number of joined countries + total + enumeration of countries.
MATCH (c:Country)-[:JOINED_AT]->(y:Year)
WITH y.value as year, apoc.coll.sort(collect(c.name)) as cs
WITH year, reduce(r = "", x IN cs | CASE WHEN r = "" THEN x ELSE (r + ", " + x) END) as desc, reduce(n = 0, x IN cs | n + 1) as num
WITH collect({ year: year, num: num, desc: desc }) as all
WITH all, reduce(s = 0, x IN all | s + x.num) as total
WITH apoc.coll.union(all, [{ year: "Total", num: total, desc: "" }]) as result
UNWIND result as item
RETURN item.year as year, item.num as countriesJoined, item.desc as countries
ORDER BY item.year
@vladbatushkov
vladbatushkov / omgc-may-10-1.cypher
Created May 10, 2019 15:30
List all not terminated IATA codes.
MERGE (bkk:Airport { IATA: "BKK" })
WITH ("https://en.wikipedia.org/wiki/List_of_Thai_Airways_destinations") as url, bkk
CALL apoc.load.html(url, { codes: "table.sortable tr[style!=\"background-color:#DDDDDD\"] td:eq(2)" }) YIELD value
WITH filter(code IN value.codes WHERE code.text <> "BKK") as outsideCodes, bkk
UNWIND outsideCodes as code
MERGE (a:Airport { IATA: code.text })
MERGE (bkk)-[:TO]->(a)
@vladbatushkov
vladbatushkov / omgc-may-10-2.cypher
Created May 10, 2019 15:31
Cayman Airlines directions.
WITH ("https://vbatushkov.bitbucket.io/cayman.json") as url
CALL apoc.load.json(url) YIELD value
WITH value.origins AS os, value.destinationsPerOrigin as ds
UNWIND os as origin
FOREACH (d IN ds[origin] |
MERGE (a:Airport { IATA: origin })
MERGE (b:Airport { IATA: d })
MERGE (a)-[:TO]->(b)
)
@vladbatushkov
vladbatushkov / omgc-may-10-3.cypher
Created May 10, 2019 15:33
PageRank to Cayman Airlines.
CALL algo.pageRank.stream('Airport', 'TO', { iterations:20, dampingFactor:0.85 })
YIELD nodeId, score
RETURN algo.asNode(nodeId).IATA as IATA, score
ORDER BY score DESC
@vladbatushkov
vladbatushkov / omgc-may-10-4.cypher
Created May 10, 2019 15:35
Find path from BKK to Cayman Islands.
MATCH path = (:Airport { IATA: "BKK" })-[:TO*1..3]->(to)
WHERE to.IATA = "CYB" OR to.IATA = "LYB" OR to.IATA = "GMC"
RETURN path
@vladbatushkov
vladbatushkov / omgc-may-10-5.cypher
Created May 10, 2019 15:36
List of Japanese Airlines destinations IATA codes
MERGE (j:Airport { IATA: "HND" })
WITH ("https://en.wikipedia.org/wiki/List_of_Japan_Airlines_destinations") as url, j
CALL apoc.load.html(url, { codes: "table.sortable tr[style!=\"background-color:#ddd\"] td:eq(3)" }) YIELD value
WITH filter(code IN value.codes WHERE code.text <> "HND") as outsideCodes, j
UNWIND outsideCodes as code
MERGE (a:Airport { IATA: code.text })
MERGE (j)-[:TO]->(a)
CALL apoc.load.html("https://www.last.fm/music", { data: "a.music-more-tags-tag-inner-wrap, section.music-section:eq(0) a.music-featured-item-heading-link" }) YIELD value
UNWIND value.data as n
MERGE (g:Genre { url: n.attributes.href, name: apoc.text.capitalizeAll(n.text) })
MATCH (g:Genre)
CALL apoc.load.html("https://last.fm" + g.url + "/artists", { data: "h3.big-artist-list-title a" }) YIELD value
UNWIND value.data as n
MERGE (b:Band { name: n.text })
MERGE (b)-[:OF]->(g)
@vladbatushkov
vladbatushkov / omgc-may-11-3.cypher
Created May 11, 2019 16:56
The most "mix-genre" bands.
MATCH (b:Band)-[:OF]->(g:Genre)
RETURN b.name, count(g) as genres
ORDER BY genres DESC
@vladbatushkov
vladbatushkov / omgc-may-11-4.cypher
Created May 11, 2019 16:57
The most "mix-band" genres.
MATCH (g1:Genre)<-[:OF]-(b:Band)-[:OF]->(g2:Genre)
RETURN g1.name as genre_name, count(DISTINCT b) as num_of_bands
ORDER BY num_of_bands DESC