first commit
[telefricabot.git/.git] / api.py
1 import requests,io,random,re
2
3 ua = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101'
4 ua += ' Firefox/60.0'
5 HEADERS = {'user-agent': ua}
6
7 class FriendApi():
8         def __init__(self,url,user,password):
9                 self.session=requests.session()
10                 r=self.session.post(url+"login/",data={'username':user,'password':password,'auth-params':'login','submit':'Acceder','openid-url':''},headers=HEADERS)
11                 if 'Login failed. Please check your credentials.' in r.text:
12                         raise ValueError('Error con la contraseƱa o usuario')
13                 print(r.json)
14                 self.url=url
15                 self.user=user
16                 self.password=password
17         def share(self,title,text):
18                 r=self.session.post(self.url.replace('login/','')+'item/',data={\
19                 'title':title\
20                 ,'body':text\
21                 ,'preview':'0'\
22                 ,'post_id':''\
23                 ,'post_id_random':str(random.randint(1,100000000000))}\
24                 ,headers=HEADERS)
25                 if '<title>Forbidden</title>' in r.text:
26                         print(r.text)
27                         print('API')
28                         raise ValueError('Error con la contraseƱa o usuario')
29                 print(r.json,r.text,'gere')
30         def notifications(self,limit):
31                 r=self.session.get(self.url+'/notifications/system',headers=HEADERS)
32                 cont=0
33                 out=''
34                 for i in re.findall('https://friendicarg.nsupdate.info/notification/.*',r.text):
35                         cont=cont+1
36                         if cont-1==limit:
37                                 break
38                         else:
39                                 out=out+'\n'+'\n'+i.replace(re.findall('"><img src=".*" aria-hidden="true" class="notif-image">',i)[0],' ').replace('<span class="notif-when">','').replace('</span></a>','')
40                 return out
41