Browse Source

Not initial commit, it's a reseted repo

drymer 3 years ago
commit
a65580454d
6 changed files with 143 additions and 0 deletions
  1. 12
    0
      LICENSE
  2. 22
    0
      README.md
  3. BIN
      __pycache__/gnusrss.cpython-32.pyc
  4. BIN
      __pycache__/gnusrss.cpython-34.pyc
  5. 24
    0
      example.py
  6. 85
    0
      gnusrss.py

+ 12
- 0
LICENSE View File

@@ -0,0 +1,12 @@
1
+This program is free software: you can redistribute it and/or modify
2
+it under the terms of the GNU General Public License as published by
3
+the Free Software Foundation, either version 3 of the License, or
4
+(at your option) any later version.
5
+    
6
+This program is distributed in the hope that it will be useful,
7
+but WITHOUT ANY WARRANTY; without even the implied warranty of
8
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9
+GNU General Public License for more details.
10
+    
11
+You should have received a copy of the GNU General Public License
12
+along with this program.  If not, see <http://www.gnu.org/licenses/>.

+ 22
- 0
README.md View File

@@ -0,0 +1,22 @@
1
+gnusrss
2
+=======
3
+
4
+
5
+Dependencias
6
+------------
7
+
8
+* pycurl
9
+* PySocks
10
+
11
+Se pueden instalar así:
12
+
13
+    su -c "apt-get install python3-pip"
14
+    su -c "pip-3.2 install pycurl PySocks"
15
+
16
+Uso
17
+---
18
+
19
+gnurss es una libreria escrita en python 3. Sirve para postear en GNU Social el contenido de un RSS, con soporte de Tor optativo. 
20
+
21
+El archivo example.py es un ejemplo de cómo usar la libreria.
22
+

BIN
__pycache__/gnusrss.cpython-32.pyc View File


BIN
__pycache__/gnusrss.cpython-34.pyc View File


+ 24
- 0
example.py View File

@@ -0,0 +1,24 @@
1
+#!/bin/env python3
2
+# -*- coding: utf-8 -*-
3
+
4
+import gnusrss
5
+import os.path
6
+
7
+if "__main__":
8
+
9
+    feedurl = ''
10
+
11
+    #GNU Social credentials
12
+    username = ''
13
+    password = ''
14
+    api_url  = ''
15
+    tor = 'yes'
16
+    txt = 'feed'
17
+    txt_tmp = txt + '_tmp'
18
+    
19
+    if os.path.exists(txt):
20
+        gnusrss.rss(feedurl, txt_tmp, tor)
21
+        new = gnusrss.compare(txt, txt_tmp)
22
+    else:
23
+        gnusrss.rss(feedurl, txt, tor)
24
+                                                

+ 85
- 0
gnusrss.py View File

@@ -0,0 +1,85 @@
1
+#!/bin/env python3
2
+# -*- coding: utf-8 -*-
3
+
4
+import urllib.request
5
+import urllib.parse
6
+import pycurl
7
+import time
8
+import socks
9
+import socket
10
+from xml.dom import minidom
11
+
12
+
13
+def rss(feedurl, file_name, tor):
14
+    """ Request the feed, parse it and write it on a txt file. """
15
+    file = open(file_name, 'w')
16
+    feedurl = urllib.request.urlopen(feedurl)
17
+    xmldoc = minidom.parse(feedurl)
18
+    item = xmldoc.getElementsByTagName('item')
19
+    if tor == 'yes':
20
+        torify_urllib()
21
+    
22
+    for node in item:
23
+        t_list = node.getElementsByTagName('title')
24
+        for a in t_list:
25
+            title = a.firstChild.data
26
+
27
+        u_list = node.getElementsByTagName('link')
28
+        for a in u_list:
29
+            url = a.firstChild.data
30
+
31
+        msg = title + ': ' + url + '\n'
32
+        file.write(msg)
33
+    file.close()
34
+
35
+def post(new_feeds, api_url, username, password, tor):
36
+    """ Create a pycurl object to post to GNU Social using the API. """    
37
+    curl = pycurl.Curl()
38
+    curl.setopt(pycurl.URL, api_url)
39
+    curl.setopt(pycurl.USERPWD, username + ':' + password)
40
+    curl.setopt(pycurl.VERBOSE, 0)
41
+    for feed in new_feeds:
42
+        post_data = {'status' : feed, 'source' : 'gnusrss'}
43
+        postfields = urllib.parse.urlencode(post_data)
44
+        curl.setopt(curl.POSTFIELDS, postfields)
45
+        curl.setopt(pycurl.WRITEFUNCTION, lambda x: None)
46
+        if tor == 'yes':
47
+            curl = torify_curl(curl)
48
+        curl.perform()
49
+        time.sleep(10)
50
+
51
+    curl.close
52
+
53
+def compare(feed, tmp):
54
+    """ Compare the saved feed file to the temporal one. """
55
+    a_feed = [line.strip() for line in open(feed)]
56
+    t_feed = [line.strip() for line in open(tmp)]
57
+    diff = []
58
+
59
+    a_feed = set(a_feed)
60
+    for aa in t_feed:
61
+        if aa not in a_feed:
62
+            with open(feed, 'a') as file:
63
+                file.write(aa + '\n')
64
+            diff.append(aa)
65
+            file.close()
66
+
67
+    return diff
68
+
69
+def torify_urllib():
70
+    """ Torify the urllib request. """
71
+    #Patch DNS request
72
+    def create_connection(address, timeout=None, source_address=None):
73
+        sock = socks.socksocket()
74
+        sock.connect(address)
75
+        return sock
76
+    socks.set_default_proxy(socks.SOCKS5, 'localhost', 9050)
77
+    socket.socket = socks.socksocket
78
+    socket.create_connection = create_connection
79
+    
80
+def torify_curl(curl):
81
+    """ Torify the pycurl request. """
82
+    curl.setopt(curl.PROXY, 'localhost')
83
+    curl.setopt(curl.PROXYPORT, 9050)
84
+    curl.setopt(curl.PROXYTYPE, curl.PROXYTYPE_SOCKS5)
85
+    return curl