]> gitweb.pimeys.fr Git - today.git/blob - today_server.py
1727e27d5a19b6ecb164fd0929117851471cf48f
[today.git] / today_server.py
1 #!/usr/bin/python
2 # -*- encoding: utf-8 -*-
3
4 """ Codé par 20-100
5 script qui affiche des trucs à penser, des J-n des conneries
6 or that kind of stuff.
7
8 Partie serveur, prévue pour chercher périodiquement les trucs non lus
9 et répondre à un check.
10
11 """
12
13 import re
14 from lxml import etree
15 import os
16 import sys
17 import urllib
18 import json
19 os.chdir('/home/vincent/scripts/today/')
20
21 #: Config serveur
22 import serverconfig
23
24 #: Récupération de toutes les nouveautés
25 import gather
26
27 def last_xkcd():
28 p = urllib.urlopen("http://xkcd.com")
29 t = p.read()
30 current_id = int(re.findall("Permanent link to this comic: http://xkcd.com/(.*?)/", t)[0])
31 return current_id
32
33 def last_xantah():
34 p = urllib.urlopen("http://www.adoprixtoxis.com/lite/download/xantah_downloads.php")
35 t = p.read()
36 ids = re.findall("""<div class="top">Xantah (.*?)</div>""", t)
37 ids = [int(i) for i in ids]
38 return max(ids)
39
40 def last_jl8():
41 rss = urllib.urlopen("http://limbero.org/jl8/rss/")
42 t = rss.read()
43 x = etree.fromstring(t)
44 links = x.xpath("//link")
45 maxnum = links[1].text.split("/")[-1]
46 maxnum = int(maxnum)
47 return maxnum
48
49 def get_file():
50 """Récupère la liste des derniers ids de chaque truc, stockée dans le fichier."""
51 f = open(serverconfig.store_published_file)
52 news = json.load(f)
53 f.close()
54 return news
55
56 def update_file(news):
57 """Met à jour la liste des derniers ids dans le fichier."""
58 f = open(serverconfig.store_published_file, 'w')
59 json.dump(news, f)
60 f.close()
61
62 FETCHS = {
63 "xkcd" : last_xkcd,
64 "xantah" : last_xantah,
65 "dc" : last_jl8,
66 }
67
68 def fetch_all():
69 """Va chercher sur les différents sites les nouveaux trucs."""
70 news = {}
71 for (k, f) in FETCHS.iteritems():
72 try:
73 news[k] = f()
74 except Exception as e:
75 raise
76 news.update(gather.gather())
77 return news
78
79 def sync():
80 """Reçoit une requête de synchronisation."""
81 # On récupère où en est le client sur stdin
82 t = sys.stdin.read()
83 on_client = json.loads(t)
84 # On récupère où en est le serveur dans le fichier idoine
85 if os.path.isfile(serverconfig.store_seen_file):
86 on_server = json.load(open(serverconfig.store_seen_file))
87 else:
88 on_server = {}
89 # On garde le maximum
90 for k in set(on_client.keys() + on_server.keys()):
91 on_server[k] = max(on_client.get(k, 0), on_server.get(k, 0))
92 # On enregistre ce nouveau dico
93 json.dump(on_server, open(serverconfig.store_seen_file, "w"))
94 # On envoie au client ce nouveau dico
95 print json.dumps(on_server)
96
97 if __name__ == "__main__":
98 DEBUG = ("--debug" in sys.argv) or ("--verbose" in sys.argv) or serverconfig.DEBUG
99 if sys.argv[1] == "check":
100 news = fetch_all()
101 if "--init" in sys.argv:
102 olds = news
103 else:
104 olds = get_file()
105 olds.update(news)
106 update_file(olds)
107 elif sys.argv[1] == "whatsup":
108 news = get_file()
109 print json.dumps(news)
110 elif sys.argv[1] == "sync":
111 sync()