summaryrefslogtreecommitdiff
path: root/feeder/feeder.py
blob: 39b43e5bfafb82c4a7861a6844ea586b023131ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/python

import commands
#import MySQLdb
import urllib
import sha
import sys
import time
import re
import os
def now ():
        return int(time.mktime(time.localtime()))

SERVER_HOST = 'scannerjammer.com'
SERVER_PORT = 80 

API_HEADER = "#@scanjam 0.2\n"

HTML_TITLE_RE = re.compile('<title>([^<]+)</title>')

DUPE_LIST = "feeder/dupes.test"
#DUPE_LIST = "feeder/dupes.txt"
FEED_LIST = "feeder/feeds.txt"
#FEED_PATH = "feeder/feeds"
FEED_PATH = "feeder/test"
FEED_STALE_TIME = 0;
#FEED_STALE_TIME = 3600
FEED_ROOM = "feederbleeder"


API_LOGIN = "http://"+SERVER_HOST+":"+str(SERVER_PORT)+"/api/auth/sneakin"+"/"
API_POST_VIDEO = "http://"+SERVER_HOST+":"+str(SERVER_PORT)+"/api/room/video"+"/"
API_POST_IMAGE = "http://"+SERVER_HOST+":"+str(SERVER_PORT)+"/api/room/say"+"/"
API_LOGOUT = "http://"+SERVER_HOST+":"+str(SERVER_PORT)+"/api/auth/logout"+"/"

#{{{ **USE IF YOU ADD APPEND_SLASH
#API_LOGIN = "http://"+SERVER_HOST+":"+str(SERVER_PORT)+"/api/auth/sneakin"
#API_POST_VIDEO = "http://"+SERVER_HOST+":"+str(SERVER_PORT)+"/api/room/video"
#API_POST_IMAGE = "http://"+SERVER_HOST+":"+str(SERVER_PORT)+"/api/room/say"
#API_LOGOUT = "http://"+SERVER_HOST+":"+str(SERVER_PORT)+"/api/auth/logout"
#}}}

print API_LOGIN 
print API_POST_VIDEO 
print API_POST_IMAGE 
print API_LOGOUT 


dupes = {}

class Feed:
  def __init__ (self, src, title, userid):
    self.src = src
    self.title = title
    self.userid = userid
    self.domain = "http://" + src.split("/")[2]
    self.urls = []
    self.images = []
    self.load()

  def load (self):
    filename = "/".join([FEED_PATH, self.title])
    refresh = True

    # check last update of feed
    if os.path.exists(filename):
      stat = os.stat(filename)
      if stat.st_mtime > now() - FEED_STALE_TIME:
        refresh = False
  
    # if stale/empty then download
    if refresh:
      print self.title, "loading from web"
      feedhtml = urllib.urlopen(self.src).read()
      if len(feedhtml):
        out = open(filename, 'w')
        out.write(feedhtml)
      self.parse(feedhtml)

    # otherwise, load from disk
    else:
      print self.title, "loading from disk"
      feed = open (filename, 'r')
      feedhtml = feed.read()
      feed.close()
      self.parse(feedhtml)

  # parse urls out of html files
  # display these urls (by date, by source)
  def parse (self, html):
    tags = html.replace("&gt;","<").split("<")
    lastimage = ""
    for t in tags:
      url = None
      if len(t) < 1:
        continue
      if t[0] == "a":
        if "href" not in t:
          continue
        url = self.getAttribute("href", t)
      elif t[0] == "iframe":
        if "src" not in t:
          continue
        url = self.getAttribute("src", t)
      elif t[0:3] == "img":
        if "src" not in t:
          continue
        if "php" in t:
          continue
        url = self.getAttribute("src", t)
        if url is None:
          continue
        if url in dupes:
          continue
        if url[-3:] != "jpg":
          continue
        print url
        lastimage = url
        dupes[url.strip()] = True
        self.images.append(url)
        continue
      else:
        continue

      if url is None:
        continue
      if url in dupes:
        continue
      if "youtube.com" in url:
        dupes[url.strip()] = True
        self.urls.append(url)
      if "youtu.be" in url:
        dupes[url.strip()] = True
        self.urls.append(url)
      if "vimeo.com" in url:
        dupes[url.strip()] = True
        # http://player.vimeo.com/video/23731158
        if "http://player.vimeo.com/video/" in url:
          url = "http://vimeo.com/" + url.replace('http://player.vimeo.com/video/', '')
        self.urls.append(url)
      if "soundcloud.com" in url:
        dupes[url.strip()] = True
        self.urls.append(url)
      if url[-3:] == "mp3":
        dupes[url.strip()] = True
        u = url.replace(" ","%20")
        self.urls.append(lastimage+" "+u)

  def getAttribute (self, attr, s):
    quote = None
    if '\"' in s:
      quote = '\"'
    elif '\'' in s:
      quote = '\''
    else:
      return None

    attrpos = s.find(attr)
    startquotepos = s.find(quote, attrpos+1)
    endquotepos = s.find(quote, startquotepos+1)
    url = s[startquotepos+1:endquotepos]
    #if url[0] == "/":
    #  url = self.domain + url
    if url[0:4] != "http":
      return None
    return url
  def getTitle (self, s):
    if '>' in s:
      return s.split(">")[1]
    return None
  def login (self):
    print "getting token for", self.title
    data = urllib.urlencode({ 'userid': self.userid, 'username': self.title })
    f = urllib.urlopen(API_LOGIN, data)
    api = f.read().split("\n")
    if api[0] != "#@scanjam 0.3b" or api[1] != "OK":
      print "ERROR GETTING TOKEN"
      return None
    payload = api[2].split("\t")
    print "GOT SESSION:", payload[2]
    time.sleep(0.5)
    return payload[2]
  def report (self):
    if len(self.urls) == 0 and len(self.images) == 0:
      print self.title, "nothing to do"
      return
    self.session = self.login()
    if self.session is None:
      print self.title, "error getting session"
      return
    print ""
    print self.title, "reported", len(self.urls), "urls,", len(self.images), "images"
    for url in reversed(self.urls):
      if "wearesolidgold" in url:
        continue
      if url == "http://vimeo.com/":
        continue
      print "URL", url
      data = urllib.urlencode({ 'session': self.session, 'room': FEED_ROOM, 'msg': url })
      f = urllib.urlopen(API_POST_VIDEO, data)
      print f.read()
      print data, API_POST_VIDEO; exit(0);
      time.sleep(5)
    for url in reversed(self.images):
      print "IMG", url
      data = urllib.urlencode({ 'session': self.session, 'room': FEED_ROOM, 'msg': url })
      f = urllib.urlopen(API_POST_IMAGE, data)
      time.sleep(5)

def load_dupes ():
  dupelist = open (DUPE_LIST, 'r')
  for line in dupelist:
    dupes[line.strip()] = True
  dupelist.close()

def load_feeds ():
  feedlist = open (FEED_LIST, 'r')
  feeds = []
  for line in feedlist:
    src,title,userid = line.strip().split("\t")
    feed = Feed (src,title,userid)
    feeds.append(feed)
  feedlist.close()
  for feed in reversed(feeds):
    feed.report()

def save_dupes ():
  dupelist = open (DUPE_LIST+".tmp", 'w')
  for k,v in dupes.iteritems():
    dupelist.write(k.strip()+"\n")
  dupelist.close()
  os.rename(DUPE_LIST+".tmp", DUPE_LIST)

load_dupes()
load_feeds()
save_dupes()